How can I get the average angle from a set of three points?

55 Views Asked by At

Please forgive me, but I'm not mathematically inclined and i'm trying to solve a problem for a video game i'm developing. My levels are composed of vector shapes and i want to find out what the average angle given three points. Please see this image for an example: example here

I can get the angles for the points easily, so i know the two angles from a->b->c

my efforts so far have given me crazy results, there are certain angles and use cases where things are upside down and all over the place.

Can anybody help?

1

There are 1 best solutions below

1
On BEST ANSWER

Take the angle from $A$ to $B$ and add $180$; this is the angle from $B$ to $A$. Call this angle1 and do not worry if it is out of range, that is, is greater than 360 or less than 0.

Average the angle from $B$ to $C$ and angle1; this is the angle of the bisector of $\angle ABC$. (Note that it may be the angle of the "inner" $\angle ABC$ or the "outer" $\angle ABC$, but either way, it is perpendicular to the line segment $N$ in your linked image.) Call this angle2 and do not worry if it is out of range.

Set angle3 = mod(angle2 + 90, 360). This one of the angles of line segment $N$ and is in the range $[0,360)$. The other angle of $N$ is mod(angle3 + 180, 360) and is also in the range.

In C-like pseudo-code (no promises, I haven't compiled C-ish code in years):

double angleN(double angleAB, double angleBC){
  double angle1, angle2, angle3;

  angle1 = angleAB + 180; // angle1 is angle from B to A
  angle2 = (angle1 + angleBC)/2; // angle2 is a bisector of angle ABC
  angle3 = fmod(angle2 + 90, 360); // angle3 is an angle of N
  // If you have some preference for one of the angles of N, or want both of them, modify the next lines appropriately.
  return angle3;  // or 
  // return fmod(angle3 + 180, 360);  // the other angle for N
}

Note that this could be compacted significantly, but I have no reason to believe a vaguely modern compiler can't at least match any "mad hax" I can express via pseudocode.