(Link to AcmlmWiki) Offline: thank ||bass
Register | Login
Views: 13,040,846
Main | Memberlist | Active users | Calendar | Chat | Online users
Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album
04-23-23 03:03 AM
0 users currently in Programming.
Acmlm's Board - I3 Archive - Programming - Math -- Finding the angle of a line New poll | |
Add to favorites | Next newer thread | Next older thread
User Post
Disch

Red Cheep-cheep


 





Since: 12-10-05

Last post: 6187 days
Last view: 6187 days
Posted on 02-15-06 01:27 PM Link | Quote
I knew how to do this at some point, but I can't remember it.

I know the two points of a line -- what I need is to find out the angle of the line.

For example I know that (1,2) to (5,6) makes a 45 degree line -- but how do I do the math to figure that out? Finding the slope seems easy enough but then I have to have a special case for 'undefined' slope. Plus it wouldn't count the direction (45 degree and 225 degree lines have the same slope, for example).

So yeah -- anyone know?

Thanks in advance.
MathOnNapkins

1100

In SPC700 HELL


 





Since: 11-18-05

Last post: 5907 days
Last view: 5907 days
Posted on 02-15-06 02:03 PM Link | Quote
you need to use an inverse tangent function, or something equivalent.

Take your two points (a,b) and (c,d)

let dx = (c - a) and let dy = (d - b). The d stands for change of course. change in the respective coordinate.

since tan(angle) gives the slope of the line with that angle, arctan(slope) gives the angle of that line. arctan is aka inverse tangent.

Now, a line in the plane has no inherent angle. However, a vector does. We could possibly say that one of the ends acts as a makeshift origin, and that the other point extends from it to form a vector/ray. The reason I say the line has no angle is that arctan(slope) is double valued for every slope of a line. e.g. arctan(1) can be 45 degrees, or 225 degrees. But a ray does have a definite angle.

Anyways, on to the math:

to find the angle, compute arctan(dy/dx). To speed things up, you can of course round the fraction and use a table of values. arctan is probably expensive on a computer.

Assuming that this is a ray, and that (a,b) is the starting point, use the following code:



// you could use radians but whatever.

if(dx == 0)
{
if( d > b)
angle = 90;
else
angle = 270;
}
else
{
angle = arctan(dx/dy); //

if( (dx * dy) < 0)
{
if( dx > 0 )
angle += 180;
}
else
{
if( dx < 0)
angle += 180;

}

angle = angle % 360; // the arctan should output values ranging from 0 to 180, but this is in case you decide to use -90 to 90 instead.


}

Disch

Red Cheep-cheep


 





Since: 12-10-05

Last post: 6187 days
Last view: 6187 days
Posted on 02-15-06 02:34 PM Link | Quote
atan, that's right.

It's all coming back to me now. Thanks a million =D
Guy Perfect









Since: 11-18-05

Last post: 5909 days
Last view: 5907 days
Posted on 02-15-06 05:50 PM Link | Quote
In retrospect, you can use the angle and velocity of a vector to project a point in cartesian space. Just use Sin * Vel for Y and Cos * Vel for X.

For detecting the angle to a point, I think I can improve on MoN's method a bit. This is majoratively cosmetics and a few logical messings with. It's also written in BASIC, which many people wouldn't consider an improvement.

Specifically, I wouldn't use modulus to return the value. It may work in C, but memory is telling me that you'll get an integer value doing that. BASIC's automaticity might be giving me false knowledge, however.

And of course, in languages that support it, I recommend returning the value the instant it's finalized instead of proceeding through the remainder of the unnecessary conditional statements. "Return 0" as opposed to "Temp = 0"

This function takes two parameters, which are referred to as the "horizontal" and "vertical" points, depending on the perspective. It works for X and Y, but it also serves well in 3D applications. It uses the built-in trigonometric functions for speed purposes, which means that it returns an angle expressed in radians.

Pi is assumed to be defined, but if it isn't, you can calculate it with 4 * Tan-1(1). That'd be atan() in C.

'Calculates the angle to a point
Public Function AngleToPoint(Horz As Double, Vert As Double) As Double
Dim Temp As Double

If Horz = 0 Then 'Undefined slope
If Vert > 0 Then Temp = Pi / 2
If Vert = 0 Then Temp = 0
If Vert < 0 Then Temp = Pi * 1.5
Else 'Defined slope
Temp = Atn(Vert / Horz)
If Horz < 0 Then Temp = Temp + Pi
If Temp < 0 Then Temp = Temp + 2 * Pi
End If

'Return the value
AngleToPoint = Temp
End Function
FreeDOS +

Giant Red Koopa
Legion: freedos = fritos








Since: 11-17-05
From: Seattle

Last post: 5907 days
Last view: 5907 days
Posted on 02-15-06 07:51 PM Link | Quote
Not everything should be done in BASIC
Guy Perfect









Since: 11-18-05

Last post: 5909 days
Last view: 5907 days
Posted on 02-15-06 07:58 PM Link | Quote
Not everything should be done in C. Let us keep things on topic, hm?
Disch

Red Cheep-cheep


 





Since: 12-10-05

Last post: 6187 days
Last view: 6187 days
Posted on 02-15-06 10:43 PM Link | Quote
Thanks everyone. Fortunately, the lovely standard C library includes a function 'atan2' which lets me avoid all those ugly conditionals and checks and mods and crap.


angle = atan2( y2-y1, x2-x1 );


is all it takes.

Thanks again everyone.
Guy Perfect









Since: 11-18-05

Last post: 5909 days
Last view: 5907 days
Posted on 02-15-06 11:07 PM Link | Quote
Huh... Neato. I didn't know that was in the library. I guess everyone learned something today!
neotransotaku

Sledge Brother
Liberated from school...until MLK day








Since: 11-17-05
From: In Hearst Field Annex...

Last post: 5909 days
Last view: 5907 days
Posted on 02-16-06 04:50 PM Link | Quote
There is more to the math library than meets the eye...
Add to favorites | Next newer thread | Next older thread
Acmlm's Board - I3 Archive - Programming - Math -- Finding the angle of a line |


ABII

Acmlmboard 1.92.999, 9/17/2006
©2000-2006 Acmlm, Emuz, Blades, Xkeeper

Page rendered in 0.019 seconds; used 390.43 kB (max 475.62 kB)