Login  Register

Re: Measure of the angles of a segmented line

Posted by vischer on Jan 11, 2013; 3:04pm
URL: http://imagej.273.s1.nabble.com/Measure-of-the-angles-of-a-segmented-line-tp5001041p5001372.html

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