S
S
Sergey Nozdrin2016-11-24 15:41:33
PHP
Sergey Nozdrin, 2016-11-24 15:41:33

How to calculate the angle in 2 coordinates?

Good afternoon! I'm having trouble solving a small math problem, so I'm asking for the community's help.
So, I have a transport tracking service. For each car on the map, you need to set the rotation angle via transform: rotate(<angle>).
For each car, I have its past X1, Y1 coordinate and current X2, Y2 coordinate.
Question. What is the formula for calculating this angle?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
A
Alexander X, 2016-11-24
@san-x

alpha = arccos (deltaX / deltaY)
PS and "higher mathematics" in the tags to the question - is it marketing? :)
UPD: oyblin, sorry... all the math teachers of my youth, forgive me :/ of
course alpha = atan ( deltaY / deltaX )
but with deltaX=0, alpha = sign(deltaY) * pi/2;

G
GavriKos, 2016-11-24
@GavriKos

Draw it on a piece of paper - and you will understand. In essence, you have a right triangle, where the hypotenuse is a segment from (x1, y1) to (x2, y2), and the legs are projections onto the corresponding axes. Next, find the desired angle from the formulas for the ratio of angles and sides of a right triangle.

X
xmoonlight, 2016-11-24
@xmoonlight

So, I have a transport tracking service.
I can't solve a little math problem
Hand over the didactic material and go learn the lessons!
X = x0 + (x - x0) * cos(a) - (y - y0) * sin(a);
Y = y0 + (y - y0) * cos(a) + (x - x0) * sin(a);

A
abcd0x00, 2016-11-26
@abcd0x00

Here, about the angle between the vectors
, angle(a, b) = arccos((a * b) / (|a| * |b|))
a * b is the dot product (the sum of the products of coordinates)
|a| - the length of the vector (the root of the sum of the squares of its coordinates)

M
Mercury13, 2017-01-09
@Mercury13

As I understand it, you need the angle of the vector (x1,y1)→(x2,y2).
Any school "arch", if they act on the forehead, in a certain range of angles is not defined or unstable.
But this is precisely what most languages ​​have a function for.
atan2(y2 - y1, x2 - x1)

E
Evgeny Kucherenko, 2016-11-24
@evgenyspace

If the task sounds exactly like this, then it comes down to finding the angle between vectors (see the scalar product of vectors):

const getScalarProduct = ([xA, ...restA], [xB, ...restB]) => 
   (restA.length === 0 || restB.length === 0)
      ? xA * xB
      : xA * xB + getScalarProduct(restA, restB)
  
const getSquareModule = ([x, ...rest]) =>
   (rest.length === 0)
      ? x * x
      : x * x + getSquareModule(rest)

const getModule = (A) => Math.sqrt(getSquareModule(A))
  
const getCosOfAngle = (A, B) => getScalarProduct(A, B) / (getModule(A) * getModule(B))

getCosOfAngle([1, 0],[0, 1]) - two-dimensional case, but you can find the angle for any dimension
P.S.: JavaScript code , the getCosOfAngle function returns the cosine of the angle

A
assasinsarni, 2021-01-23
@assasinsarni

It's never too late, possible exceptions are yours.

<?php 
  $x1 = 10;
  $y1 = 10;
  $x2 = 5;
  $y2 = 5;

  $alpha = rad2deg(atan2($y1 > $y2 ? $y1 - $y2 : ($y1 == $y2 ? $y1 : $y2 - $y1), $x1 > $x2 ? $x1 - $x2 : ($x1 == $x2 ? $x1 : $x2 - $x1)));

  echo($alpha)
?>

UPD: In this solution, you can get the angle, if the coordinates change in distance from each other, the solution will be correct.
UPD2: An example, if it's a radar type and the angles are 0 - 180 and 0 - -180:
$alpha = (($y1 > $y2 ? 180 : -180) - rad2deg(atan2($y1 - $y2, $x1 - $x2))) * -1;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question