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.