Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
angle between 2 points (c++)
#1
is there a function in c/c++ to get the angle between 2 points? if so, what is it? if not, how can i write one? i assume it will use trig but my maths is a bit rusty.
Help?? :???:
Reply
#2
two points is a line... you'd need a third to get an actual angle.

Or are you talking about something like this?
Code:
.


              .
--------------------------------------

where the line is the base and the two dots are the intersecting line?

You can just take the distance formula of the two dots, and a random dot on the base. Say, the dot near the bottom is A, the dot on the base is B, and the one in the air is C.

Code:
AC = SQRT((Ax - Cx)^2 + (Ay - Cy)^2)
AB = SQRT((Ax - Bx)^2 + (Ay - By)^2)

Angle = 1/cos(AB/AC)

I think...
am an asshole. Get used to it.
Reply
#3
I can't understand your question, but the atan y atan2 should be the function you are looking for.
Look at the manual for further examples about this.
Reply
#4
as i said, my maths is rusty. i think it would go something like this

Code:
x1=10;y1=10;
x2=100;y2=60;

xdif=x2-x1;
ydif=y2-y1;

i know tan theta = opposite/ajacent
thus theta = arctan opposite/ajacent
thus theta = arctan xdif/ydif
if that correct. the machine im using dosnt have a compiler on it so i cant test it. is the atan function the arc tan im looking for. or is there a easier way to do this (speed is always good!)

EDIT:
Im looking for the equivelent to RelLibs RelAngle. its for calculating x and y velocitys and trajectories in my game.
Reply
#5
I'm pretty sure my way would work. I don't know what you're trying to do though.
am an asshole. Get used to it.
Reply
#6
citpes wrote:
Quote:i know tan theta = opposite/ajacent
thus theta = arctan opposite/ajacent
thus theta = arctan xdif/ydif
Your in the right ballpark
Code:
theta = arctan (ydif/xdif)             (the adjacent is xdif)
This will give you an answer from -pi/2 to pi/2. If you want an angle from 0 to pi, add pi to the negative values. If you want values from 0 to 2pi, then add pi when xdif is negative and also when ydif is negative (this totals 2pi when both are negative).

Ninkazu,
try your method with some points. If I understand what you are trying to do, I don't think it will work.
For example if A =(0,0), then if you try it with B = (Bx,By) and with B = (By,Bx) you will get the same answer both times.
hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
Reply
#7
Allegro: Atan2 or was it fatan?


If all else fails...


translate this to c. ;*)

Code:
FUNCTION Rel.Angle% (X1, Y1, X2, Y2)
'Parameters:
'x1,y1= the starting coord
'x2,y2=the target coord
'Returns=the Angle in degrees between the two coords
'useable with SIN and COS so this is useful for calculating the
'vectors between 2 points.

'Rad=Deg*PI/180
'Rad*180=Deg*PI
'Rad*180/PI=Deg
'Deg=Rad*180/PI

IF X1 = X2 THEN
IF Y1 < Y2 THEN
Rel.Angle% = 90
EXIT FUNCTION
ELSEIF Y1 > Y2 THEN
Rel.Angle% = 270
EXIT FUNCTION
ELSE
Rel.Angle% = 0
EXIT FUNCTION
END IF
ELSE
DeltaX = X2 - X1
DeltaY = Y2 - Y1
AngleTemp% = ATN(DeltaY / DeltaX) * 180 / 3.141593
AngleTemp% = AngleTemp% + 90
IF X1 > X2 THEN
AngleTemp% = AngleTemp% + 180
END IF
END IF
AngleTemp% = AngleTemp% - 90
IF AngleTemp% < 0 THEN AngleTemp% = AngleTemp% + 360
Rel.Angle% = AngleTemp%


END FUNCTION

If you want the ASM version, just ask .;*)

BTW, if you're doing homing missiles, ATAN2 and FATAN is not a good choice. Use a modified dot product.
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#8
That's just atan2 written out using atan... it's built into the standard C/C++ library...
Reply
#9
thanx, rel. ill try it later. the asm source would be useless to me, i hardly understand the qbasic source :lol:
whats all this ruckess bout some atan2 thing. its all gone straight over my head!
Reply
#10
This is what you want. Print it out and sleep with it under your pillow, treat it like your best friend, trust me you'll thank me after you read it:

This sine and cosine tut for game coders:
http://www.pixelate.co.za/issues/5/5/art...sincos.htm

A description of the article
Quote:This article will explain sine, cosine, vectors, atan2, and some useful special effects such as how to make homing missiles and how bitmap rotation works
'Nuf said.
b]Hard Rock[/b]
[The Stars Dev Company] [Metal Qb flopped] [The Terror]
Stop Double Posts!
Whats better? HTML or Variables?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)