Skip to main content
femkeblanco
Legend
May 30, 2021
Answered

Geometric test functions

  • May 30, 2021
  • 1 reply
  • 1174 views

 

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. 

This topic has been closed for replies.
Correct answer femkeblanco
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() );

 

 

1 reply

CarlosCanto
Community Expert
Community Expert
May 31, 2021

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

femkeblanco
femkeblancoAuthorCorrect answer
Legend
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("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() );

 

 

CarlosCanto
Community Expert
Community Expert
June 1, 2021

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