How to calculate the direction of rotation

I have three full coordinates, which are two segments A from B to C. I also found a function that can return the northern bearing of segment AB or BC from -180 to 180. However, it is difficult for me to determine when the car will reach from A to B if he turn right or left to continue C.

+5
source share
6 answers

EDIT: The previous answer was wrong. now it's right

public Direction GetDirection(Point a, Point b, Point c) { double theta1 = GetAngle(a, b); double theta2 = GetAngle(b, c); double delta = NormalizeAngle(theta2 - theta1); if ( delta == 0 ) return Direction.Straight; else if ( delta == Math.PI ) return Direction.Backwards; else if ( delta < Math.PI ) return Direction.Left; else return Direction.Right; } private Double GetAngle(Point p1, Point p2) { Double angleFromXAxis = Math.Atan ((p2.Y - p1.Y ) / (p2.X - p1.X ) ); // where y = m * x + K return p2.X - p1.X < 0 ? m + Math.PI : m ); // The will go to the correct Quadrant } private Double NormalizeAngle(Double angle) { return angle < 0 ? angle + 2 * Math.PI : angle; //This will make sure angle is [0..2PI] } 
+9
source

Edited to fix a problem of more than 180, now also supports U-Turns.

 const int THRESHOLD = 0; Direction TurnLeftOrRight(Point A, Point B, Point C) { int angle = ToAngle(B,C) - ToAngle(A,B); if((angle > THRESHOLD && angle < 180 - THREASHOLD) || angle < -180 - THREASHOLD) return Direction.Right; else if ((angle < 0 - THREASHOLD && angle > -180 + THREASHOLD) || angle > 180 + THREASHOLD) return Direction.Left; else if (angle >= 0 - THREASHOLD && angle <= THREASHOLD) return Direction.Straight else return Direction.UTurn; } 

You can also make tolerances between left and right right, just change the first angle > 0 to angle > 45 , and the second to angle < -45

+3
source

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 ; } 
+3
source

If AB is the ratio of B from A and BC, which is from C from B, the rotation angle is the remainder (BC-AB, 360.0); (assuming degree). If positive, turn right. In your example, the remainder is (BC-AB, 360.0) (271,360) = -89.

0
source

if you take the line = 1 horizontal X axis, moving the sliding action on the horizontal strip in the upper left corner, where there would be a zero beginning, pushing it 25%, i.e. 0.25 lengths, there will be 25%, 0.5 lines Circumference in the point will be equal to 90 degrees 0.5 half circle = 180 degrees with a pin at the top of the circle, moving at the same time, directed by a spindle, catching up with the length moving from above, and at the same time, adjustment with the movement of the length, showing how the speed of the car, which moves along the push of the vertical line with the handle at a certain point ku. Halfway up, which will move to the right, until 1 = 100%, will be at the end. Xasis End on page 4

-1
source

yes Sorry for not mentioning. There will be two horizontal lines, one of which is a bar at the top of the A4 page, going side by side from left and right, and a horizontal bar passing between up and down, pushed by the hand with another sliding panel. works with a line on a horizontal bar that can move the reel below to wind it with the movement you press left and right above the auxiliary bar, where the slider is fixed when moving from left to right or right to left

Do any of you have a mobile phone connected to Whatsapp BRIAN 076 369 7080? For an additional version, to explain something by writing is difficult

BRIAN

-1
source