Skip to main content
xero
Participant
April 23, 2010
Question

Determining the angle of a line segment.

  • April 23, 2010
  • 1 reply
  • 549 views

I drew a line segment with the pen tool. How can I determine the angle of the segment drawn?

This topic has been closed for replies.

1 reply

Inspiring
April 23, 2010

Something like this...

   measureLine = function(path){
   var res = new Array;
   var lineStart = path.subPathItems[0].pathPoints[0].anchor;
   var lineEnd = path.subPathItems[0].pathPoints[1].anchor;
   var a = Math.max(lineStart[0],lineEnd[0])-Math.min(lineStart[0],lineEnd[0]);
   var o = Math.max(lineStart[1],lineEnd[1])-Math.min(lineStart[1],lineEnd[1]);
   var c = Math.round(Math.sqrt((a*a)+(o*o)));
   res.push(c);
   var ang = (180/Math.PI) * Math.atan2(o,a);
   if(lineStart[1] < lineEnd[1]){//negative angle
      ang = -ang;
   };
   res.push(ang);
   return res;
   }


//the above is for the pen tool. If the path was made with the line tool replace pathPoints[1] with pathPoints[3]

xero
xeroAuthor
Participant
April 26, 2010

Thank you for the script, but where do I insert in photoshop??

Inspiring
April 26, 2010

That was just a function to get the lenght and angle of a path.

Here is an example sscript showing how to call that function. You save the script anywhere as plain text. If you want it to show up in the scripts menu you save it in the app/presets/scripts folder.

Then with a simple line path as the work path you run the script by Choosing FIle-Scripts-nameOfScript if you saveed it in the scripts folder or File-Scripts-Browse if you saved it somewhere else.

var p = app.activeDocument.pathItems.getByName('Work Path');
alert(measureLine(p)[1]);// alert just the angle.
function measureLine(path){
   var res = new Array;
   var lineStart = path.subPathItems[0].pathPoints[0].anchor;
   var lineEnd = path.subPathItems[0].pathPoints[1].anchor;
   var a = Math.max(lineStart[0],lineEnd[0])-Math.min(lineStart[0],lineEnd[0]);
   var o = Math.max(lineStart[1],lineEnd[1])-Math.min(lineStart[1],lineEnd[1]);
   var c = Math.round(Math.sqrt((a*a)+(o*o)));
   res.push(c);
   var ang = (180/Math.PI) * Math.atan2(o,a);
   if(lineStart[1] < lineEnd[1]){//negative angle
      ang = -ang;
   };
   res.push(ang);
   return res;
}