# How to get the angle between two 2D coordinates?

Lets say I have 2 frames:

Now, Lets say I wanted to make the red frame point towards the green frame like in this image:

Now I accomplished that by just manually setting the rotation property of the red frame, but how would I get the angle that the red frame should be at if I wanted it to point towards the green frame using a script?

1 Like

First you’ll need to find the centre positions of both of the GuiObjects. You can do this by adding half the AbsoluteSize to the AbsolutePosition. Then find the difference between the two GuiObjects by subtracting the X and Y components from the previous calculations.

Once you have a dX (change in X) and dY (change in Y), resort to some trigonometry.

So:

math.tan(theta) = dX/dY

Use the atan2 (or tan inverse) function to rearrange:

math.atan2(dY, dX) = theta (I believe atan2 does it in that order)

And there you have you angle (theta) in radians. You can convert this to degrees by using math.deg(theta)

2 Likes

I’m talking about 2D coordinates, not 3D coordinates. xD.

This is valid for parts in the workspace, but I believe he was referring to using GuiObjects which work in 2D and do not take a CFrame.

1 Like

When you said AbsoluteSize, and AbsolutePosition, Do you mean the AbsoluteSize, and AbsolutePosition of both GUIObjects combined?

Individually:

``````local GuiObject = script.Parent --Imaginary GuiObject
local AbsSize = GuiObject.AbsoluteSize
local AbsPos = GuiObject.AbsolutePosition

local newPositionX = AbsPos.X+(0.5*AbsSize.X)
local newPositionY = AbsPos.Y+(0.5*AbsSize.Y)``````
1 Like

Why do we multiply 0.5 by AbsSize?

If you imagine a line, if you have the position at the very left of the line, but you want the middle. Simply add half of its length. This is the same concept, we’re just doing it on the X and Y to get the centre of the 2D box

1 Like

So it’s kinda like an anchor point?

So one of your Guis was very tall and very thin. If we used the .Position.X and .Y then we would choose our angle from the top left of the Frame, because that’s how Gui position is calculated. This could cause the angle to be incorrect, so we find the very centre of the frame and calculate from that so we get the correct angle.

1 Like

Let’s take a look at this graph:

We can see that O and P are the 2 points that we need to find the difference of. We can get X by getting the distance by subtracting and getting the absolute value.

``````local X = math.abs(P.X - O.X)
local Y = math.abs(P.Y - O.Y)
``````

Now that we have the lengths of the triangle, we need to find the value of θ. To do that, we will utilize the `atan2` function: an extended version of the tangent inverse function. To do this, we will have to input the numbers Y and X. I believe in using `atan2` as opposed to the classic `atan` function due to it utilizing the signs of the numbers to determine the quadrant, and it being able to handle 0 properly. This makes it best in this case.

``````local Theta = math.atan2(Y, X)
``````

Theta (θ) is the the angle from point O to point P, and that is your answer.

6 Likes

I forgot that `Theta` is in radians. To convert it into degrees, simply use the `math.deg` function.

This is a replacement for the `Theta` declaration line.

``````local Theta = math.deg(math.atan2(Y, X))
``````
3 Likes

I removed the

``````math.abs
``````

part and it works thanks