Copy link to clipboard
Copied
Hello all,
I am looking for the command for javascript in Illustrator to query the position (x,y) of the midpoint (A)(transformation point, pivot point) of a PathItem.
The calculation of the centre of the boundbox (B) logically returns the wrong point.
I could not find any information about (A) in the documentation or on the web.
I look forward to your answer and help,
Jens.
Hi Jens, here's a mathematical way. This is only for a triangle. You might be able to adapt for other polygons. - Mark
/*
Find the center of a triangle
by m1b,
with big help from: https://www.geeksforgeeks.org/program-to-find-the-incenter-of-a-triangle/
*/
var centerPoint = centerOfTrianglePathItem(app.activeDocument.selection[0]);
$.writeln('centerPoint = ' + centerPoint);
function centerOfTrianglePathItem(item) {
return calculateInCenter(triangleFromPathItem(item));
}
fu
...
1. Illustrator knows the point (A), so there should by a way to get the position.
By @Parts4Arts
Illustrator does know point (A), but it does not expose that property (among a million other metrics) to developers.
Copy link to clipboard
Copied
Hi Jens, here's a mathematical way. This is only for a triangle. You might be able to adapt for other polygons. - Mark
/*
Find the center of a triangle
by m1b,
with big help from: https://www.geeksforgeeks.org/program-to-find-the-incenter-of-a-triangle/
*/
var centerPoint = centerOfTrianglePathItem(app.activeDocument.selection[0]);
$.writeln('centerPoint = ' + centerPoint);
function centerOfTrianglePathItem(item) {
return calculateInCenter(triangleFromPathItem(item));
}
function triangleFromPathItem(item) {
var triangle = {},
pts = item.pathPoints;
if (pts.length != 3) return; // not a triangle
triangle.points = [pts[0].anchor, pts[1].anchor, pts[2].anchor];
triangle.sideLengths = [
distanceBetweenPoints(triangle.points[0], triangle.points[1]),
distanceBetweenPoints(triangle.points[1], triangle.points[2]),
distanceBetweenPoints(triangle.points[2], triangle.points[0])
];
return triangle;
}
function distanceBetweenPoints(p1, p2) {
var a = p1[0] - p2[0];
var b = p1[1] - p2[1];
return Math.sqrt(a * a + b * b);
}
function calculateInCenter(triangle) {
var x1 = triangle.points[0][0],
y1 = triangle.points[0][1],
x2 = triangle.points[1][0],
y2 = triangle.points[1][1],
x3 = triangle.points[2][0],
y3 = triangle.points[2][1],
a = triangle.sideLengths[1],
b = triangle.sideLengths[2],
c = triangle.sideLengths[0];
// Formula to calculate in-center
var x = (a * x1 + b * x2 + c * x3) / (a + b + c);
var y = (a * y1 + b * y2 + c * y3) / (a + b + c);
return [x, y];
}
Copy link to clipboard
Copied
Hi Mark,
thanks for the answer. But it's not the way I like.
1. Illustrator knows the point (A), so there should by a way to get the position.
2. The triangle is a simple example. It costs time to calculate for each polygon the center point.
Best regards, Jens
Copy link to clipboard
Copied
I've not tried it of late, but this is a script which aims to find the centroids of polygons.
Finding the Center of Gravity (Centroid) of a Polygon in Adobe Illustrator (gritlab.org)
Copy link to clipboard
Copied
Thanks for the link. – jens.
Copy link to clipboard
Copied
Scott Swaaley's code mentioned by @femkeblanco is nice! I'd recommend adapting from his code rather than mine. It does handle arbitrary polygons, but works with regular polygons, too, of course.
- Mark
Copy link to clipboard
Copied
1. Illustrator knows the point (A), so there should by a way to get the position.
By @Parts4Arts
Illustrator does know point (A), but it does not expose that property (among a million other metrics) to developers.