• Javascript how to get the position of the center p...

# Javascript how to get the position of the center point?

Engaged ,
Dec 10, 2021 Dec 10, 2021

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.

Jens.

ā

TOPICS
Scripting , SDK

Views

173

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

Adobe Community Professional , Dec 10, 2021 Dec 10, 2021
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...

Likes

Adobe Community Professional , Dec 10, 2021 Dec 10, 2021
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.

Likes

6 Replies 6
Dec 10, 2021 Dec 10, 2021

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

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
Engaged ,
Dec 10, 2021 Dec 10, 2021

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

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
Dec 10, 2021 Dec 10, 2021

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)

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
Engaged ,
Dec 10, 2021 Dec 10, 2021

Copied

Thanks for the link. ā jens.

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
Dec 13, 2021 Dec 13, 2021

Copied

LATEST

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

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
Dec 10, 2021 Dec 10, 2021

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.

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
Crash on launch and known issues
Fonts and Typography in Illustrator