How to calculate aspect ratio

I’m not trying to find the aspect ratio. (It can be found using X/Y). I’m trying to use the aspect ratio to find the Y value. The only inputs I have are the X Value and Aspect Ratio.

function GetAspectRatioValues(X, OriginalSize)
     local Val = X / OriginalSize
     return Val * OriginalSize
end

This worked fine for a while. But then I started adding the ability to resize the UI. And it all messed up. If this is confusing as questions and I will give you the info you need.

From what I understand you’re trying to get the Y size only of the aspect ratio
You should just have to do:

X/Ratio

Right now you’re doing

X/Ratio * Ratio

Which just returns the X value cause the ratio cancels out

No. I was doing

temp: CurrentX (200) / OrigninalX (150)

Y: Temp (1.333) * CurrentY (185)

Y = 246

In the examples on the first post and this one, the ratio was never present. Im trying to bring the ratio in to replace having to save OriginalX

The aspect ratio also depends on which axis the major axis is. For the major axis being X:

YSize = XSize / AspectRatio

If the aspect ratio uses the Y axis as its major axis, then:

YSize = XSize * AspectRatio

1 Like

Is originalSize a vector2 value?
Either way it’s still just dividing then multiplying, unless you meant the second line of the function to have a different variable, like originalSizeY for example

I found this out right before you posted it. Thanks anyways!

1 Like

If you’re making a function that returns the aspect ratio of a certain 2d rectangle given a Vector2 value, then the function would look like:

function GetAspectRatio(Size)
    return Size.X / Size.Y -- assuming x is the major axis
end

To get a size that fits an aspect ratio given an aspect ratio and a scalar X, you would use a function like this:

function GetAspectSize(XScale, AspectRatio)
    return Vector2.new(X, X / AspectRatio); --returns the x and y sizes respectively, fitting the aspect ratio
end
1 Like