[RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.
the other is i have hit a roadblock in programming math-wise.
i can't figure out how to convert two points on a grid into an angle.
edit: final post has working code, but the code is in xna 4 using vector2. The only difference is you will need to find length yourself.
Re: any good online sites to refresh trigonometry?
Re: any good online sites to refresh trigonometry?
Quote:
Originally Posted by
Lord Orwell
i can't figure out how to convert two points on a grid into an angle.
The dot product formula makes the angle between vectors easy to compute. For points (a, b), (c, d), the angle between the vectors from (0, 0) to each point is
ArcCos((a, b) dot (c, d)/|(a, b)||(c, d)|)
= ArcCos((ac+bd)/Sqrt((a^2+b^2)*(c^2+d^2)))
It can be proven with the law of cosines, which is a generalization of the Pythagorean theorem. There are much more intuitive ways to state this rule but this one is computationally simple. Sorry, I don't know any trig review sites.
Re: any good online sites to refresh trigonometry?
Quote:
Originally Posted by
jemidiah
The dot product formula makes the angle between vectors easy to compute. For points (a, b), (c, d), the angle between the vectors from (0, 0) to each point is
ArcCos((a, b) dot (c, d)/|(a, b)||(c, d)|)
= ArcCos((ac+bd)/Sqrt((a^2+b^2)*(c^2+d^2)))
It can be proven with the law of cosines, which is a generalization of the Pythagorean theorem. There are much more intuitive ways to state this rule but this one is computationally simple. Sorry, I don't know any trig review sites.
so (ac + bd), how would i do just that part if point a,b is on the origin? It looks like ac + bd would then be zero and i would get a division by zero error?
Re: any good online sites to refresh trigonometry?
If ac+bd = 0 there is no problem, since you're just multiplying by it. If a=b=0, there is a problem, since a^2+b^2 = 0 so you're dividing by 0. To be clear, the formula I gave is used to find the angle theta (not theta_1 or theta_2) as in this picture. The algebraic "trouble" of dividing by 0 when a=b=0 corresponds to the geometric trouble of the A vector having length 0, in which case the angle isn't defined since you're trying to find the angle between a vector and a point, or, if c=d=0, a point and a point.
My best guess is that you want to find the reference angle of a single vector. In the above picture, the reference angle for vector A is labeled as theta_2. You can find it using my formula by making the B vector, (c, d), lie on the positive x-axis. For instance, use c=1, d=0. The formula simplifies significantly in this special case.
Re: any good online sites to refresh trigonometry?
i finally solved this. There was a code sample on a yahoo answers page that i started with, and after i corrected the three glaring errors in it, i rotated the axis and fixed it to make a 0-359 degrees with vertical top being 0.
C# Code:
Vector2 diff = new Vector2(mx, my);
Rotation = Math.Asin(1 * diff.Y / diff.Length());
Rotation = Math.PI / 2 + Rotation;
if (diff.X < 0.0)
{
Rotation = MathHelper.ToRadians(360) - Rotation;// Rotation * -1;
}
Re: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.
For things like this, in the future you might prefer the Atan2 function. Most modern language's math libraries include it; there's a Wikipedia page on it.
Re: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.
i actually tried atan2, but didn't have any luck. If you can improve my code, please do so. I'm happy it works, but it is for a game and processing may become an issue.
Re: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.
I imagine a library atan2 implementation would be somewhat optimized, though no matter what you'll need to evaluate an inverse trig function. Using atan2 gets rid of some extra calculations like square rooting. More importantly, using it makes for simple, readable code--no muddling through trig identities to figure out what the code does.
The following two pure VC# routines should be identical. The first is your original without using XNA. The atan2 version would be even simpler except your axes are slightly non-standard and you wanted output to be positive.
Code:
public double GetAngle1(double x, double y)
{
double rotation = Math.Asin(1 * y / (Math.Sqrt(x*x + y*y)));
rotation += Math.PI / 2;
if (x < 0.0)
{
rotation = 2 * Math.PI - rotation;
}
return rotation;
}
public double GetAngle2(double x, double y)
{
double rotation = Math.Atan2(x, -y);
return rotation > 0 ? rotation : rotation + 2*Math.PI;
}
Re: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.
ok i am not complete learned on all the c#/c style math operands. What do ? and : do?
Re: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.
Oh, that's just the inline if syntax. The C#
is equivalent to the VB6 (and I believe .NET)
I suppose I should mention the one difference, which I think has been fixed in newer versions of VB.NET. On older versions of the language, both the t and f "arguments" get evaluated in VB, but they're not both evaluated in C# (which follows the C tradition of using short-circuiting logic by default).
Perhaps I should have written my translation of your code as
Code:
public double GetAngle1(double x, double y)
{
double rotation = Math.Asin(1 * y / (Math.Sqrt(x*x + y*y)));
rotation += Math.PI / 2;
return rotation < 0.0 ? 2 * Math.PI - rotation : rotation;
}
I find ? : more idiomatic than small If blocks, but that's just personal preference.