Skip to content

Instantly share code, notes, and snippets.

@howyay
Forked from scottswaaley/CentroidFinder.js
Created June 20, 2020 06:49
Show Gist options
  • Save howyay/f40a71271dbf02a6d55852e16bc2e3b1 to your computer and use it in GitHub Desktop.
Save howyay/f40a71271dbf02a6d55852e16bc2e3b1 to your computer and use it in GitHub Desktop.
/* Center of Gravity Finder
This script will find the center of gravity (centroid) of all closed paths in the selection set. It was built for a specific purpose so does not have much error
handling. For example, if you get errors it may be the result of the selection set containing compound shapes or a self-intersecting polygon.
References for the math:
http://paulbourke.net/geometry/polygonmesh/ (Calculating the area and centroid of a polygon)
https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
Save this file with a jsx extension and place in your Illustrator/Presets/en_US/Scripts folder. You can then access it from the File > Scripts menu
Made by: Scott Swaaley, April 2, 2017
*/
var holeDiameter = 0.125 * 72; // in points
if (app.documents.length > 0 && app.activeDocument.selection.length > 0) {
//initialize log file
var reportFile =new File(app.activeDocument.path+'/'+'AI_SCRIPT_LOG.txt');
reportFile.open('w'); // open with write access
reportFile.writeln(new Date().toLocaleString());
for(var selectionIndex = 0; selectionIndex < app.activeDocument.selection.length; selectionIndex++) {
var object = app.activeDocument.selection[selectionIndex];
reportFile.writeln("Selection Index:" + selectionIndex);
// copy anchor points into array
// The equations used below require that " the vertex ( xn, yn ) is assumed to be the
// same as ( x0, y0 ), meaning i + 1 on the last case must loop around to i = 0."[https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon]
var arrayOfAnchors = new Array();
for(var i = 0; i < object.pathPoints.length;i++) arrayOfAnchors.push(object.pathPoints[i].anchor); // fill array with shape's anchor point coordinates
arrayOfAnchors.push(arrayOfAnchors[0]);
reportFile.writeln("Number of Anchor Points (exclusive):" + object.pathPoints.length);
var shapeArea = 0;
var Cx = 0;
var Cy = 0;
for(var i = 0; i < arrayOfAnchors.length-1;i++) {
shapeArea += (arrayOfAnchors[i][0]*arrayOfAnchors[i+1][1] - arrayOfAnchors[i+1][0]*arrayOfAnchors[i][1]);
Cx += (arrayOfAnchors[i][0]+arrayOfAnchors[i+1][0]) * (arrayOfAnchors[i][0]*arrayOfAnchors[i+1][1]-arrayOfAnchors[i+1][0]*arrayOfAnchors[i][1]);
Cy += (arrayOfAnchors[i][1]+arrayOfAnchors[i+1][1]) * (arrayOfAnchors[i][0]*arrayOfAnchors[i+1][1]-arrayOfAnchors[i+1][0]*arrayOfAnchors[i][1]);
}
// performing final steps of calculation [https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon]
shapeArea = 0.5*shapeArea;
Cx = Cx / (6*shapeArea);
Cy = Cy / (6*shapeArea);
//print results
reportFile.writeln("Shape Area by New Script:" + shapeArea + " square points");
reportFile.writeln("Center of Gravity (in points) at: " + Cx + "," + Cy);
//alert(object.typename);
for(var i = 0; i<arrayOfAnchors.length;i++) {
reportFile.writeln(arrayOfAnchors[i]);
//note += object.pathPoints[i].anchor; + '\n'
}
var newGroup = app.activeDocument.groupItems.add();
object.move(newGroup,ElementPlacement.INSIDE);
newGroup.pathItems.ellipse(Cy + holeDiameter/2,Cx - holeDiameter/2, holeDiameter, holeDiameter, false, true);
newGroup.selected=true; // ensures that entirety of new group is selected
reportFile.writeln("- - - - - - - - - - - - -");
}
} else alert('Select a path first');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment