Forum Topic: Math Question (geometry)

(794 views • 18 replies)

This topic is 1 page long.

<< < > >>
Questioning

TheShrike

Reply To Post Reply & Quote

Posted at: 2/11/03 03:44 AM

TheShrike EVIL LEVEL 38

Sign-Up: 01/05/01

Posts: 10,267

Ok, I really wish I'd spent more time in highschool learning math than dismantling the school network.
My question is simple:
Anyone know how to figure out the angle of a line between two points on a grid?
I know I'll kick myself when I read it, or figure it out.
Help is greatly appriciated!

"A witty quote proves nothing."
~Voltaire

BBS Signature

Questioning

TheShrike

Reply To Post Reply & Quote

Posted at: 2/11/03 03:49 AM

TheShrike EVIL LEVEL 38

Sign-Up: 01/05/01

Posts: 10,267

say, for instance, you know the location of point A, and you want to figure out the angle between point A and point B assuming that any point with the same y value on the right of point A would be 0 degrees and 180 degrees on the left of point A

Did that make sense?

"A witty quote proves nothing."
~Voltaire

BBS Signature

Happy

R34NissanSkyline

Reply To Post Reply & Quote

Posted at: 2/11/03 10:39 AM

R34NissanSkyline EVIL LEVEL 13

Sign-Up: 01/06/03

Posts: 2,477

At 2/11/03 03:49 AM, TheShrike wrote: Did that make sense?

nope


None

liljim

Reply To Post Reply & Quote

Posted at: 2/11/03 10:45 AM

liljim NEUTRAL LEVEL 27

Sign-Up: 12/16/99

Posts: 8,482

Do a search on google for "Trigonometry". You'll most likely find all the stuff you need there.


None

LordAba

Reply To Post Reply & Quote

Posted at: 2/11/03 11:47 AM

LordAba EVIL LEVEL 10

Sign-Up: 11/02/00

Posts: 2,523

At 2/11/03 03:49 AM, TheShrike wrote: say, for instance, you know the location of point A, and you want to figure out the angle between point A and point B assuming that any point with the same y value on the right of point A would be 0 degrees and 180 degrees on the left of point A

Did that make sense?

I guess. If you are using it in programming there is an easy way. Just define it being 0 at all times!!
Here would be the code(For C++)

class Points
{
private:
int firstpointX, firstpointY;
int secpointX, secpointY;
public:
Points(int x1, int y1, int x2, int y2)
{
firstpointX = x1; firstpointY = y1;
secpointX = x2; secpointY = y2;
}
int returnAngle()
{
return 0;
}
};

Simple, no?

What may man within him hide, though angel on the outward side.

BBS Signature

None

Pyromonger

Reply To Post Reply & Quote

Posted at: 2/19/03 02:34 AM

Pyromonger NEUTRAL LEVEL 15

Sign-Up: 05/03/00

Posts: 47

At 2/11/03 03:44 AM, TheShrike wrote: Anyone know how to figure out the angle of a line between two points on a grid?

There really no such thing as an angle b/w 2 points... heh... that's just a line. But from your follow up post I assume your considering the first point to be the center of a coordinate system, and you want the angle a vector from that point (now the origin) to the 2nd point would make with the X-axis...

Like the two points on a grid:

.

.

Becomes:
. ?
/
180---.-------- 0

And you want that inner angle.

Well it's pretty easy to find that... You can make the two coordinates points on a triangle. Subtract the X and Y values to get the lengths of the sides, and use a li'l trig.

Say point 1 is at (5,6) and point 2 is at (8,10).

X-length = 8 - 5 = 3
Y-Length = 10 - 6 = 4

You want the angle b/w hypotenuse and bottom side so it's just
tan(angle) = opposite/adjacent
tan(angle) = 4/3
angle = tan^-1(4/3)
angle = 53.13010235 degrees

Course that particular method only works for values < 90 degrees, but you get the idea (I hope). Programming it to handle arbitrary points and angles should be fun, and think of all the trig you'll finally learn! :P

-Pyro


None

spinewall2

Reply To Post Reply & Quote

Posted at: 2/19/03 02:41 AM

spinewall2 EVIL LEVEL 04

Sign-Up: 02/13/03

Posts: 23

by the angle of the line do you mean the slope of the line? If so its easy

(x2-x1)
m= -------
(y2-y1)


None

spinewall2

Reply To Post Reply & Quote

Posted at: 2/19/03 02:42 AM

spinewall2 EVIL LEVEL 04

Sign-Up: 02/13/03

Posts: 23

At 2/19/03 02:41 AM, spinewall2 wrote: by the angle of the line do you mean the slope of the line? If so its easy

(x2-x1)
m= -------
(y2-y1)

that should read

m= (x2-x1)/(y2-y1)

Confounded spacing! ;P


None

Pyromonger

Reply To Post Reply & Quote

Posted at: 2/21/03 09:11 PM

Pyromonger NEUTRAL LEVEL 15

Sign-Up: 05/03/00

Posts: 47

