# Geometric test functions

Enthusiast ,
May 30, 2021 May 30, 2021

Copied

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

``````var intersects = function() {
if (app.selection.length == 1) {
return false;
}
var a = app.selection.length;
app.redraw();
app.undo();
return a == 1;
}

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

``````var contains = function() {
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
}

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) {
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.

TOPICS
Scripting

Views

408

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

7 Replies 7
May 30, 2021 May 30, 2021

Copied

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
May 31, 2021 May 31, 2021

Copied

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

By @CarlosCanto

That sounds better.

``````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;
var a = app.selection.length;
for (var i = app.selection.length - 1; i > -1; i--) {
app.selection[i].remove();
}
return a == 1;
}

``````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;
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;
}

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 31, 2021 May 31, 2021

Copied

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 31, 2021 May 31, 2021

Copied

same as these two objects, intersecting returns "true"

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
May 31, 2021 May 31, 2021

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

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 01, 2021 Jun 01, 2021

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

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 01, 2021 Jun 01, 2021

Copied

LATEST

I see, the Bounding Boxes do intersect.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources
Learning Resources for Illustrator
What's new and fixed in Illustrator
Fonts and Typography in Illustrator