The problem is this:

It is necessary to find the angle between the points X, Y.

For example, there are points:

Coordinates of the car:

x: 1141.513916
y: -1162.998169

The coordinates of the point where you need to come to her:

x: 1162.112061
y: -1199.149658

It is necessary to somehow calculate the angle between the machine and the point.

There must be something like this:

if (angel < 5){ Turn_Car_Left(); } if (angel == 5){ Ride_Forward(); } if (angel > 5){ Turn_Car_Right(); } 
  • 7
    Angle between two points? = / - Suvitruf
  • @Suvitruf, yes. Like .. To know where to turn the wheels of a car - CrazyProgrammist
  • eight
    only a straight line can be drawn between two points. the straight has no corners. what angle would be required 3 points - Mike
  • 2
    Considering that this is a turn of the wheels, I suspect that an angle is needed between the current direction of the vehicle and the required point, but then it is required to know the current direction of the vehicle. either in the form of 2 points or in the form of an angle with respect to something, for example, one of the coordinate axes - Mike
  • one
    namely, the angle between the motion vector of the machine and two points. type gender solution here ru.wikihow.com/… - vusaldev

2 answers 2

Introduction

So, it is worth starting with the fact that you set an incorrect condition, since the angle -

a geometric figure formed by two rays (sides of the corner), emerging from a single point (which is called the top of the corner).

In turn, the beam -

the part of the line consisting of the given point and all points lying on one side of it. Any point on the line divides the line into two beams.

In turn, one single straight line passes through 2 points => to build the angle, parts of 2 intersecting straight lines are required (with one common point) => 2 * 2 - 1 = 3 points

Thus, we get an obvious fact for all: there can be no between two points of any angle


A bit of theory

Let us briefly depart from the explanation of the geometry for the N class of the secondary school and still try to guess what you need

As I understand it, you are modeling the movement of the machine in the xOy plane. As the car moves, it has some vector that characterizes its movement.

I suppose that the car left the point (0; 0) => if its current coordinates are (x; y) , then the displacement vector is {x - 0; y - 0; } = {x; y; }

However, since you need to find the angle to rotate the car, you should use its speed vector, but you are deprived of information about it, so I assume that it is in line with the displacement vector

So. At this step, we have a vector and a point , total: 3 points . To calculate the angle is more than enough

Next, we find the direction vector from the origin to the required point and find the smallest angle between the two existing vectors ( a and b ) using the formula:

 cos(α) = (a * b) / (|a| * |b|) 

Example

Let's try an example:

Let the machine be located at the point (1; 2.5) , and the destination - at the point (3; 3) :

example

a = {1; 2.5}
b = {3; 3}

 cos(α) = (1*3 + 2.5*3) / (sqrt(1*1 + 2.5*2.5) * sqrt(3*3 + 3*3)) ≈ 0.91914503001 

=>

 α = arccos(0.91914503001) ≈ 0.404891786 rad ≈ 23.1985905 deg 

So we got the coveted angle, which is approximately equal to 23 degrees

On this course, the geometry is over, go to the software implementation


Implementation

Sketch this function:

 private static double GetAngle(Point Machine, Point Destination) { // Получим косинус угла по формуле double cos = Math.Round((Machine.X * Destination.X + Machine.Y * Destination.Y) / (Math.Sqrt(Machine.X * Machine.X + Machine.Y * Machine.Y) * Math.Sqrt(Destination.X * Destination.X + Destination.Y * Destination.Y)), 9); // Вернем arccos полученного значения (в радианах!) return Math.Acos(cos); } 

Judging by the values ​​in your example, which are clearly greater than one, you use not radian , but degree measure, and therefore, the value that the function returns to you will need to be converted by the formula:

 dAngle = rAngle * 180 / Pi 

That is so:

 // Переведем угол в градусы private static double ToDegrees(double Angle) => Angle * 180 / Math.PI; 

Test:

Let the machine be located at the point (-3; -3) , and the destination - at the point (3; 3) :

example2

Find the angle:

 Console.WriteLine(ToDegrees(GetAngle(new Point(-3, -3), new Point(3, 3)))); // 180 

180 degrees , which is obviously the purest truth!


Results

Try not to forget that programming consists not only of typing, but also of applying knowledge of a certain subject area with which you come into contact within the project.

Don't you know something? Read and learn as much as you can!

And yes, I emphasize that the above method will work only if your machine is straight away from the origin (i.e., the displacement vectors and velocities are co-directed), but if the machine turns around and goes to the point (0; 0), everything will break !
To solve a problem, you need to know which way the car is moving. I do not know the details of your implementation, so I can suggest to cache the previous point where the car was, and then move it to a new one. Thus, you can easily find the velocity vector of the machine at any time and apply it in the algorithm described above.

  • Overly grateful to you! Yes, you are right, I made a mistake when I wrote "finding the corner" - CrazyProgrammist
  • @NoName_as_Null if you need, I can sketch an example of implementation - Kir_Antipov
  • if it's not difficult for you - sketch, please) - CrazyProgrammist
  • hmm .. There is an interesting story. The bottom line is that when away from the checkpoint - GetAngel starts to increase. That is, it turns out that I cannot pick up a condition in which the car needs to be turned in one direction or another, because with different distances from the checkpoint (even if you put the car under 90 degrees to the checkpoint) - GetAngel is different. The machine can stand straight nose with a checkpoint, but at the same time GetAngel will not give out "90", for example, but some other values. How to deal with this? :( It is necessary to precisely align the car with the checkpoint so that it gets there. - CrazyProgrammist
  • @NoName_as_Null that’s why the speed vector is needed) Tomorrow I’ll show you how it looks like with a simple PictureBox example in WinForms) - Kir_Antipov

I believe that it is given:

 S: точка, координаты машины D: вектор направления машины T: целевая точка 

Then the target vector

 V = T - S //или в компонентах: VX = TX - SX VY = TY - SY 

Angle between current machine direction and target vector

 A = atan2(D x V, D * V) //векторное и скалярное произведение векторов //или в компонентах: A = atan2(DX * VY - DY * VX, DX * VX + DY * VY) 

The result is an angle in radians in the -Pi..Pi range, which the car should turn to, so that the instantaneous vector of its direction points to the target.

The sign of the angle corresponds to the left or right turn.

(Physics (inertia, etc.), as I understand it, do not take into account)