Hey guys,
Is there a quicker way to find the angle of a graphic line other than using trigonometry?
Background: If I create a new line using add(), this line needs to get a gradient from start point to end point (these are given and used to create the line in the first pace). In order to use the right angle in the gradient settings, I need to know the angle of the graphic line. If I would have to use trigonometry, I would use geometricBounds to find out the height/width of the bounding box, find out the start and end points of my line (which I have) and with that determine in which quadrant the line goes through to use the right formula (sin/cos/tan) and finally convert from radiant to degree. Phew, so if there is any way how to just read the angle of the line, tat would be cool.
Thanks,
Frank
Hi Frank,
I'll start by saying I don't know if there is a method to do this, I couldn't find one in the API. The thing is if you physically draw a line in InDesign the angle in the interface is always zero. If you rotate the line by a number of degrees it will be that number in the display.
However, the math to actually find the angle of a line is pretty easy and fun.
var myLine = app.activeDocument.graphicLines.item(0).paths.item(0).entirePath; var a = myLine[0][0] - myLine[1][0]; var b = myLine[1][1] - myLine[0][1]; var angleDeg = Math.atan2(a, b) * 180 / Math.PI; $.writeln(angleDeg);
If you use this code it will calculate the actual angle between the 2 points of the line where the direction of the line is:
down 0 degrees
left 90 degrees
up 180 degrees
right -90 degrees
Brett
Hey Brett,
Awesome!
Yes, I was using a similar calculation, however, the only thing I didn't consider (which makes the whole thing much easier) is to use "entirePath" and let ID determine start and end points automatically.
Very helpful, thanks!
Frank