### Tags: angle, cani, endpoints, lines, matlab, problemsuppose, programming, represented, solution

# Angle between two lines

On Programmer » Matlab

6,223 words with 5 Comments; publish: Mon, 19 May 2008 01:38:00 GMT; (200171.88, « »)

Hi everyone,

I am trying to find a quick and easy solution to this problem

suppose I have two lines which are represented by 4 endpoints. How can

I find the angle between two lines.

e.g:

line 1: (x1 y1) , (x2 y2)

line 2: (x3 y3), (x4 y4)

I have one solution is

1. create equations for 2 lines

2 . find the intersection

3. compute the vectors from that intersection

4. angle = atan2(x1*y2-y1*x2,x1*x2+y1*y2)

It seems a lot of steps, any one could show me a quick way to solve

this ?

Thanks a lot,

*http://matlab.todaysummary.com/q_matlab_3258.html*

All Comments

Leave a comment...

- 5 Comments
- phamtrungthanh.matlab.todaysummary.com.gmail.com wrote in message <3da2d87a-37ab-
4e32-8706-efa946b5b5f5.matlab.todaysummary.com.a1g2000hsb.googlegroups.com>...

> Hi everyone,

> I am trying to find a quick and easy solution to this

problem

> suppose I have two lines which are represented by 4

endpoints. How can

> I find the angle between two lines.

> e.g:

> line 1: (x1 y1) , (x2 y2)

> line 2: (x3 y3), (x4 y4)

> I have one solution is

> 1. create equations for 2 lines

> 2 . find the intersection

> 3. compute the vectors from that intersection

> 4. angle = atan2(x1*y2-y1*x2,x1*x2+y1*y2)

> It seems a lot of steps, any one could show me a quick

way to solve

> this ?

> Thanks a lot,

atan((y2-y1)/(x2-x1)) - atan((y4-y3)/(x4-x3))

Maybe you need to worry about signs ...etc. using this.

#1; Mon, 19 May 2008 01:39:00 GMT

- phamtrungthanh.matlab.todaysummary.com.gmail.com wrote in message <3da2d87a-37ab-
- On Mar 20, 9:40 pm, "Amir " <amir_se....matlab.todaysummary.com.hotmail.com> wrote:
> phamtrungth....matlab.todaysummary.com.gmail.com wrote in message <3da2d87a-37ab-

> 4e32-8706-efa946b5b....matlab.todaysummary.com.a1g2000hsb.googlegroups.com>...

>

>

>

> problem

> endpoints. How can

>

>

>

>

> way to solve

>

> atan((y2-y1)/(x2-x1)) - atan((y4-y3)/(x4-x3))

> Maybe you need to worry about signs ...etc. using this.

it works great, thanks Amir

#2; Mon, 19 May 2008 01:40:00 GMT

- On Mar 20, 9:40 pm, "Amir " <amir_se....matlab.todaysummary.com.hotmail.com> wrote:
- phamtrungthanh.matlab.todaysummary.com.gmail.com wrote in message
<3da2d87a-37ab-4e32-8706-

efa946b5b5f5.matlab.todaysummary.com.a1g2000hsb.googlegroups.com>...

> Hi everyone,

> I am trying to find a quick and easy solution to this problem

> suppose I have two lines which are represented by 4 endpoints. How can

> I find the angle between two lines.

> e.g:

> line 1: (x1 y1) , (x2 y2)

> line 2: (x3 y3), (x4 y4)

> I have one solution is

> 1. create equations for 2 lines

> 2 . find the intersection

> 3. compute the vectors from that intersection

> 4. angle = atan2(x1*y2-y1*x2,x1*x2+y1*y2)

> It seems a lot of steps, any one could show me a quick way to solve

> this ?

> Thanks a lot,

--

You can use the cosine law of triangles, but it suffers accuracy loss at ang

les

near 0 or pi radians. With 'atan2' both sine and cosine are used to avoid

this

loss.

angle = atan2(abs((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1)), ...

(x2-x1)*(x4-x3)+(y2-y1)*(y4-y3));

This is the angle between the two vectors p21 = (x2-x1,y2-y1) and p43 =

(x4-x3,y4-y3) placed with their bases at the origin. It will range from 0 t

o pi

in value.

Roger Stafford

#3; Mon, 19 May 2008 01:41:00 GMT

- phamtrungthanh.matlab.todaysummary.com.gmail.com wrote in message
- On Mar 20, 9:50 pm, "Roger Stafford"
<ellieandrogerxy....matlab.todaysummary.com.mindspring.com.invalid> wrote:

> phamtrungth....matlab.todaysummary.com.gmail.com wrote in message

> <3da2d87a-37ab-4e32-8706-

> efa946b5b....matlab.todaysummary.com.a1g2000hsb.googlegroups.com>...

>

>

>

>

>

>

>

> --

> You can use the cosine law of triangles, but it suffers accuracy loss at

angles

> near 0 or pi radians. With 'atan2' both sine and cosine are used to avoi

d this

> loss.

> angle = atan2(abs((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1)), ...

> (x2-x1)*(x4-x3)+(y2-y1)*(y4-y3));

> This is the angle between the two vectors p21 = (x2-x1,y2-y1) and p43 =

> (x4-x3,y4-y3) placed with their bases at the origin. It will range from 0

to pi

> in value.

> Roger Stafford

Interesting, I dont even know it has such a problem, thank you very

much.

#4; Mon, 19 May 2008 01:42:00 GMT

- On Mar 20, 9:50 pm, "Roger Stafford"
- phamtrungthanh.matlab.todaysummary.com.gmail.com wrote in message <8d091bf3-4720-42df-
b410-90bde38683b0.matlab.todaysummary.com.n77g2000hse.googlegroups.com>...

> Interesting, I dont even know it has such a problem, thank you very

> much.

--

Below is a concrete example of the accuracy difficulties one gets into with

small angles using the arccosine function from the cosine law of triangles.

We manufacture a triangle with vertices at A = (0,0), B = (x1,y1), and C =

(x2,y2) having a known small angle BAC.

clear

format long

ang = 1.2345e-6; % Let this be the known small angle

t1 = .789; % Let this be the angle from x-axis to line AB

t2 = t1 + ang; % This will be the angle from x-axis to line AC

a = 3.3; % Make this the length of side AB

b = 1.7; % and this the length of side AC

x1 = a*cos(t1); y1 = a*sin(t1); % Coordinates of B

x2 = b*cos(t2); y2 = b*sin(t2); % Coordinates of C

c = sqrt((x2-x1)^2+(y2-y1)^2); % Length of side BC

% Use cosine law to calculate ang

ang1 = acos((a^2+b^2-c^2)/(2*a*b));

% Use 'atan2' function to calculate ang

ang2 = atan2(abs(x1*y2-y1*x2),x1*x2+y1*y2);

% Compare Results

[ang;ang1;ang2]

ans =

1.0e-05 *

0.12345000000000 <-- Given angle

0.12344602559826 <-- Value calculated by acos

0.12345000000265 <-- Value calculated by atan2

As you see, the arccosine method has an error in the 11th decimal place,

while the 'atan2' method is first off in the 17th place, a very significant

difference in accuracy. The same problem would occur for angles near pi

(180 degrees.) If the acos(x) function is plotted from x = -1 to x = +1, yo

u

will notice that its first derivative becomes infinite at the two endpoints

where

the angle is pi and 0, respectively, and it is this high slope that is respo

nsible

for the difficulty.

Roger Stafford

#5; Mon, 19 May 2008 01:43:00 GMT

- phamtrungthanh.matlab.todaysummary.com.gmail.com wrote in message <8d091bf3-4720-42df-