I think your life will be easier if you use cross-product vector .
Although strictly speaking, cross product is defined only for three-dimensional vectors, for 2D vectors p = (px, py) and q = (qx, qy), you can think about your cross product p & times; q , like pxqy-pyqx. This last number will be positive if p is clockwise from q , and negative if p is counterclockwise from q . It will be zero if p and q are parallel - that is, a point in the same or opposite direction.
In your case you use (lat, lon). The equivalent in coordinates (x, y) is (-lon, lat), so if you have two vectors (lat1, lon1) and (lat2, lon2), you want to calculate (-lon1, lat1) & times; (- lon2, lat2), which goes to lat1 * lon2-lon1 * lat2.
If this number is zero, you can use dot product to determine if the direction is straight or a turn.
Thus, your code may look like this, assuming that the dots and vectors are written in the form (lat, lon) (the code will be slightly different if they are in x and y):
public Direction GetTurnDirection(Point A, Point B, Point C) { Vector v1 = B - A ; Vector v2 = C - B ; double cross = v1.lat*v2.lon - v1.lon*v2.lat ; if (cross > 0) { return Direction.Left ; } if (cross < 0) { return Direction.Right ; } double dot = v1.lat*v2.lat + v1.lon*v2.lon ; if (dot > 0) { return Direction.Straight ; } return Direction.UTurn ; }
source share