Skip to content

Instantly share code, notes, and snippets.

@dscho
Created June 25, 2012 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dscho/2990484 to your computer and use it in GitHub Desktop.
Save dscho/2990484 to your computer and use it in GitHub Desktop.
// take a list and return a newArray(...) command
function join(list) {
result = "newArray(";
for (i = 0; i < list.length; i++) {
if (i > 0)
result = result + ", ";
result = result + list[i];
}
result = result + ")";
return result;
}
function distance(x1, y1, x2, y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
function distance2segment(x, y, x1, y1, x2, y2) {
return distance(x, y, x1, y1) + distance(x, y, x2, y2);
}
function closestSegment(x, y, xList, yList) {
best = -1;
bestDistance = 9999999; // really large
for (i = 0; i < xList.length - 1; i++) {
distance = distace2segment(x, y, xList[i], yList[i], xList[i + 1], yList[i + 1]);
if (bestDistance > distance) {
bestDistance = distance;
best = i;
}
}
return best;
}
getSelectionCoordinates(x, y);
command = "makeSelection(" + selectionType + ", "
+ join(x) + ", "
+ join(y) + ");";
print(command);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment