Sg2.9
Adding points to curves
Recent RSS
Adding points to curves
From:  Alex
Date:  9. January 2012, 16:11

Hi all,

I couldn't find answers to my questions on the forum. I am trying to add points to a curve, separated from each others by a predefined distance. Any suggestions for relevant topics on the forum?

I have also looked around and found a few things:
CurveLocation
Cuvetopoints
path.getLocation(length)
path.getLength(location)
path.getPoint(length)
path.split(location)

Any advice?

Re: Adding points to curves
From:  pqbd
Date:  9. January 2012, 22:38

Like this?

http://scriptographer.org/forum/help/uneven-distribution-in-curvestop/

Re: Adding points to curves
From:  Alex
Date:  10. January 2012, 10:02

hi pqbd,

thanks for the link! but it seems the script is a bit outdated and it seems i am not savvy enough at the moment to update it to the current scriptographer syntax ...

i changed line 7 from
var sel = document.getMatchingItems(Path, { selected: true });
to
var sel = document.selectedItems;

but on line 33 there is a problem i don't understand:
cv = new Curve(art.segments, i);

console says:
Java constructor for "com.scriptographer.ai.Curve" with arguments "com.scriptographer.ai.SegmentList,number" not found.
at Scriptographer/divide_sample_previous.js:33
at Scriptographer/divide_sample_previous.js:12
at Scriptographer/divide_sample_previous.js:5

any advice how to rephrase that?

many thanks

Re: Adding points to curves
From:  pqbd
Date:  10. January 2012, 15:20

Oh yeah, that's the old curve constructor. Try

cv = new Curve(art.curves[i]);

Re: Adding points to curves
From:  Alex
Date:  11. January 2012, 10:32

hi pqhd,

thanks again! but the syntax is still not correct/outdated ... it's really funny the script is super short, but i am not able to update it ... checking the reference didn't help me.

any hint how to resolve this?

line 45
t = cv.getParameterWithLength( total_length, 0.1 );

thanks.

Re: Adding points to curves
From:  pqbd
Date:  12. January 2012, 08:34

The older versions of scriptographer have the previous documentation, should you need it.

t=getParameter(total_length);

Re: Adding points to curves
From:  Alex
Date:  12. January 2012, 13:01

thanks! it's now working :)

here is the edited script ... just in case some else is stumbling across the same problem.

Re: Adding points to curves
Date:  7. March 2012, 15:54

I just wrote this little function as I remembered that this topic had recently been debated here on this forum. It basically does the same thing but it couldn't hurt having two different solutions. Maybe it will provide some help to people new to scripting. With this function you can choose if you want to divide the path by a certain number OR divide the path by a given distance (in points).
Anyway, my function looks like this:

function divideEven(obj, num, divOrDist, fit){
		if(arguments.length != 4 ||
			typeof arguments[0] !== 'object' ||
			typeof arguments[1] !== 'number' ||
			typeof arguments[2] !== 'boolean' ||
			typeof arguments[3] !== 'boolean' ||
			arguments[1] <= 0) return false

		var pathLength = obj.length
		var points = []
		var divs = Math.round(num)

		if(!divOrDist){
			if(fit){
				divs = Math.round(pathLength / num)
			} else {
				divs = pathLength / num
			}
		}

		for (var i = 0; i < divs; i++){
			points.push(obj.getPoint(i / divs * pathLength))		
		}
		points.push(obj.getPoint(pathLength))

		return points
}

It returns an array with points evenly distributed over the provided path.
The arguments are:

  • obj - the path object which will be divided.
  • num - the number of divisions on the path IF the next argument is 'true'. If, however, the next argument is 'false' this number is the distance between each division.
  • divOrDist - true or false. Divide by number or divide by distance.
  • fit - If this argument is 'true' the function will adjust the distance to fit evenly over the path. If 'false' the distance will fit exactly over the path.

Confusing? This image may help clarify.

Use this code to apply this function to all selected paths and mark each point with a circle:

var sel = document.selectedItems

for(var i = 0; i < sel.length; i++){
	var arr = divideEven(sel[i], 3, true, true)
	for(var j = 0; j < arr.length; j++){
		var c = new Path.Circle(new Point(arr[j]), 4)
	}
}

Note that if a closed path is submitted, its start and end point are the same so the returned array will start and end with points that has the same coordinates.

Re: Adding points to curves
From:  Alex
Date:  8. March 2012, 10:35

sweet :)

this seems to be a really nice and well structure solution.

thanks for sharing!!

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
L-System 26.10.11