Error in calculating theta of angle
This question / bug is directly related to this Angle Measurer in C#. The
problem seems to be that the theta is wrong in angles greater than 180
(I'm working in degrees, not radians).
Here's a helpful screen shot. This picture represents an overhead view of
three dinosaurs. The body of the dinosaurs are in gray. The heads are a
white dot. The 'angle of vision' for each dinosaur (not the same for all
species) are the blue lines.
As you can see the facing of each dinosaur is correct. The theta for the
angle between John and Julie looks correct. However, the angles from Julie
to John and Muffie to John are quite wrong. Each should be > 180 degrees.
Here's the code snippet:
double DinoAFacing = FindAngle(Dinosaurs[DinoA].Head.X,
Dinosaurs[DinoA].Head.Y,
Dinosaurs[DinoA].Location[Dinosaurs[DinoA].Location.Count - 1].X,
Dinosaurs[DinoA].Location[Dinosaurs[DinoA].Location.Count - 1].Y);
int Specie = ReturnDinosaurSpecie(DinoA);
double x = 50 * Math.Cos((DinoAFacing - 90) * (Math.PI / 180.0));
double y = 50 * Math.Sin((DinoAFacing - 90) * (Math.PI / 180.0));
x += Dinosaurs[DinoA].Head.X;
y += Dinosaurs[DinoA].Head.Y;
System.Windows.Point A = new System.Windows.Point();
A.X = x - Dinosaurs[DinoA].Head.X;
A.Y = y - Dinosaurs[DinoA].Head.Y;
System.Windows.Point B = new System.Windows.Point();
B.X = Dinosaurs[DinoB].Head.X - Dinosaurs[DinoA].Head.X;
B.Y = Dinosaurs[DinoB].Head.Y - Dinosaurs[DinoA].Head.Y;
double ALen = Math.Sqrt(Math.Pow(A.X, 2) + Math.Pow(A.Y, 2));
double BLen = Math.Sqrt(Math.Pow(B.X, 2) + Math.Pow(B.Y, 2));
double dotProduct = A.X * B.X + A.Y * B.Y;
double Angle = Math.Abs(((180 / Math.PI) * Math.Acos(dotProduct / (ALen *
BLen))));
slug = Dinosaurs[DinoA].PersonalName + " is facing: " +
string.Format("{0:f2}", string.Format("{0:f2}", DinoAFacing)) +
"\nThe angle between " + Dinosaurs[DinoA].PersonalName
+ " and " + Dinosaurs[DinoB].PersonalName + " is " +
string.Format("{0:f2}", Angle) +
"\n" + Dinosaurs[DinoA].PersonalName + " CAN see " +
Dinosaurs[DinoB].PersonalName;
System.Windows.MessageBox.Show(slug, "Dino vision",
System.Windows.MessageBoxButton.OK);
Can any of you math boffins see the error of my ways?
Thanks!
No comments:
Post a Comment