Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫  π  -¹ ² ³ °

You are not logged in.

## #1 2014-01-16 13:58:26

pari_alf
Member
Registered: 2013-12-01
Posts: 85

### angle between points of a line

Hi all,

I have some points as shown below.

@ 0(105, 66)
@ 1(114, 92)
@ 2(104, 11)
@ 3(99, 131)
@ 4(71, 144)
@ 5(37, 168)

Problem statement:

I have to find angle between points of line.

Note: The 0th index and the 5th index point are the start and end point of string.

I calculated angle using atan2 (dy, dx) and my angles are show as below..

angle : 70.9065
angle : -97.0379
angle : 92.3859
angle : 155.095
angle : 144.782

The issue is that the direction of 1st index point to the 2nd index point is upward (down to going up), but my angle is computed in downward direction .. why?

The upward direction is inverted ..why?

Pls guide me to fix this issue .

thanks

Last edited by pari_alf (2014-01-16 14:01:28)

Offline

## #2 2014-01-16 21:22:20

bob bundy
Administrator
Registered: 2010-06-20
Posts: 8,155

### Re: angle between points of a line

hi pari_alf

Only just woken up to your problem here in the UK.

I've made a graph to show your points but I'm unclear about what angles you are measuring.  The angle I've marked with an x is 40.73 which isn't any of your answers.

Using atan(dy,dx) will give different results depending on which point you start with and which next in calculating the differences dy and dx.  It may be that you have reversed the order.

You'll have to show the actual calculation if you want me to investigate further.

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #3 2014-01-16 22:27:30

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,593

### Re: angle between points of a line

Hi Bob,

I got pari_alf's angles with Geogebra (see image) - except for the negative for point B - but this is as far as my knowledge on this sort of stuff goes.

So, over to you and pari_alf...but I hope my drawing will help.

I had to rename the points because Geogebra won't accept numeric names.

0 = A
1 = B
2 = C
3 = D
4 = E
5 = F

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

## #4 2014-01-16 23:24:37

bob bundy
Administrator
Registered: 2010-06-20
Posts: 8,155

### Re: angle between points of a line

hi Phro,

Thanks for the diagram.  That answers my question; the angles are for 0 to 1, 1 to 2, 2 to 3 and so on.

hi pari_alf

The issue is that the direction of 1st index point to the 2nd index point is upward (down to going up), but my angle is computed in downward direction .. why?

The upward direction is inverted ..why?

But it is going down.  Look at Phro's diagram.

So what is the exact calculation you are doing?  Somehow your calculator is getting this result.

Inverse trig functions often need some user input.  This is because the basic trig functions are all multi-valued (eg. tan 45 = tan 225 = tan 405 ...)

So when the designers are constructing the software for atan they have to second guess which angle you want and you have to adjust the answer if it's not the answer you were expecting.

eg. I had drawn a concave polygon and wanted the internal angles.  One was obviously reflex but my software gave an acute angle.  I had to subtract from 360 to get the 'right' result.

http://www.mathisfunforum.com/viewtopic … 34#p249834

See picture for post 2.

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #5 2014-01-17 02:45:25

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,593

### Re: angle between points of a line

Sorry...I got the angle wrong for the 1st index point (point B in my drawing).

When obtaining the angle values in Geogebra I measured in the opposite direction with this one compared to the direction in which I measured all the others, and it gave the angle to the wrong side of the point.

Here's the corrected drawing:

Last edited by phrontister (2014-01-17 14:12:22)

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

## #6 2014-01-17 19:07:22

pari_alf
Member
Registered: 2013-12-01
Posts: 85

### Re: angle between points of a line

Hi guys, thanks for replying.

Kindly see my points shown on image below, where the green and red circles are the start and end point and all other other intermediate points are in black circle.

I computed angle using atan2 (dy,dx),
Problem is that the direction from 1st index point to the 2nd index point is shown  vertically in the attached image. but my computed angle shows the direction in downward direction.

Computed angles are shown below..

Original Points:
@ 0(105, 66)
@ 1(114, 92)
@ 2(104, 11)
@ 3(99, 131)
@ 4(71, 144)
@ 5(37, 168)

angle : 70.9065
angle : -97.0379
angle : 92.3859
angle : 155.095
angle : 144.782

Why my angle vertical angle is inverted to downward direction using atan2(dy,dx) ?

Last edited by pari_alf (2014-01-17 19:18:38)

Offline

## #7 2014-01-17 19:31:57

pari_alf
Member
Registered: 2013-12-01
Posts: 85

### Re: angle between points of a line

