Calculating Largest Possible Rectangle - math

I'm uploading some images to website. They're all sorts of different dimensions. How can I determine the largest 4:3 rectangle that I could get out of that particular image without rotating the image?

If the aspect ratio is less than 4:3, keep the original width and use a height of width*3/4. If the aspect ratio is greater than 4:3, keep the original height and use a width of height*4/3.

IIUC this problem is very similar to the common problem of maximizing an image in a given rectangular area.
Supposing your screen is W * H pixels (with W = 4 * H / 3) and that the image is dx * dy then you can stretch the image to maximize it inside the screen using a scale factor of
sf = min(W / dx, H / dy)
because W / dx would be a scale factor that makes the image the same width of your screen and H / dy would be the scale factor that make it the same height instead.
Taking the minimum of the two will ensure that the image can entirely fit and that no pixel will get outside the screen... taking the maximum instead will ensure that the screen will be completely covered with (part of) the image and could be useful if you are trying to get a wallpaper out of an image.
Once you have the scale factor the formulas needed for centering are easy:
x0 = (W - dx * sf) / 2
y0 = (H - dy * sf) / 2
and you simply need to draw the image scaled by sf starting at position (x0, y0).

First rotate your image so that its aspect ratio is at least 1. Now if your aspect ratio is larger than 4/3, keep the height and crop the width; if the aspect ratio is smaller, keep the width and crop the height.

Related

What should I use for the diameter of the camera rotation around an object

I am using graphics.py to make 3D graphics. Yes I know it is stupid and super difficult but that is what makes it fun. Anyways, I use the center of the screen as the origin point to rotate the camera around and use the window height and width to determine the diameter of the radius that you can go around. When I do this and the window height is different than the width, I get 2 different sized circles for rotating around the X axis and the Y axis. What should I do to get the circles the same size.
Current math:
RadiusY = winHeight/2
RadiusX = winWidth/2
RadiusZ = (RadiusX+RadiusY)/2

Calculate how much smaller a square would have to be to fit after rotated 45 degrees

Background: I'm creating a UI element in Unity and need to rotate it. It's a square, and will be rotated 45 degrees. Since rotation in Unity's UI happens after all sizing and positioning is done, the corners of this square stick outside the maximum area.
So I have a square inside a square. By default it's the same size as the parent square. After rotating 45 degrees, it's too large to fit in the parent square. How do I calculate the percentage by which to scale down the child square before rotating to make it fit inside the parent square after rotating?
A square of edge length a has a diagonal of length d = sqrt(2)*a, because, by Pythagoras theorem, the triangle formed by the diagonal and two of the square's edges satisfies d^2 = a^2 + a^2 = 2*a^2.
Therefore the inner square must be scaled by 1/sqrt(2) which is identical to sin(pi/4) = sin(45°) and cos(pi/4) = cos(45°) and approximately 0.7071....
If the scaling is performed centered at the sqaure's center, it doesn't matter whether rotation happens before or after scaling.
Actually, I just figured it out. The size of the child square needs to be a percentage of the parent square's size, which we can get from the sine (or cosine also works, I think?) of 45 degrees.
sin(45) == 0.70710678118654752440084436210485
To get to that size, I need to reduce my child square's size by the difference between 100% and 70.7106...%.
1 - sin(45) == 0.29289321881345247559915563789515
Since I'm using Unity and controlling the size of my square UI element with anchor positions, I just divided that by 2 and set the min X anchor to 0.146 and the max X anchor to 0.854. With an Aspect Ratio Fitter component set to Width Controls Height and a ratio of 1, this causes my child square to always fit perfectly inside my parent square even when rotated.

Calculate new height from width and aspect ratio

I have an image that is 1368*1026
I have calculated the aspect ratio by doing 1368 / 1026 = 0.8746355685131195
I want the image to be 500 pixels wide, how do I use the aspect ratio to calculate the height?
w=1368 and h=1026, so r=w/h. Let's say w2 and h2 is what you're looking for, with w2=500. We also have w2/h2=r, so, 500/h2=r. We can re-arrange and have h2=500/r. With that I get h2=375.
PS: 1368/1026 is 1,33... not 0.87....

Calculate Max Width and Height

I need to calculate the width and height of X number of elements based on the overall width and height of the screen in pixels so I can write a function. The individual elements x and y should be the same for each element.
I know the height and width of the screen but the number of elements could vary.
For example, if I have 20 elements/buttons, I want them to take up the whole screen and likewise, if I have 10 elements/buttons, I want them to take up the whole screen.
Example: Screen is 628x1289 and I have 20 elements. How can I calculate the size of each element?
This is similar to web responsive but it's not for a web application so I need to figure out how to calculate it using the pixels.
I hope that makes sense but please let me know if it doesn't.
Steve
P.S. This seems relevant but I'm no math expert so have no idea exactly what they're saying...
https://en.wikipedia.org/wiki/Knapsack_problem
Seems like they already know the size of the objects which is the opposite to my needs. I have a space and want to know what x and y I need to set each object to fit the space.
There are too many free parameters. Make some restrictions to solve the task.
Example: Let's element proportions (width/height ratio) are equal to screen proportions
C = ScreenWidth / ScreenHeight
Now let's unknown element height is h, so width is w = C * h
So every row may contain ScreenWidth div (C * h), every column ScreenHeight div h, where div is integer division.
Now you can find solution (max h value) of inequality
numberX <= (ScreenWidth div (C * h)) * (ScreenHeight div h)
and get height and width of numberX elements filling the screen (last row might be incomplete)

HLSL: Keep Getting An Oval When I Want a Circle! (Pixel Shader)

I'm trying to tint a circle around the player in my 2D side scroller but I keep getting an oval! Here's the part of the code I'm using that matters:
if(length(abs(coords - playerCoords)) < .1)
{
color = color *float4(1,0,1,1);
}
return color;
My screen size is 1280 wide x 720 tall. I know that this is the reason for the distortion, but I don't know enough about my issue in order to come up with or find a solution. Can someone explain to me how to compensate for the screen stretch?
Thanks!
-ATD
multiply the "abs()" term by "float2((720./1280.),1.0)" -- or whatever your y/x aspect ration might be
The coords you are using are normalized in 0-1 space, so just correct them

Resources