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

Finding PathItem Intersection Points

Explorer ,
Dec 06, 2023 Dec 06, 2023

Copy link to clipboard

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

237

Translate

Translate

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 guidelines
Adobe
Guide ,
Dec 06, 2023 Dec 06, 2023

Copy link to clipboard

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.   

Votes

Translate

Translate

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 guidelines
Explorer ,
Dec 06, 2023 Dec 06, 2023

Copy link to clipboard

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.

Votes

Translate

Translate

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 guidelines
Enthusiast ,
Dec 07, 2023 Dec 07, 2023

Copy link to clipboard

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.

2023-12-07 16.17.44.gif

 

 

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;

  app.executeMenuCommand('group');
  app.executeMenuCommand('Make Planet X');
  selection[0].translate(0, 0);
  app.executeMenuCommand('Expand Planet X');
  try {
    app.executeMenuCommand('ungroup');
    app.executeMenuCommand('ungroup');
  } catch (e) {}

  var diff = findDiffPoints(path2.pathPoints, selection[1].pathPoints);
  alert('Intersection points:\n' + diff.join('\n'));

  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();

 

 

Votes

Translate

Translate

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 guidelines
Community Expert ,
Dec 07, 2023 Dec 07, 2023

Copy link to clipboard

Copied

Cool idea!

Votes

Translate

Translate

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 guidelines
Enthusiast ,
Dec 07, 2023 Dec 07, 2023

Copy link to clipboard

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.

divideBottomPath.gif

 

Votes

Translate

Translate

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 guidelines
Explorer ,
Dec 08, 2023 Dec 08, 2023

Copy link to clipboard

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.

Votes

Translate

Translate

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 guidelines