[Math] How to find heading angle to an object whose $x,y$ coordinates are known

trianglestrigonometry

Scenario: I have a map with a marked location on it. I know my $x,y$ coordinates on the map (top left corner is $0,0$), my distance from that marked location, my heading angle relative to true north ($0^\circ$ is north, $90^\circ$ is east, $180^\circ \rm S$, and $270^\circ \rm W$), and my bearing (bearing here meaning the angle I have to turn in order to be facing the marked location.

Imagine a half-circle sonar with $0^\circ$ on left, a ping of $35^\circ$ means I turn that angle to face the ping). I also know the $x,y$ coordinates of a point on the map I'm trying to get to, but I need to find out my distance to that point, and the heading I should be at to go straight towards that point.

Given

  • 3 points: my location, marked location, location I'm trying to get to.
  • distance from my location to marked location
  • My heading and the bearing from my location to the marked location

Trying to find

  • distance from my location to location I'm trying to get to
  • heading I should have to reach the location I'm trying to get to

In the poorly drawn image below: $\color{red}{\text{Red dot}}$ is my location. $\text{Black dot}$ is marked known location. $\text{Black line}$ is distance between red and black dot. Small Orange line is my heading ($45^\circ$ let's say). My bearing to the $\text{black dot}$ is $35^\circ$. I need to find the distance from the $\color{red}{\text{red dot}}$ to the $\color{green}{\text{green dot}}$, and the angle from the $\color{red}{\text{Red dot}}$'s heading (orange line) to the $\color{green}{\text{green dot}}$ (dotted line).

enter image description here

So, finding the distance should just be the normal distance formula right? However, the angle part is confusing me. I've been banging my head against the desk trying to figure this out. Would I have to make a right triangle using the $\color{red}{\text{red dot}}$, $\color{green}{\text{green dot}}$, and East ($90^\circ$)?

Best Answer

You know red and green dots locations respectively as the present position and target points: $$ (x_1,y_1),(x_2,y_2)$$ Distance between them: $$ d= \sqrt{(y_2-y_1)^2+(x_2-x_1)^2}$$ Use $atan2$ to find direction (automatically sensitive to signs of quadrant you are in, and the target location) $$ \theta_{relative}= atan2 \dfrac{y_2-y_1}{x_2-x_1}$$ then extra pinging angle: $$ \theta_{Orange Heading}(35^{\circ}) + 90^{\circ}-\theta_{relative} $$

Please check the above, because trigonometrical angles are measured counterclockwise and the heading rotation you gave are clockwise.

Related Question