Find Angle Between Two Line

How i want to find the angle between two line.. Example my code:
%%%Angle Between Two Line%%%
% Line 1: point(0,3) to (4,3)
% Line 2: point(0,2) to (3,0)
v1=[4,3]-[0,3];
v2=[0,2]-[3,0];
angle = mod( atan2( det([v1,v2]) , dot(v1,v2) ) , 2*pi );
what i get was
Error using ==> det
Matrix must be square..
Why I cannot get the angle.???

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2012년 5월 18일

1 개 추천

v1=[4,3]-[0,3];
v2=[0,2]-[3,0];
angle = mod( atan2( det([v1;v2]) , dot(v1,v2) ) , 2*pi );
EDIT
a1 = mod(atan2( det([v1;v2;]) , dot(v1,v2) ), 2*pi );
angleout = abs((a1>pi/2)*pi-a1)

댓글 수: 3

mnhafizuddin
mnhafizuddin 2012년 5월 18일
thank for your answer..
but why i calculate manually the angle and not get the exactly answer like matlab..???
It is my coding is wrong.???
Jan
Jan 2012년 5월 18일
편집: Jan 2012년 12월 22일
@mnhafizuddin: I don't understand the question. The problem of your code is the comma in "[v1,v2]", bcause it creates a [1 x 4] vector, but DET needs a [2x2] matrix. See some of the most popular thread in CSSM: http://www.mathworks.com/matlabcentral/newsreader/view_thread/151925#870338
mnhafizuddin
mnhafizuddin 2012년 5월 19일
thanks for your suggestion website..
It's really helps me alot..
thanks!!!

댓글을 달려면 로그인하십시오.

Geoff
Geoff 2012년 5월 18일

0 개 추천

When I want to find the angle between two vectors, I take the dot product of the unit vectors:
clamp = @(val, low, high) min(max(val,low), high);
angle = acos( clamp(dot(v1,v2) / norm(v1) / norm(v2), -1, 1);
If you want to know which direction that's in, you can do a trick with the cross-product. Since it's 2D, you do this:
vc = cross([v1,0], [v2,0]);
anticlock = vc(3) > 0;

댓글 수: 3

mnhafizuddin
mnhafizuddin 2012년 5월 18일
what is the function clamp for.???
Jan
Jan 2012년 5월 18일
ACOS(DOT(u,v)) is instable if u and v are near to (anti)-parallel. ASIN(CROSS()) is not sufficient near to perpendicular vectors. Therefore the ATAN2 approach is recommended.
Geoff
Geoff 2012년 5월 21일
Oh righto. I never had issues with stability. But that's what the clamp is for. It prevents the value from exceeding 1 (or -1) due to floating-point precision errors.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2012년 5월 18일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by