pari_alf wrote:

Hi guys, thanks for replying.

Kindly see my points shown on image below, where the green and red circles are the start and end point and all other other intermediate points are in black circle.

http://i.imgur.com/XLB0JH6.png

I computed angle using atan2 (dy,dx),
Problem is that the direction from 1st index point to the 2nd index point is shown  vertically in the attached image. but my computed angle shows the direction in downward direction.

Computed angles are shown below..

Original Points:
@ 0(105, 66)
@ 1(114, 92)
@ 2(104, 11)
@ 3(99, 131)
@ 4(71, 144)
@ 5(37, 168)

angle : 70.9065
angle : -97.0379
angle : 92.3859
angle : 155.095
angle : 144.782

Why my angle vertical angle is inverted to downward direction using atan2(dy,dx) ?

Hi, @phrontister

I saw your last figure that seems ok. fine .
but if you see my figure, the angle from 1st index to 2nd index point is -97 which meant that my line is going in downward direction. but my line is actually going upward.
How can i make my angle correct?

Offline

## #8 2014-01-17 21:12:31

bob bundy
Administrator
Registered: 2010-06-20
Posts: 8,155

### Re: angle between points of a line

hi pari_alf

I still am unable to answer that without knowing how you computed the angles.

Are you using a calculator? In which case, which one?

Or some software? In which case what software?

How have you determined dy and dx?  Please post the actual calculation you did.

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #9 2014-01-18 00:00:06

pari_alf
Member
Registered: 2013-12-01
Posts: 85

### Re: angle between points of a line

I calculated the angle by making a program in c++.

The peusdo code is something like this

for: i 1 to n-1  //n is the size of vector points

dx = points [i-1].x - points[i].x
dy = pointsp[i-1].y - points[i].y

angle = atan2 (dy , dx ) * 180/pi

This is the way i am coding to compute the angle

Offline

## #10 2014-01-18 00:31:45

bob bundy
Administrator
Registered: 2010-06-20
Posts: 8,155

### Re: angle between points of a line

hi pari_alf

I tried your code in excel and got these values:

70.906508
82.96205924
-87.61405597
-24.90476881
-35.21759297

This is what I would expect because these are the principle values for the function atan.

I can convert my answers into yours by adding or subtracting 180 as necessary.

According to

http://www.cplusplus.com/reference/cmath/atan/

Principal arc tangent of x, in the interval [-pi/2,+pi/2] radians.
One radian is equivalent to 180/PI degrees.

But curiously on this page

http://www.cplusplus.com/reference/cmath/atan2/

Principal arc tangent of y/x, in the interval [-pi,+pi] radians.
One radian is equivalent to 180/PI degrees.

This must be an error as atan has two possible values in this range.  ???

To use atan you have to compute the division (dy/dx) and then apply the function to the result.

It would be interesting to see what answers you get if you try this.

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #11 2014-01-18 00:55:49

pari_alf
Member
Registered: 2013-12-01
Posts: 85

### Re: angle between points of a line

bob bundy wrote:

hi pari_alf

I tried your code in excel and got these values:

70.906508
82.96205924
-87.61405597
-24.90476881
-35.21759297

This is what I would expect because these are the principle values for the function atan.

I can convert my answers into yours by adding or subtracting 180 as necessary.

According to

http://www.cplusplus.com/reference/cmath/atan/

Principal arc tangent of x, in the interval [-pi/2,+pi/2] radians.
One radian is equivalent to 180/PI degrees.

But curiously on this page

http://www.cplusplus.com/reference/cmath/atan2/

Principal arc tangent of y/x, in the interval [-pi,+pi] radians.
One radian is equivalent to 180/PI degrees.

This must be an error as atan has two possible values in this range.  ???

To use atan you have to compute the division (dy/dx) and then apply the function to the result.

It would be interesting to see what answers you get if you try this.

Bob

Yes these are my angle using atan(dy/dx)

70.9065
82.9621
-87.6141
-24.9048
-35.2176

but the angle from index 1 to index 2, the angle is wrong using atan2 () method

Last edited by pari_alf (2014-01-18 00:57:44)

Offline

## #12 2014-01-18 02:14:27

bob bundy
Administrator
Registered: 2010-06-20
Posts: 8,155

### Re: angle between points of a line

I'm not that familiar with C++ to know exactly how the software actually computes the angles.  I've posted in Coders Corner in the hope that someone does.

In the meantime, how much does it matter?  If you are just curious, then we can wait for a programmers response.

