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

Geometric test functions

Guide ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Untitled.png

 

Testing whether a selected path intersects() a selected path:

 

var intersects = function() {
    if (app.selection.length == 1) {
        return false;
    }
    app.executeMenuCommand("group");
    app.executeMenuCommand("Live Pathfinder Intersect");
    app.executeMenuCommand("expandStyle");
    app.executeMenuCommand("ungroup");
    var a = app.selection.length;
    app.redraw();
    app.undo();
    return a == 1;
}
alert( intersects() );

 

Testing whether a selected path contains() a selected path:

 

var contains = function() {
    app.executeMenuCommand("group");
    app.executeMenuCommand("Live Pathfinder Subtract");
    app.executeMenuCommand("expandStyle");
    app.executeMenuCommand("ungroup");
    var a = app.selection[0].typename;
    var b = app.selection.length;
    app.redraw();
    app.undo();
    return a == "CompoundPathItem" || b == 2;
    // if a front path totally eclipsing a back path shouldn't count as contains()
    // then remove the b == 2 part of the expression
}
alert( contains() );

 

Whether a path contains() a point can be tested by first converting the point to a path with the same coordinates:

 

var convertPointToPath = function (x, y) {
    var path1 = app.activeDocument.pathItems.add();
    path1.setEntirePath([ [x, y], [x, y] ]);
    return path1;
}
// e.g.
var w = app.activeDocument.width;
var h = - app.activeDocument.height;
var tempPoint = convertPointToPath(w/2, h/2);

 

  1. Feedback is appreciated.
  2. Is there any way to do away with redraw()?  Presently, without redraw(), undo() undos the script and a step before. 

 

Thanks in advance. 

TOPICS
Scripting

Views

685

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

correct answers 1 Correct answer

Guide , May 31, 2021 May 31, 2021
quote

I would rather work with a duplicate than redrawing and/or undoing.


By @CarlosCanto

 

That sounds better. 

 

How about

 

var intersects = function() {
    if (app.selection.length != 2) {
        return false;
    }
    var path1 = app.selection[0].duplicate(),
    path2 = app.selection[1].duplicate();
    app.selection = null;
    path1.selected = true;
    path2.selected = true;
    app.executeMenuCommand("group");
    app.executeMenuCommand("Live Pathfinder Intersect");
    app.executeMenuCommand("e
...

Votes

Translate

Translate
Adobe
Community Expert ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

I would rather work with a duplicate than redrawing and/or undoing.

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
Guide ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

quote

I would rather work with a duplicate than redrawing and/or undoing.


By @CarlosCanto

 

That sounds better. 

 

How about

 

var intersects = function() {
    if (app.selection.length != 2) {
        return false;
    }
    var path1 = app.selection[0].duplicate(),
    path2 = app.selection[1].duplicate();
    app.selection = null;
    path1.selected = true;
    path2.selected = true;
    app.executeMenuCommand("group");
    app.executeMenuCommand("Live Pathfinder Intersect");
    app.executeMenuCommand("expandStyle");
    app.executeMenuCommand("ungroup");
    var a = app.selection.length;
    for (var i = app.selection.length - 1; i > -1; i--) {
        app.selection[i].remove();
    }
    return a == 1;
}
alert( intersects() );

 

var contains = function() {
    if (app.selection.length != 2) {
        return false;
    }
    var path1 = app.selection[0].duplicate(),
    path2 = app.selection[1].duplicate();
    app.selection = null;
    path1.selected = true;
    path2.selected = true;
    app.executeMenuCommand("group");
    app.executeMenuCommand("Live Pathfinder Subtract");
    app.executeMenuCommand("expandStyle");
    app.executeMenuCommand("ungroup");
    var a = app.selection[0].typename,
    b = app.selection.length;
    for (var i = app.selection.length - 1; i > -1; i--) {
        app.selection[i].remove();
    }
    return a == "CompoundPathItem" || b == 2;
}
alert( contains() );

 

 

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 ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

the script works great, but one small circle fully inside a bigger circle but not touching should be "false" (not intersecting), no?

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 ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

same as these two objects, intersecting returns "true"

intersect.JPG

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
Guide ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

Thanks @CarlosCanto .

 

The intention was that if there was "overlap", whether in part or in whole, "intersect" would be true. May be "intersect" was a poor choice of word; "overlap" would have been better.

 

And I was thinking of closed paths or semi-closed paths. I have wanted to do a function which returns the point(s) where lines intersect, but I left that for later.

 

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 01, 2021 Jun 01, 2021

Copy link to clipboard

Copied

i don't think intersect is the wrong word. if the paths were filled instead of stroked then they would indeed intersect. i think intersection means that the outer limits of two shapes share at least some real estate.

 

cool stuff

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 01, 2021 Jun 01, 2021

Copy link to clipboard

Copied

LATEST

I see, the Bounding Boxes do intersect.

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