How to find a distance between 2 UDim2s

I’m trying to make a chart generator thing and I need to know how to find a distance between two UDim2s. (something like this:)


(i made this in MS paint please dont judge)

I suck at math so someone please help me

Distance = math.sqrt((x1-x2)^2 + (y1-y2)^2)

1 Like

To get the distance, you would do:

local frameSize = frame.AbsoluteSize -- the frame that your UDim2s are relative to
local p1 = UDim2.fromScale(0.124, 0.234) -- your first UDim2
local p2 = UDim2.fromScale(0.837, 0.654) -- your second UDim2

local p1Vector2 = Vector2.new(p1.X.Scale, p1.Y.Scale)
local p2Vector2 = Vector2.new(p2.X.Scale, p2.Y.Scale)

local displacementScale = p2Vector2-p1Vector2 -- displacement in scale
local displacementPixels = distanceScale*frameSize -- disaplcement in pixels

local distanceScale = displacementScale.Magnitude -- distance in scale
local distancePixels = displacementPixels.Magnitude -- distance in pixels

The displacementScale and displacementPixels variables are both Vector2s, so you just have to convert them back into UDim2 by using UDim2.fromOffset or UDim2.fromScale. To get the magnitude of the distance in scale or offset, you can just do displacementScale.Magnitude or displacementPixels.Magnitude to get the distance number instead of a distance displacement.

if you are using GUI elements you can use AbsolutePosition to get the magnitude between them with something like

(ImageLabel1.AbsolutePosition - ImageLabel2.AbsolutePosition).Magnitude
1 Like