#### Archived

This topic is now archived and is closed to further replies.

# Can an angle be mirrored?

This topic is 5045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is a really simple question. I'd like to know if an angle can be mirrored right-left without a switch-case statement. The angle is limited to 45 degree increments. if = new 45 = 315 90 = 270 135 = 225 225 = 135 270 = 90 315 = 45 0 = 0 180 = 180 Thanks for any help! [edited by - Jiia on March 20, 2004 3:06:52 PM]

##### Share on other sites
new_angle = 360 - old_angle?

[edited by - Muzzafarath on March 20, 2004 3:09:53 PM]

##### Share on other sites
LMAO! Thanks I appreciate it.

##### Share on other sites
Can I ask another simple question that will make me further look like an idiot?

How can I use two velocities (x & y) to determine a 0-360 angle? The velocities can be positive and negative, and can be any range.

Thanks again!

##### Share on other sites
Finding the angle? If I understand you correctly, the below code should work, I'm not entirely sure, but in theory I think it will.

float tangent = y / x;float angle = 1 / tangent;

That should give you the angle. Just a warning, this code only works with first quadrant angles. If you want other quadrants, you will have to write some testing and so forth.

(Stolen from Programmer One)
UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus

[edited by - Dragon88 on March 20, 2004 6:09:34 PM]

[edited by - Dragon88 on March 20, 2004 6:10:17 PM]

##### Share on other sites
quote:
Original post by Jiia
Can I ask another simple question that will make me further look like an idiot?

How can I use two velocities (x & y) to determine a 0-360 angle? The velocities can be positive and negative, and can be any range.

Thanks again!

I assume you mean that x and y are the components of one velocity.

If so, the angle would be arctan(y/x).

Here''s why.

Given a triangle where the hypotenuse is the actual velocity, and each leg is a component of that velocity (x and y), the angle formed between the resultant velocity and the x-axis would be the angle you''re looking for.
The tangent of that angle is equal to the opposite side (y) over the adjacent side (x). Therefore, the angle is equal to the arctangent of the ratio y/x.

##### Share on other sites
quote:
Original post by Dragon88
float tangent = y / x;float angle = 1 / tangent;

...won''t work. It should be (for example)
float angle_rad = atan2(y,x);float angle_deg = angle_rad*180/PI;

The result is in range -180...180.
There''s also a function, atan(), which will take the tangent as parameter and return a value in range -PI/2...PI/2. In this case you have to determine the quadrant yourself.

##### Share on other sites
This seems to return an angle of -78 with XVel=40, YVel=-40:

angle = (LONG) atan(DOUBLE(Vel.Y)/DOUBLE(Vel.X)) * 100.0;

Did I screw something up? Is up negative or positive? What is the value range of atan?

Thanks for all of the help, much appreciated!

EDIT: I totally forgot to mention that my straight-up angle is 0. Straight down is 180. But that could be easily fixed by adding or subtracting. I also need to watch out for X-velocity to be zero, correct? Should I just make a special case and set 0 and 180 manually?

[edited by - Jiia on March 20, 2004 6:40:59 PM]

##### Share on other sites
atan() returns the angle in radians. To convert it to degrees, you do
angle_deg = angle_rad*180/PI;

EDIT: Typo. BTW if you'll stay with atan() instead of atan2(), you'll have to determine the quadrant of the angle yourself (basically you check whether vel_x and/or vel_y are positive or negative and such; drawing things on paper will help).

[edited by - nonpop on March 20, 2004 6:55:10 PM]

##### Share on other sites
New_Angle = 360 - Old_Angle;

##### Share on other sites
Hmm how does atan2 work? It just does the 4 quadrants for you?

This is what I had to do to get the correct angle for my coordinate system:

angle_rad = atan2( DOUBLE(Vel.Y) , DOUBLE(Vel.X) );
angle_deg = -( (angle_rad * 180 / 3.141) - 90.0 );

Can that be right? Am I doing anything unnecessary?
Thanks again.

quote:
Original post by Anonymous Poster
New_Angle = 360 - Old_Angle;

LMAO! Okay, I get it already

[edited by - Jiia on March 20, 2004 7:33:18 PM]

##### Share on other sites
Just in case anyone is curious, I''m trying to get an arrow to point in it''s own path.

##### Share on other sites
quote:
Original post by Jiia
Hmm how does atan2 work? It just does the 4 quadrants for you?

Well, let's assume a coordinate system with x pointing right, y pointing up and angles growing counter-clockwise (zero degrees points right). Our mission is to determine the angle d between the x-axis and vector v = (v_x,v_y).

First we'll calculate the tangent of the angle:
tan d = v_y/v_x

That's the slope of the vector v. To get the angle we do:
d = arctan (tan d) = arctan (v_y / v_x)
(arcusfunctions have, by definition, the following property: f(arcf(x)) = x)

So, now we have an angle. But that's only the angle between the x-axis and a line parallel to the vector. The problem is that vectors also have direction.

To get the wanted angle we'll examine the signs of v_x and v_y.
- If they're both positive, the vector is in the 1st quadrant (up-right) and the angle is already correct
- v_x < 0 and v_y > 0 => 2nd quadrant. Now d is negative and you have to add 180 to it to get the angle you want
- Both negative => 3rd quadrant. d is positive and you add 180 again
- v_x > 0 and v_y < 0 => 4th quadrant. d is negative; you'll add 360.

All this will become clearer if you draw it on paper. And, of course, you can ask more here and I'll try to explain (I'm sure I've left things out and could probably have been much more clearer)

BTW, this is my first post in which I try to explain things. Actually, it's one of my first times I even try to explain something. I'll appreciate any (constructive) feedback.

[edited by - nonpop on March 21, 2004 4:20:27 PM]

##### Share on other sites
I usually just use atan2(x,y); ... but it depends on your coordinate system (whether you want angle 0 to point at +y or +x and so on). Also it''s probably not necessary to convert angles into degrees since most math you do with them will require converting back to radians anyway.