Re: Measure of the angles of a segmented line

Posted by Michael Schmid on
URL: http://imagej.273.s1.nabble.com/Measure-of-the-angles-of-a-segmented-line-tp5001041p5001375.html

Hi Norbert,

for calculating angles from x & y, usually the best method is the atan2 function. It is not sensitive to rounding errors and takes care of all special cases like 0°, 90°, 180° etc.
This can be also used for the angle between two vectors:

  dotprod = (x[i+1]-x[i])*(x[i-1]-x[i])+(y[i+1]-y[i])*(y[i-1]-y[i]);
  crossprod = (x[i+1]-x[i])*(y[i-1]-y[i])-(y[i+1]-y[i])*(x[i-1]-x[i]);
  angle = (180/PI)*atan2(crossprod, dotprod);

I you don't care about positive or negative angles, take the absolute value, abs(angle).


Michael
________________________________________________________________
On Jan 11, 2013, at 16:04, Norbert Vischer wrote:

> I was using Wayne's macro below and experienced NaN values. This happened on straight lines, when
> the argument of acos() was outside range -1 .. 1 due to rounding errors.
> An improved version is here:
>
> getSelectionCoordinates(x, y);
> for (i=1; i<x.length-1; i++) {
> dotprod = (x[i+1]-x[i])*(x[i-1]-x[i])+(y[i+1]-y[i])*(y[i-1]-y[i]);
> len1 = sqrt((x[i-1]-x[i])*(x[i-1]-x[i])+(y[i-1]-y[i])*(y[i-1]-y[i]));
> len2 = sqrt((x[i+1]-x[i])*(x[i+1]-x[i])+(y[i+1]-y[i])*(y[i+1]-y[i]));
> val = minOf(1, dotprod/(len1*len2));
> val = maxOf(-1, val);
> angle = (180/PI)*acos(val);
> print(i, angle);
> }
>
> Norbert Vischer
>
>
>> On 5. Dec 2012, at 19:11, Rasband, Wayne (NIH/NIMH) [E] wrote:
>>
>> On Dec 5, 2012, at 8:23 AM, Eric Denarier wrote:
>>
>>> Hi all,
>>> Is there a macro/plugin to measure all the angles formed by a segmented
>>> line selection ?
>>
>> Here is a macro that does this:
>>
>> getSelectionCoordinates(x, y);
>> for (i=1; i<x.length-1; i++) {
>>   dotprod = (x[i+1]-x[i])*(x[i-1]-x[i])+(y[i+1]-y[i])*(y[i-1]-y[i]);
>>   len1 = sqrt((x[i-1]-x[i])*(x[i-1]-x[i])+(y[i-1]-y[i])*(y[i-1]-y[i]));
>>   len2 = sqrt((x[i+1]-x[i])*(x[i+1]-x[i])+(y[i+1]-y[i])*(y[i+1]-y[i]));
>>   angle = (180/PI)*acos(dotprod/(len1*len2));
>>   print(i, angle);
>> }
>>
>> I found the code for calculating an angle given 3 points using the dot product at
>>
>>  http://stackoverflow.com/questions/6719563/
>>
>> -wayne
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html