Skip to content

Instantly share code, notes, and snippets.

@dscho dscho/gist:2990484
Created Jun 25, 2012

Embed
What would you like to do?
// 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
You can’t perform that action at this time.