I’ve implemented a method that compares the angles formed at root between the vector to reference and two other points p1 and p2:
// Compares the angles formed at ‘root’ between the vector to ‘reference’ and two other points ‘p1’ and ‘p2’.
// Returns:
// 0 => delta angles are equal
// -1 => delta angle to ‘p1’ is smaller than angle to ‘p2’
// +1 => delta angle to ‘p1’ is larger than angle to ‘p2’
int CompareAngle(Int2 root, Int2 reference, Int2 p1, Int2 p2)
{
// Vectors from root to p1 and p2
var vx1 = p1.x - root.x;
var vy1 = p1.y - root.y;
var vx2 = p2.x - root.x;
var vy2 = p2.y - root.y;
// Reference vector
var vrx = reference.x - root.x;
var vry = reference.y - root.y;
// Compute signed angles relative to reference vector using atan2
var angle1 = Math.Atan2(vy1 * vrx - vx1 * vry, vx1 * vrx + vy1 * vry); // angle between reference and p1
var angle2 = Math.Atan2(vy2 * vrx - vx2 * vry, vx2 * vrx + vy2 * vry); // angle between reference and p2
// Compare absolute angles
var diff = Math.Abs(angle1) - Math.Abs(angle2);
if (Math.Abs(diff) < double.Epsilon)
return 0;
return diff < 0 ? -1 : 1;
}
All inputs are integer-based (Int2), and I’d like the calculation to avoid using floats or atan2 for performance and precision reasons.
Does A* provide an integer-based version of CompareAngle,
If there isn’t an existing integer-based version of CompareAngle, can you provide some ideas on how to implement it?