Sg2.9
Problem with drawing a line
Recent RSS
Problem with drawing a line
From:  Rolf
Date:  13. February 2010, 15:44

Have been looking at this for an hour, but can't find the problem.
Can anyone help?

var matriXdots = 5;
var matriXspacing = 25;
var maxLine = 2 * matriXspacing;
var clr = new CMYKColor(0,0,0,1); // set stroke color to black, if the selected objects already have a strok color, that will be used. 
puntArray = new Array();

var fromX = 0;
var fromY = 0;
var toX = 0;
var toY = 0;

for (a=0; a<matriXdots; a++) {
	for (b=0; b<matriXdots; b++) {
		id = a+"-"+b;
		
		xpos = 50 * a;
		ypos = 50 * b;
		
		var circle = new Path.Circle(new Point(xpos, ypos), 5); 
		
		currentLoc = [];
		currentLoc[0] = xpos;
		currentLoc[1] = ypos;
		
		puntArray.push(currentLoc);
	}
}

while(puntArray.length > 0){
	randomFrom = Math.floor(Math.random() * puntArray.length);
	from = puntArray[randomFrom];
	randomTo = Math.floor(Math.random() * puntArray.length);
	to = puntArray[randomTo];
	
	fromX = parseInt(from[0]);
	fromY = parseInt(from[1]);
	toX = parseInt(to[0]);
	toY = parseInt(to[1]);
	
	xdistance = Math.abs(fromX - toX);
	ydistance = Math.abs(fromY - toY);
	distance = Math.sqrt(Math.pow(xdistance,2) + Math.pow(ydistance,2));
	

	if(distance < maxLine){	
		path = new Path();
		// i think the problem is here
		path.moveTo(fromX,fromY); //if I change this to 0,0 it works
		path.lineTo(toX, toY); //if I change this to 0,0 it works
		// but I can't figure out why it works if one of the two is 0, but doesn't work otherwise
	
		puntArray.splice(randomFrom,1);
		puntArray.splice(randomTo,1);
	} else
	{
		path.remove();	
	}
}
Re: Problem with drawing a line
Date:  13. February 2010, 16:52

There seems to be a problem with the code tag on the website - so although other people can't see your code, I was able to log in and find it.

I've cleaned up your code to use the Point object more, I also used the getDistance function of Point to calculate the distance between the points in your grid.

The problem is quite simple: 'distance' will never be smaller then 'maxLine', unless the distance is 0 - i.e. when the 'randomFrom' and 'randomTo' indexes are the same.

Also, you're using splice to remove the points from the array - but since you're splicing twice, the index of the second point could have changed in the mean time:

for example the following code won't remove both 'one' and 'two' from the array:

var array = ['one', 'two'];
var firstIndex = 0;
var secondIndex = 1;
array.splice(firstIndex, 1);

// the array now only has a length of 1,
// and its only remaining member has an index of 0, not 1!
array.splice(secondIndex, 1);
print(array); // 'two'

So we have to splice the indexes in order from highest, to lowest:

var array = ['one', 'two'];
var firstIndex = 0;
var secondIndex = 1;
array.splice(secondIndex, 1);
array.splice(firstIndex, 1);
print(array.length); // 0

I've attached the fixed script - it will run endlessly if it does not connect all the points, so you have to click on cancel when the progress bar shows up.. I guess you could add a maximum for the amount of times the loop is allowed to run..

var matriXdots = 20;
var matriXspacing = 25;
var maxLine = 2 * matriXspacing;
var points = [];

for (x = 0; x < matriXdots; x++) {
	for (y = 0; y < matriXdots; y++) {
		var point = new Point(x, y) * 50;
		var circle = new Path.Circle(point, 5); 
		points.push(point);
	}
}

while(points.length > 0) {

	var randomFrom = Math.floor(Math.random() * points.length);
	var randomTo = Math.floor(Math.random() * points.length);

	var from = points[randomFrom];
	var to = points[randomTo];
	
	var distance = from.getDistance(to);
	if(distance <= maxLine && randomFrom != randomTo) {
		var path = new Path.Line(from, to);
		
		// order the indexes first
		var indexes = [randomFrom, randomTo].sort(function(a, b) {
		  return b - a;
		});

		points.splice(indexes[0], 1);
		points.splice(indexes[1], 1);
	}
}

Let me know if you have any questions!

(edited the script a bit after first post)

Re: Problem with drawing a line
Date:  13. February 2010, 17:25

Ofcourse its simpler to just remove one of the random points - and it leads to quite a nice effect, since you end up creating connected lines (see attached script)

var matriXdots = 20;
var matriXspacing = 25;
var maxLine = 1.5 * matriXspacing;
var points = [];

for (x = 0; x < matriXdots; x++) {
	for (y = 0; y < matriXdots; y++) {
		var point = new Point(x, y) * matriXspacing;
		points.push(point);
	}
}

while(points.length > 0) {

	var randomFrom = Math.floor(Math.random() * points.length);
	var randomTo = Math.floor(Math.random() * points.length);

	var from = points[randomFrom];
	var to = points[randomTo];
	
	var distance = from.getDistance(to);
	if(distance <= maxLine && randomFrom != randomTo) {
		var path = new Path.Line(from, to);
		new Path.Circle(from, 5);
		new Path.Circle(to, 5);
		points.splice(randomFrom, 1);
	}
}
Re: Problem with drawing a line
From:  Rolf
Date:  13. February 2010, 18:10

Awesome!
Dank je Jonathan! Tot snel.

Re: Problem with drawing a line
From:  Rolf
Date:  13. February 2010, 20:09

Zoiets ben ik aan het maken

Re: Problem with drawing a line
From:  Jürg Lehni
Date:  13. February 2010, 20:17

Nice script! And the code tag is fixed now.

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Lorem Ipsum Generator 25.06.11