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

How to test if two PathPoints are the same

Contributor ,
Jun 17, 2018 Jun 17, 2018

Copy link to clipboard

Copied

I am trying to write a function that takes a PathPoint and returns its index relative to it's parent PathItem. Something like this:

function getPathPointIndex(pathPoint) {

     var pathPoints = pathPoint.parent.pathPoints,

          i,

          length = pathPoints.length;

     for (i = 0; i < length; i++) {

          if (pathPoint === pathPoints) {

               return i;

          }

     }

     return false;

}

However I have found when working with PathPoint objects even if they represent the same point they rarely identify as the same object. This can be demonstrated with the following example. Create a new Illustrator document and draw a path with at least two points then run the following:

var pointA = app.activeDocument.pathItems[0].pathPoints[1],

     pointB,

     i,

     length = pointA.parent.pathPoints.length;

for (i = 0; i < length; i++) {

     var pointB = pointA.parent.pathPoints;

     if (pointA === pointB) {

          $.writeln('index ' + i + ': Same');

     } else {

          $.writeln('index ' + i + ': Different');

     }

}

On my system the pointA and pointB are never the same. It doesn't make much that a point doesn't exist in it's parent element.

You will notice that I used the second PathPoint (index 1) to demonstrate this issue. This is because the script works as expected if the first PathPoint (index 0) is used.

It would be great if somebody could please:

1. Explain this phenomenon

2. Provide a solution to testing if two PathPoints are the same

3. Get the index of a PathPoint relative to its parent PathItem (this should be easy if point to can be achieved)

Thanks

TOPICS
Scripting

Views

472

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
Explorer ,
Jun 18, 2018 Jun 18, 2018

Copy link to clipboard

Copied

1. I'm not in front of my computer, so I'm not 100% sure, but I believe it depends on how the variable is being passed and read. Flanagan's "Definitive guide to Javascript" and Crockford's "Javascript - the Good Parts" probably provides a detailed explanation. Keep in mind, that Extendscript is Adobe's implementation of ECMA3. ( Update: Check out the "Object Equality" linked provided by Canto. )


2. Solution:
Don't compare the pathPoints object, compare the x,y values for pathPoints.anchor and/or left and right direction.

3. I'm sure there's a far more elegant solution but this should work. I did not test it on Ai yet.

function getPointIndex( point ) 
{
var i;
var pth = point.parent; // pathItem
var lth = pth.pathPoints.length;
var aX = point.anchor[0], aY = point.anchor[1];

for ( i = 0; i < lth; i++ ) {
  if ( pth.pathPoints.anchor[0] == aX && pth.pathPoints.anchor[1] == aY ) return i;
}
}
 

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 ,
Jun 18, 2018 Jun 18, 2018

Copy link to clipboard

Copied

here's a link for detailed information about comparing Objects in Javascript.

Object Equality in JavaScript - A Drip of JavaScript

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 ,
Jun 19, 2018 Jun 19, 2018

Copy link to clipboard

Copied

LATEST

Thanks for sharing this link Canto

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