At 2/19/03 02:41 AM, spinewall2 wrote: by the angle of the line do you mean the slope of the line? If so its easy

(x2-x1)
m= -------
(y2-y1)

No, that's not what he wanted. :P He's thinking like creating a circle using the first point as the center, and the distance b/w the 2 points as the radius, then calculating the angle the 2nd point would make relative to the newly created x-axis...

Think "unit circle", 7th or 8th grade geometry.

If the 2nd point is straight to the right of 1st, the angle is 0.

If the 2nd point is right above 1st, it's pi/2 rads or 90 degrees.

If the 2nd point is straight to the left of the 1st, it's pi rads or 180 degrees.

etc etc... if it's in between, use a li'l trig to calculate the angle. Taking rise/run like your slope formula we can find opp/adj for tan... but at 90 and 270 tan is undefined so I think you need to do it case by case for each quadrant. Hmmm lemme try this in C for ya real quick... I'm gonna do it quadrant by quadrant, using arctan (tan^-1) to find the angle b/w the "bottom axis" of the quadrant and the line the 2nd point makes, then adding 90 degrees for each quadrant passed the 1st.

For instance, if we're in 3rd quadrant the "bottom axis" is the negative x-axis, so opposite = the absolute value of the y displacement, and adjacent = absolute value of the x displacement... and we'd add 180 degrees to the arctan of that.

Points on the axis are gonna be handled as special cases...

this is gonna look REALLY ugly with the spacing changed/taken out... :P

email me if ya want the pretty .C file I wrote in wordpad

-------------------------------------------------------

#include <stdio.h>
#include <math.h>

typedef enum{Quad1, Quad2, Quad3, Quad4, x_axis, y_axis, origin} Quad;
//Which Quadrant, Quad1 = 0, Quad2 = 1, etc, and values for axis and pts being same spot

typedef struct COORDINATE { //xy coordinate of pixel on screen
int x;
int y;
} Coor;

double CalcAngle(Coor pt1, Coor pt2);
Quad GetQuad(int dx, int dy);

void main(void)
{
Coor point1, point2;
double angle;

printf("Enter point 1: ");
scanf(" %i %i", &point1.x, &point1.y);
printf("Enter point 2: ", point2.x, point2.y);
scanf(" %i %i", &point2.x, &point2.y);

angle = CalcAngle(point1, point2);

printf("Angle = %g", angle);

return;
}

double CalcAngle(Coor pt1, Coor pt2)
{
int disp_x, disp_y; //displacement in X and Y direction
double angle;
Quad location;

disp_x = pt2.x - pt1.x;
disp_y = pt1.y - pt2.y; //reverse order because of pixel numeric values

location = GetQuad(disp_x, disp_y); //find out where we are

switch(location) {
case origin: angle = 0; break; //angle is undefined, but we return a value of zero
case y_axis:
if(disp_y > 0) angle = 90; //above point 1
else angle = 270; break; //below point 1
case x_axis:
if(disp_x > 0) angle = 0; //to right of point 1
else angle = 180; break; //to left of point 1
case Quad1:
angle = atan2(disp_y, disp_x); //generic case
angle = angle / M_PI * 180; break; //convert from radians to degrees
case Quad2:
angle = atan2(-disp_x, disp_y);
angle = angle / M_PI * 180.0 + 90; break; //"rotate" quadrants to use tan function
case Quad3:
angle = atan2(-disp_y, -disp_x);
angle = angle / M_PI * 180.0 + 90*2; break; //by switching signs of displacements
case Quad4:
angle = atan2(disp_x, -disp_y); //then add 90 degrees for each quad passed 1st
angle = angle / M_PI * 180.0 + 90*3;
}

return angle;
}

Quad GetQuad(int dx, int dy) //finds which quadrant point 2 is in relative to point 1
{ //will check if point 1 = point 2 or in point 2 is on an axis of point 1
if(dx == 0 && dy == 0) return origin;
if(dx == 0) return y_axis;
if(dy == 0) return x_axis;
if(dx > 0 && dy > 0) return Quad1;
if(dx < 0 && dy > 0) return Quad2;
if(dx < 0 && dy < 0) return Quad3;
if(dx > 0 && dy < 0) return Quad4;
}

-------------------------------------------------------

That's one way of doing it. You can simplify it alot with a few utility fuctions and breaking it down to a generic "if(disp_x == 0) angle = 90 + location*90; else angle = atan2(y_disp, x_disp) / M_PI * 180 + location*90" and losing the x_axis/y_axis/origin out of the enumerated type... I was too lazy to spend the extra 5 mins tho heh :P

-Pyro


Thinking

MightyJakeWASP

Reply To Post Reply & Quote

Posted at: 2/25/03 08:09 PM

MightyJakeWASP EVIL LEVEL 08

Sign-Up: 02/22/03

Posts: 3

Im sure this probably won't help, but all lines (especially if there between only two points) have 180 degrees. There's no angle in a line. If you're looking for slope, that's y= mx + b; with m being slope, b being the y-intercept and, of course, x and y being the respective points.