If this is holding up the completion of a project, then let's try to find a work-around.  What will you do with the angles now you have found them?

LATER EDIT:

According to Microsoft (http://msdn.microsoft.com/en-us/library/88c36t42.aspx

atan2 uses the signs of both parameters to determine the quadrant of the return value.

So let's get some more evidence.  In the image below I've made up some values to cover all four quadrants with the principle value results for atan.  With those values for dy and dx what does C++ do?

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #13 2014-01-18 02:19:11

pari_alf
Member
Registered: 2013-12-01
Posts: 85

### Re: angle between points of a line

Actually, i am trying to find the direction of each line using angle computed by atan2 (dy, dx).

To find the direction of each line, i did something like this..

if ( angleInDegree > 0 )
directionCode = abs ((angleInDegree / 22.5) - 16 );
print --> directionCode

else
directionCode = abs ((angleInDegree / 22.5) + 16);
print --> directionCode
-------------------------------------------

My Result with angle and direction codes are like this

Original Points:
@ 0(105, 66)
@ 1(114, 92)
@ 2(104, 11)
@ 3(99, 131)
@ 4(71, 144)
@ 5(37, 168)

angle : 70.9065
angle : -97.0379
angle : 92.3859
angle : 155.095
angle : 144.782

Print the directional codes..
@ - 0:12
@ - 1:11
@ - 2:11
@ - 3:9
@ - 4:9

you guys can note the the angle of 1st index to the 2nd index point is -97 that meant in downward direction.

It should be at upward direction i meant angle should be arround 90.
and direction code should be 3 or 4.

Why my angle of point index 1 to index2, is inverted.

Last edited by pari_alf (2014-01-18 03:53:48)

Offline

## #14 2014-01-18 05:32:44

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,593

### Re: angle between points of a line

Hi Bob,

I tried your code in excel and got these values:

70.906508
82.96205924
-87.61405597
-24.90476881
-35.21759297

ATAN2 in Excel is different from most other programs by having switched x and y: ie, ATAN2(x,y) = ATAN(y/x).

I allowed for that in my Excel calcs, with the following results:

70.906508
-97.03794076
92.38594403
155.0952312
144.782407

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

## #15 2014-01-18 05:46:09

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,593

### Re: angle between points of a line

Hi pari_alf

Having thought about this some more I now can't see anything wrong with the Index 1/Index 2 angle measurement of -97.0379°, because:
- the negative sign indicates that the angle is measured in a clockwise direction from Index 1's  x-axis; and
- the clockwise measuring direction combined with the angle value places the sloping line from Index 1 to Index 2 downward at 7.0379° left of vertical.

Something odd about your drawing in post #6: it is an exact vertical flip of mine in post #5. I checked that in Geogebra, and could only replicate your drawing by entering negative y values (see image).

Last edited by phrontister (2014-01-19 02:05:16)

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

## #16 2014-01-18 08:31:52

bob bundy
Administrator
Registered: 2010-06-20
Posts: 8,155

### Re: angle between points of a line

hi pari_alf

I do not have a C++ compiler so I cannot check this myself.  But you can.  Please let me know.

In the diagram below I have taken the y direction as upwards as is usual.

C++ has a basic function atan which gives the inverse of the tan function.  Let's assume there is a machine code level calculation that works out atan values (possibly a Taylor series but it does not matter how the values are worked out).

Because many angles give the same tan, the inverse would be multi-valued.  So the software programmers have made a single valued function by always giving the principle value ... meaning a value between  minus pi/2 and plus pi/2.  Most calculators work this way.

Let alpha be an acute angle measured anti-clockwise from the x axis.  Let a and b be positive numbers.

Then atan(b/a) = atan(-b/-a) so a line in the first quadrant and a line in the third quadrant will give the same angle.

Similarly, lines in the second and fourth quadrant will give the same angle.

So the software programmers have made a second function, atan2, which works out the same angle but applies the following correction, depending on the sign of dx and dy.

(dx,dy) = (a,b) then if atan(b/a) = alpha return the value of alpha.

(dx,dy) = (-a,b) then if atan(b/a) = alpha return the value of 180 - alpha, ie. give a second quadrant angle.

(dx,dy) = (-a,-b) then if atan(b/a) = alpha return the value of alpha - 180, ie. give a third quadrant angle.

(dx,dy) = (a,-b) then if atan(b/a) = alpha return the value of minus alpha, ie. give a fourth quadrant angle.

Hope that helps,

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #17 2014-01-19 11:36:31

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,593

### Re: angle between points of a line

Hi pari_alf,

you guys can note the angle of 1st index to the 2nd index point is -97 that meant in downward direction.

It should be at upward direction i meant angle should be around 90.
and direction code should be 3 or 4.

Why my angle of point index 1 to index2, is inverted.

According to my calculations in Geogebra, Excel and Mathematica, all six angles (including their signs) that you gave in your first post are correct.

Have you checked out my post #15 yet?

Looking at the values of the y-axis points in your first post, and assuming the usual upward direction of the y-axis, index 5 is the highest point (168) on the y-axis and index 2 the lowest (11), yet your drawing in post #6 has the orientation of these two points, and all the others, inverted (ie, you show index 5 as the lowest and index 2 as the highest..etc).

It appears that in your drawing all six y-axis values have been incorrectly signed. I wonder if this could be the reason that you think the line from index 1 to index 2 should be upward instead of downward, and that the corresponding angle is inverted. To me, the line from index 1 to index 2 is downward and the corresponding angle is negative, indicating a clockwise (downward) angle from the x-axis of index 1.

In the image below I've used atan instead of atan2, which might help clarify things because of its slightly different approach.

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

## #18 2014-01-19 14:36:03

pari_alf
Member
Registered: 2013-12-01
Posts: 85

### Re: angle between points of a line

phrontister wrote:

It appears that in your drawing all six y-axis values have been incorrectly signed. I wonder if this could be the reason that you think the line from index 1 to index 2 should be upward instead of downward, and that the corresponding angle is inverted.

Well if you see in my posted figure,
1. the start point is shown in green circle that is index 0. then point of index 0 is going down diagonally probably in 3rd quadrant.
2. then index point 1 is going upward probably in 2nd or 1st quadrant.
3. point of index 2 is going down to index point 3 in 3rd quadrant.
4. then index point 3 is going diagonally in 3rd  quadrant.

that meant that our points of line are going in anti clockwise direction because our start point is green and end point is red in the figure post 6.

Then how are you saying that it is going clockwise direction?

Note: These points are exactly correct. these points are drawn on an image window. where the index of an image is started 0,0 at the top left corner of the image.. and going down diagonally bottom right of the image is the highest index value of the image.

So I have problem in only direction of line of points index 1 to index 2.
It is shown that the line from index 1 to index 2 , is going upward direction that meant angle should be positive and around 97,..
because our points are shown in anti-clockwise direction.

I don't know that is the issue. because these points are correct and we know the start and end point as we can see in the image attached in post 6. Which clearly shows that lines are going in anti clockwise direction.

Last edited by pari_alf (2014-01-19 14:46:29)

Offline

## #19 2014-01-19 15:12:29

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,593

### Re: angle between points of a line

Hi pari_alf,

Note: These points are exactly correct. these points are drawn on an image window. where the index of an image is started 0,0 at the top left corner of the image.. and going down diagonally bottom right of the image is the highest index value of the image.

Aha! Ok, that answers the question of whose drawing is the inverted one...it's mine, although I was following what I believe is the standard convention.

Sorry, but I'm home for lunch and just saw your post, but can't answer fully because I have to go out again shortly and I'll be gone for a few hours. I'll have a closer look at your post when I return and I'll reply then...and I think I can now see the answer through the fog.

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

## #20 2014-01-19 15:13:50

pari_alf
Member
Registered: 2013-12-01
Posts: 85

### Re: angle between points of a line

phrontister wrote:

Hi pari_alf,

Note: These points are exactly correct. these points are drawn on an image window. where the index of an image is started 0,0 at the top left corner of the image.. and going down diagonally bottom right of the image is the highest index value of the image.

Aha! Ok, that answers the question of whose drawing is the inverted one...it's mine, although I was following what I believe is the standard convention.

Sorry, but I'm home for lunch and just saw your post, but can't answer fully because I have to go out again shortly and I'll be gone for a few hours. I'll have a closer look at your post when I return and I'll reply then...and I think I can now see the answer through the fog.

Ahhhan.. Thanks .. hope to see you soon.

Offline

## #21 2014-01-20 01:09:32

pari_alf
Member
Registered: 2013-12-01
Posts: 85

### Re: angle between points of a line

waiting for reply
I need to fix this issue..

Offline

## #22 2014-01-20 02:24:53

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,593

### Re: angle between points of a line

Sorry...working on it now. I started my reply some time ago but then I had to leave it to finish something that couldn't wait. Won't be too much longer, I hope...

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

## #23 2014-01-20 04:24:45

bob bundy
Administrator
Registered: 2010-06-20
Posts: 8,155

### Re: angle between points of a line

hi pari_alf

I thought this was settled with post 16.  In case you missed it here it is again.

Let alpha be an acute angle measured anti-clockwise from the x axis.  Let a and b be positive numbers.

Then atan(b/a) = atan(-b/-a) so a line in the first quadrant and a line in the third quadrant will give the same angle.

Similarly, lines in the second and fourth quadrant will give the same angle.

So the software programmers have made a second function, atan2, which works out the same angle but applies the following correction, depending on the sign of dx and dy.

(dx,dy) = (a,b) then if atan(b/a) = alpha return the value of alpha.

(dx,dy) = (-a,b) then if atan(b/a) = alpha return the value of 180 - alpha, ie. give a second quadrant angle.

(dx,dy) = (-a,-b) then if atan(b/a) = alpha return the value of alpha - 180, ie. give a third quadrant angle.

(dx,dy) = (a,-b) then if atan(b/a) = alpha return the value of minus alpha, ie. give a fourth quadrant angle.

If you look at the table below you will see that atan2 is behaving exactly as expected.  Your first line is in the first quadrant, the second is in the third quadrant and the remainder are in the second quadrant.  That's how they are in your diagram and that's what atan2 is reporting.

Hope that helps,

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## #24 2014-01-20 04:26:04

phrontister
Real Member
From: The Land of Tomorrow
Registered: 2009-07-12
Posts: 4,593

### Re: angle between points of a line

Hi pari_alf,

Ok, to avoid confusion I'll depart from the entry of points via the standard convention that is used by Geogebra, Mathematica and others where the first quadrant is used, and continue instead in the fourth quadrant that is used by your program. I've been able to redraw my atan2 and atan images in Geogebra in the fourth quadrant (see below)...by which I mean that all six index points appear in Geogebra's fourth quadrant.

pari_alf wrote:

that meant that our points of line are going in anti clockwise direction because our start point is green and end point is red in the figure post 6.

Then how are you saying that it is going clockwise direction?

I referred only to the Index1/Index2 angle that is measured from the x-axis of Index1, and not the positional relationship between all the points...as follows:

phrontister wrote:

I now can't see anything wrong with the Index 1/Index 2 angle measurement of -97.0379°, because:
- the negative sign indicates that the angle is measured in a clockwise direction from Index 1's  x-axis; and
- the clockwise measuring direction combined with the angle value places the sloping line from Index 1 to Index 2 downward at 7.0379° left of vertical.

The angles we've been calculating are those formed by a line to the x-axis of its index point, and whether or not an angle is positive or negative is determined by the direction of the creation of the angle: anticlockwise = positive, and clockwise = negative. Btw, I didn't know any of this until trying to find the answer to your problem, but all sites I found on the net that mention this agree on it.

Now, I should point out that what I'll say next about positive and negative angles is the opposite of how they're understood in standard convention, but, short of asking you to redraw your image to agree with standard orientation, this is probably my best alternative. Where 'positive' is used, convention has it as 'negative' (and vice versa), and where 'clockwise' is used, convention has it as 'anticlockwise' (and vice versa). Therefore in your case anticlockwise = negative, and clockwise = positive. Confusing, no?! Maybe I should have asked you to follow convention!

In my first drawing below, imagine that line AB is laying directly along and on top of its x-axis line AB, both 'hinged' at start point A (Index0) at (105,66). AB needs to rotate on its hinge away from its x-axis line in a clockwise direction so that the B end (Index1) reaches its target position at (114,92), thus creating positive angle PAB (70.9065°).

All angles other than QBC are created the same way and are positive.

QBC is the angle formed by line BC to its x-axis line BQ, both 'hinged' at point B (Index1) at (114,92). BC needs to rotate on its hinge away from its x-axis line in an anticlockwise direction so that the C end (Index2) reaches its target position at (104,11), thus creating negative angle QBC (-97.0379°).

The second drawing is just the atan version that I referred to in post #17, but inverted to your program's orientation.

So...there you have it. Hope that helps.

Last edited by phrontister (2014-01-20 04:29:55)

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." - Ted Nelson

Offline

## #25 2014-01-20 04:33:06

bob bundy
Administrator
Registered: 2010-06-20
Posts: 8,155

### Re: angle between points of a line

hi Phro,

Looks like we posted at almost the same time.  What you have said is equivalent to what I have said but you've used rotations and I've used quadrants.

Bob

ps.  What's wrong with having the positive y axis downwards.  Isn't that how it is for you in Oz all the time?

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

Offline

## Board footer

Powered by FluxBB