Let me try to give you something somewhere between The Light Spark's answer and Elliot's answer, because from what I read, you're really looking for an algorithm to follow and not just math tossed at you.
Problem Statement: Given that you have a location A (50, 50)
and a heading (since you didn't provide one, I'll assert it as y = 2 * x + 25
), find where B (80, 90)
is relative to A
and the heading.
What you want to do is actually fairly straightforward.
1) Relocate A
to the origin of your system. This is simply means that the local-to-A
values are going to be the global position values minus the global position values of A
. A
becomes (0, 0)
and B
becomes (30, 40)
.
1.1) The heading also needs to be moved. This is a actually very easy to do, because the y-intercept in local-to-A
terms is always 0, and the slope won't change, so we have y = 2 * x
as the heading.
2) Now we need to align the prior heading to the X axis. So, how do we do this? The easiest way, conceptually to do this is to convert from x, y co-ordinates to a polar co-ordinate system. Polar co-ordinate system involves R
, the distance to a location, and phi
, an angle of rotation from the x-axis. R
is defined as sqrt(x^2 + y^2)
and phi
is defined as atan(y / x)
. Most computer languages these days go ahead and define a atan2(y, x)
function which does the exact same thing as atan(y/x)
but does so in such a way that the output tends to be from -180 degrees to 180 degrees rather than 0 degrees to 360 degrees, but either work.
B
thus becomes R = sqrt(30^2 + 40^2) = sqrt(2500) = 50
, and phi = atan2(40, 30) = 53.13
in degrees.
Similarly, the heading now changes. This is a bit tricky to explain, but it because the heading, by definition, always passes through our origin A
, we don't need to be worried about the R
component. Headings are always going to be in the form of phi = C
where C
is a constant. In this case, phi = atan(2 * x / x) = atan(2) = 63.435
degrees.
Now, we can rotate the system to move the heading to the X-axis of the local-to-A
system. Much like when we moved A
to the origin of the system, all we have to do is subtract the phi
of the heading from all phi
values in the system. So the phi
of B
becomes 53.13 - 63.435 = -10.305
degrees.
Finally, we have to convert back out of polar co-ordinates into x, y co-ordinates. The formula to do that transformation are X = R * cos(phi)
and Y = R * sin(phi)
. For B
therefore, we get X = 50 * cos(-10.305) = 49.2
and Y = 50 * sin(-10.305) = 8.9
, so B
in local-to-A
co-ordinates is close to (49,9)
.
Hopefully that helps, and is light enough on the math for you to follow.