## Orthogonal Graphic Lines

I was called upon the other day to write a script that required that the selection be an orthogonal graphic line. That is, one that was either perfectly horizontal or perfectly vertical.

A graphic line is not necessarily straight. It appears to be any object that has exactly two points, no matter how it was drawn. For example, anything drawn with the Line Tool is a graphic line; any two-point path drawn with the Pen Tool is a graphic line; any ploygon or rectangle that has been reduced to two points by deleting points from its original shape is a graphic line.

This means that finding an "orthogonal" line is not as simple a looking at the coordinates of a graphic line and checking to see if the two x-values or two y-values are the same as each other.

Here's a framework:

//DESCRIPTION: Framework for operating on selected orthogonal lines
(function() {
if (app.documents.length > 0) {
for (var j = app.documents[0].selection.length - 1; j >= 0; j--) {
if (app.documents[0].selection[j] instanceof GraphicLine &&
isOrthogonal(app.documents[0].selection[j])) {
processOrthogLine(app.documents[0].selection[j]);
}
}
}

function processOrthogLine(theLine) {
var delta = 0.00001;
var vert = Math.abs(theLine.paths[0].entirePath[0][1] - theLine.paths[0].entirePath[1][1]) > delta
"The selected line is " + (vert ? "vertical." : "horizontal."));
}

function isOrthogonal(gl) {
// By definition, a graphicLine has just one path with two pathPoints
var myPath = gl.paths[0];

var entirePath = myPath.entirePath;

if (entirePath[0].length > 2 || entirePath[1].length > 2) {
// one or other of the path points has control handles
// thus, the line is not straight
return false;
}

var delta = 0.00001;

if (Math.abs(entirePath[0][0] - entirePath[1][0]) < delta ||
Math.abs(entirePath[0][1] - entirePath[1][1]) < delta) {

return true;
}

return false;
}
}())

It is possible that lines drawn with carefully positioned control handles could result in a straight line, but we make the assumption that the lines we're looking for have no control handles.