converting degrees to vector normals

Started by
6 comments, last by Zakwayda 17 years, 1 month ago
Hi everybody, is there a formula for converting degrees to vector normals? What i'm trying to do is this: to create an arc from a circle with a center point and radius, and start degrees and end degrees. I can already do this, but it's a long and complicated method and i don't think it's the best way to do it. Thanks for anyone's help.
Advertisement
Are you trying to solve this problem in 2D or 3D?

Also, if you have some working code, you might post it so that we can see what you're doing (be sure to use [source] tags).
This is in 2D. I tried to implement my method, but it doesn't seem to be working...
This code might not be perfect because I don't use C++, but here it is:

// calculates the arc start and end points, given the two degree values// 'a' and 'b' are two class members (vectors)void calcPoints(float aDeg, float bDeg)  {		Vector an = degToVector(aDeg);	Vector bn = degToVector(bDeg);		a = an * radius;	b = bn * radius;}// calculates and returns a normal of direction based on a degree value// 0 degrees is a left-to-right horizontal lineVector degToVector(deg#)  {		Vector startNormal;	        //make the degrees less than 90, storing the rest of the degrees (to the closest multiple of 90) as a normal	If(deg >= 0 && deg < 90)   {		startNormal = New Vector(1,0);        }	ElseIf(deg >= 90 && deg < 180)  {		startNormal = New Vector(0,-1);		deg -= 90;        }	ElseIf(deg >= 180 && deg < 270)  {		startNormal = New Vector(-1,0);		deg -= 180;        }	ElseIf(deg >= 270 && deg < 360)  {		startNormal = New Vector(0,1);		deg -= 270;	}	        //find the side of the triangle formed by a horizontal line (0 degrees) and the remaining degrees	float triSide = tan(deg)*radius;	Vector normal = New Vector(radius, triSide);        normal.normalize();        //return the normal of the sum of the bulk and remainder normals	Return (startNormal + normal).normalize();	}
It sorta looks like you trying to create a unit vector oriented counter clockwize from the x axis by forming a right triangle using the radius as the hypotenuse. A better way to approach this is to use a bit of trig... assuming this is what you are trying to do.

(I am assuming a circle)

r(t) = cos(theta)i + sin(theta)j

If you want normals to arbitrary plane curves, then look into the gradent and level curves.

Edit.
Opps... ViperG is right. No need to nomalize when using the unit circle.



[Edited by - smc on March 8, 2007 1:08:45 AM]
∫Mc
If you just want the vector normal all you do is:

piover180=0.0174532925f; (this is needed to convert radians back to degrees,c functions sin and cos return radians...)

vector.x=sin(deg*piover180);
vectoy.y=cos(deg*piover180);

and they return in doubles, so typecast it to just get a float

vector.x=(float)sin(deg*piover180);
vectoy.y=(float)cos(deg*piover180);
Black Sky A Star Control 2/Elite like game
Ok, ViperG's method seems simple, but it's still not working. Do I still need to make the degree value below 90? I tried it with and without that, but neither works. The angles are always wrong, and the vectors go beyond the radius for some reason. I'd post some code, but i tried so many different ways of doing it that i don't know what to post :P

*edit*
nevermind it's working fine after i fixed a dumb mistake i made. It works perfectly now, thank you thank you thank you thank you thank you!!! :P
Just one more question... to convert vector normals to degrees i figured I would just have to do the inverse sine function on the x value or the inv. cosine on the y value. I tried this, but it doesn't work. It seems like the orientation of the normal messes things up. How am i supposed to do this?
Quote:Original post by Epilef
It seems like the orientation of the normal messes things up. How am i supposed to do this?
In C, C++, and other languages that offer it, use the 'atan2' function when you need to compute an angle from a 2D vector. It's numerically stable, and takes care of the details of determining the correct quadrant for you.

This topic is closed to new replies.

Advertisement