![]() |
| Register | Login | |||||
|
Main
| Memberlist
| Active users
| Calendar
| Chat
| Online users Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album |
|
| | |||
| 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 |
| ||
| 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 |
| ||
| 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:
|
|||
|
Disch Red Cheep-cheep Since: 12-10-05 Last post: 6187 days Last view: 6187 days |
| ||
| 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 |
| ||
| 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 exp 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 |
|||
|
FreeDOS + Giant Red Koopa Legion: freedos = fritos Since: 11-17-05 From: Seattle Last post: 5907 days Last view: 5907 days |
| ||
| Not everything should be done in BASIC | |||
|
Guy Perfect Since: 11-18-05 Last post: 5909 days Last view: 5907 days |
| ||
| 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 |
| ||
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.
is all it takes. Thanks again everyone. |
|||
|
Guy Perfect Since: 11-18-05 Last post: 5909 days Last view: 5907 days |
| ||
| 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 |
| ||
| 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 |
| ![]() |