None

Cheesemold

Reply To Post Reply & Quote

Posted at: 2/25/03 09:25 PM

Cheesemold LIGHT LEVEL 12

Sign-Up: 03/16/01

Posts: 284

alright, umm, you're question involves detecting mouse angle. i'm actully in a class where we're workin on the pythagorym theroem and shit that involves symilar stuff, i guess. but the only thing i can help you with now, is that i remeber a tutorial on www.flashkit.com about detecting mouse angle, that is, detecting the angle that another object has to rotate in order to be facing the mouse's location. sorry i couldn't help you directly.


None

Pyromonger

Reply To Post Reply & Quote

Posted at: 2/28/03 02:38 AM

Pyromonger NEUTRAL LEVEL 15

Sign-Up: 05/03/00

Posts: 47

At 2/25/03 09:25 PM, cheesemold wrote: i remeber a tutorial on www.flashkit.com about detecting mouse angle, that is, detecting the angle that another object has to rotate in order to be facing the mouse's location. sorry i couldn't help you directly.

well, you could actually use the code I gave to do just that... just make the current location of the mouse be point 2, and the location of the object point 1 (defining the origin of the coordinate system to be the object).

The angle it'll return would be how many degrees the object must rotate counter-clockwise to be pointing at the mouse (ie, to make the angle b/w them zero).

Of course, you'd want to insert code like "if(angle > 180) angle = angle - 180" then reverse the direction of rotation so the object rotates clockwise... otherwise if the mouse cursor is right below the object the object would spin alllllll the way up and around to face it :P

-Pyro


None

Pyromonger

Reply To Post Reply & Quote

Posted at: 2/28/03 02:41 AM

Pyromonger NEUTRAL LEVEL 15

Sign-Up: 05/03/00

Posts: 47

At 2/28/03 02:38 AM, Pyromonger wrote:
Of course, you'd want to insert code like "if(angle > 180) angle = angle - 180" then reverse the direction of rotation so the object rotates clockwise... otherwise if the mouse cursor is right below the object the object would spin alllllll the way up and around to face it :P

Eerrrrrrrrrrr... more like "if(angle > 180) angle = 360 - angle;"

sorry :P


None

NEMESiSZ

Reply To Post Reply & Quote

Posted at: 3/1/03 10:50 AM

NEMESiSZ LIGHT LEVEL 41

Sign-Up: 04/13/01

Posts: 1,787

C = Too old to be relevant.

C = The Reason Why Computers Will Crash in 2038, Worse than Anyone Even Thought for Y2K.


None

Pyromonger

Reply To Post Reply & Quote

Posted at: 3/4/03 06:36 PM

Pyromonger NEUTRAL LEVEL 15

Sign-Up: 05/03/00

Posts: 47

At 3/1/03 10:50 AM, NEMESiSZ wrote: C = Too old to be relevant.

C = The Reason Why Computers Will Crash in 2038, Worse than Anyone Even Thought for Y2K.

C = most popular language, and very easy to understand

I guess tho that whole boolean logic thing is too old to be relevant too, eh? ;)

C code would be easy to rewrite in any language, so I wrote it in that. The logic would be the same no matter what language you write it in... Sort of like writing out some code for an assembly language program in a high level language, then translating it from there so you make less mistakes.

It was a math question anyways.

Sides, what would you know about the relevance of a language twice your age? ;) Remember, C was invented to implement unix OS, and you don't hear any complaints about it's stability.


None

PhantomGames

Reply To Post Reply & Quote

Posted at: 3/8/03 12:21 AM

PhantomGames NEUTRAL LEVEL 07

Sign-Up: 10/31/02

Posts: 13

all of you are just overcomplicating things.

to find the angle between two points is the arctan of the dirfference between the y values over the difference between the x values. simple as that. In flash it would be:

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

the result is in RADIANS so in order to convert it into degrees use:

angle = angle * 180/Math.PI;

badda boom badda bing done.


None

<deleted>

Reply To Post Reply & Quote

Posted at: 3/8/03 01:13 AM

engrish rules


None

AngryArab

Reply To Post Reply & Quote

Posted at: 3/17/03 11:25 AM

AngryArab LIGHT LEVEL 16

Sign-Up: 03/03/03

Posts: 4,259

Simple you make a line down and make it go to the ground that you messure anywhere from ground to the line and that you x it.

look at the picture you might know what im talking about

Math Question (geometry)


None

Pyromonger

Reply To Post Reply & Quote

Posted at: 3/17/03 04:20 PM

Pyromonger NEUTRAL LEVEL 15

Sign-Up: 05/03/00

Posts: 47

Eeeh... just drop the subject lol, phantom_games got it right.

That is, as long as the language you are coding in automatically determines which quadrant you are in...

Mathematically arctan has asymptotes at +90 and -90 degrees, so unless there is a library function in the language similar to the one I wrote that defines the arctan function, it won't work for angles outside the 1st quadrant...

*shrug*


All times are Eastern Standard Time (GMT -5) | Current Time: 08:01 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!