• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• Re: Finding PathItem Intersection Points

# Finding PathItem Intersection Points

Explorer ,
Dec 06, 2023 Dec 06, 2023

Copied

Is there any (relatively) simple method of finding the point or points at which two PathItems intersect? If not, does anyone know of code that is already in existence to determine intersections using the anchors + (right + left  Direction) properties of PathPoints?

TOPICS
Scripting

Views

244

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
6 Replies 6
Guide ,
Dec 06, 2023 Dec 06, 2023

Copied

I have not seen a ready-made script.  There's a well known algorithm out there to find the intersection of two cubic Bezier curvese (the Bezier subdivision algorithm), but it is not simple, and it will take some work to write it into a script.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Dec 06, 2023 Dec 06, 2023

Copied

Aye, I really appreciate the guidance. Looks like I may have some fun math the attempt to figure out. Will definitely look into re-creating this algorithm in Extendscript and see what I can come up with. Was really hoping someone had already figured it out haha.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
Dec 07, 2023 Dec 07, 2023

Copied

If you are working with simple paths, you can implement the Live Paint trick in a script. After Live Paint > Expand, a point is created at the intersection. We can compare the original array of points and the new array to find the coordinates of the new point. I find this trick fun, but it may not work in some cases.

Because Live Paint > Expand shifts the path points slightly, the calculated coordinates of the intersection points are not quite accurate. If you need maximum coordinate accuracy.

``````function main() {
if (selection.length !== 2 || selection.typename === 'TextRange') return;
var path1 = selection[0];
var path2 = selection[1];
if (path1.typename !== 'PathItem' || path2.typename !== 'PathItem' ) return;

path1.duplicate();
path1.selected = false;

path2.duplicate();
path2.selected = false;

selection[0].translate(0, 0);
try {
} catch (e) {}

var diff = findDiffPoints(path2.pathPoints, selection[1].pathPoints);

for (var i = selection.length - 1; i >= 0; i--) {
selection[i].remove();
}
}

function findDiffPoints(points1, points2) {
var diff = [];
for (var i = 0; i < points2.length; i++) {
var found = false;
for (var j = 0; j < points1.length; j++) {
if (matchNum(points2[i].anchor[0], points1[j].anchor[0], 0.1) &&
matchNum(points2[i].anchor[1], points1[j].anchor[1], 0.1)) {
found = true;
break;
}
}
if (!found) diff.push([points2[i].anchor[0].toFixed(3), points2[i].anchor[1].toFixed(3)]);
}
return diff;
}

function matchNum(num1, num2, tolerance) {
var diff = Math.abs(num1 - num2);
return diff <= tolerance;
}

main();``````

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Dec 07, 2023 Dec 07, 2023

Copied

Cool idea!

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
Dec 07, 2023 Dec 07, 2023

Copied

Thanks, Mark. Despite the coordinate shift when recalculating Bezier curves with Adobe Illustrator. However, Pathfinder also shifts the points. Also, after Live Paint > Expand, the path may split into several segments. Nevertheless, I used this trick when I wrote my DivideBottomPath script.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Dec 08, 2023 Dec 08, 2023

Copied

LATEST

Wow, this is really neat. Unfortunately, my implementation of this requires very high accuracy. The script I am working on divides paths that are used to cut fabric on an industrial cutting machine. It essentially allows the user to draw a line across a cut shape and then splits the shape and includes the necessary overlap between the two panels. A lot of times these cut paths are in 10th scale so a very slight shift in the decimal point would mean a drastic shift in the cut shape at full scale.