From: Beho nashed on
Nathan <ngreco32(a)gmail.com> wrote in message <8db784bf-a96e-431b-b5c6-d6836905d350(a)o16g2000prh.googlegroups.com>...
> On Feb 23, 4:41 pm, "Benjamin McCrite" <dragonmast...(a)hotmail.com>
> wrote:
> >  Greg Heath <he...(a)alumni.brown.edu> wrote in message <1184052626.324470.175...(a)n2g2000hse.googlegroups.com>...

Hello Nathan,
I am trying to calculate the angle between two lines, I found this topic, and I have a quick question.
Here's my code:
I = imread('angle.jpg');
figure, imshow(I);
[xp, yp] = ginput;
line(xp,yp,'color',[0 1 0]);
cal_angle = atan2(norm(cross(xp,yp)),dot(xp,yp))
cal_angle2 = acosd(dot(xp,yp)/(norm(xp)*norm(yp)))

I found those two equations online, but I don't think they're right. the first one should be right, I remember it from calculus!

I select three points, get two lines. the angle should be greater than 90, but it always comes smaller than 90!!!
I am not sure, what am I doing wrong here?

Thanks
From: Roger Stafford on
"Beho nashed" <beho86(a)yahoo.com> wrote in message <hvbni8$s0h$1(a)fred.mathworks.com>...
> I am trying to calculate the angle between two lines, I found this topic, and I have a quick question.
> Here's my code:
> I = imread('angle.jpg');
> figure, imshow(I);
> [xp, yp] = ginput;
> line(xp,yp,'color',[0 1 0]);
> cal_angle = atan2(norm(cross(xp,yp)),dot(xp,yp))
> cal_angle2 = acosd(dot(xp,yp)/(norm(xp)*norm(yp)))
>
> I found those two equations online, but I don't think they're right. the first one should be right, I remember it from calculus!
>
> I select three points, get two lines. the angle should be greater than 90, but it always comes smaller than 90!!!
> I am not sure, what am I doing wrong here?
>
> Thanks
- - - - - - - - - -
Based on your statement, "I select three points", it looks as though you are entering three quantities each for xp and yp. Unfortunately the 'cross' function which requires vectors with three components interprets each of these as a three-dimensional space vector and not three points in two-dimensional space. The same holds for 'dot' in this case. That means both your angles are wrong.

If you have three points P1 = (x1,y1), P2 = (x2,y2), and P3 = (x3,y3) in two dimensional space, the inner angle at P1 between lines P2P1 and P3P1 is given by

atan2(abs(det([x1,x2,x3;y1,y2,y3;1,1,1])),(x2-x1)*(x3-x1)+(y2-y1)*(y3-y1))

The angle from atan2 is given in radians.

Roger Stafford
From: Beho nashed on
Hello Roger,
Thanks, so to use this equation, I should change my code to :
[x,y] = ginput;

angle = atan2(abs(det([x(1),x(2),x(3);y(1),y(2),y(3);1,1,1])),(x(2)-x(1))*(x(3)-x(1))+(y(2)-y(1))*(y(3)-y(1)))

I am not sure, I meant x(1) so it will use first value in array x, am I right?

Thanks,
From: Roger Stafford on
"Beho nashed" <beho86(a)yahoo.com> wrote in message <hvbs37$a30$1(a)fred.mathworks.com>...
> Hello Roger,
> Thanks, so to use this equation, I should change my code to :
> [x,y] = ginput;
>
> angle = atan2(abs(det([x(1),x(2),x(3);y(1),y(2),y(3);1,1,1])),(x(2)-x(1))*(x(3)-x(1))+(y(2)-y(1))*(y(3)-y(1)))
>
> I am not sure, I meant x(1) so it will use first value in array x, am I right?
>
> Thanks,
- - - - - - - - -
Yes, that looks right if you make sure to enter the vertex of the angle you are finding first so that it goes into P1 = [x(1),y(1)], then followed by the other two points on the two respective lines.

For brevity you can write

P1 = [x(1),y(1)]; % The point at the angle vertex
P2 = [x(2),y(2)];
P3 = [x(3),y(3)];

ang = atan2(abs(det(P2-P1;P3-P1])),dot(P2-P1,P3-P1));

It will give you the same answer (in radians.)

Don't use the word 'angle' for your angle variable. It is a reserved word in matlab and the confusion could cause errors in your program when you least expect it.

Roger Stafford
From: Beho nashed on
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hvc2qe$59b$1(a)fred.mathworks.com>...
> "Beho nashed" <beho86(a)yahoo.com> wrote in message <hvbs37$a30$1(a)fred.mathworks.com>...
> > Hello Roger,
> > Thanks, so to use this equation, I should change my code to :
> > [x,y] = ginput;
> >
> > angle = atan2(abs(det([x(1),x(2),x(3);y(1),y(2),y(3);1,1,1])),(x(2)-x(1))*(x(3)-x(1))+(y(2)-y(1))*(y(3)-y(1)))
> >
> > I am not sure, I meant x(1) so it will use first value in array x, am I right?
> >
> > Thanks,
> - - - - - - - - -

Hello Roger,
I keep selecting points for an angle bigger than 90 degree, but the program is giving me an angle less than 90 deg!!!

http://drop.io/rlv072i (image 3, called test)
I pick three points, two points on the surface (on the left side) and the third on the slope ( the white one), making two lines with an angle bigger than 90 deg!

Thanks in advance Roger