# Find Angle Between Two Line

2 views (last 30 days)

Show older comments

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.???

##### 0 Comments

### Answers (2)

Andrei Bobrov
on 18 May 2012

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 Comments

Jan
on 18 May 2012

Edited: Jan
on 22 Dec 2012

Geoff
on 18 May 2012

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 Comments

Jan
on 18 May 2012

Geoff
on 21 May 2012

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!