Geometric test functions

Enthusiast ,
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

408

Likes

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

Enthusiast , May 31, 2021 May 31, 2021
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("...

Likes

Translate

Translate
Adobe Community Professional ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

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

Likes

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

 

 

Likes

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 Community Professional ,
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?

Likes

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

Copy link to clipboard

Copied

same as these two objects, intersecting returns "true"

intersect.JPG

Likes

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 ,
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.

 

Likes

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 Community Professional ,
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

Likes

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

Copy link to clipboard

Copied

LATEST

I see, the Bounding Boxes do intersect.

Likes

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