How to calc sides of rectangle inside a rotated rectangle

  • Thread starter Thread starter mhurkman
  • Start date Start date
  • Tags Tags
    Rectangle
AI Thread Summary
The discussion focuses on calculating the dimensions of the largest rectangle that can fit inside a rotated rectangle for a crop function in image editing software. The user, Mark, seeks assistance with trigonometric formulas to determine the crop area based on the center coordinates, aspect ratio, and rotation angle of the enclosing rectangle. A suggested method involves using equations for the lines representing the crop area and the edges of the photo to find intersection points, which can then be used to calculate the crop dimensions. The importance of recalculating the crop window as the user rotates and drags the image is emphasized to avoid issues with the crop area going outside the image boundaries. The conversation highlights the complexity of the task and the need for careful programming to handle potential errors.
mhurkman
Messages
3
Reaction score
0
I'm hoping for some help as it has been 20 years since I last used trigonometry and Google has come up empty. I'm programming simple image editing software and for a crop function I need a formula to calculate the sides of the largest possible rectangle inside a rotated rectangle, given the coordinates of the center of the inner rectangle and aspect ratio of the sides. Largest is defined as the largest rectangle that touches but not exceeds any edge of the outer rectangle.

I know the length of the sides of the enclosing rectangle, L and W as well as angle of rotation, theta.
I also know the center coordinate of the inner rectangle (Px,Py) relative to the center of the enclosing rectangle as well as aspect ratio of the sides, p and q. Ie. p = r * q

I need this because the user is to freely rotate and drag the image (larger rectangle) and the crop window(smaller rectangle) must auto-adjust because the crop window of course cannot fall outside the image. Any help would be greatly appreciated.

Thanks,
Mark
 

Attachments

  • maxRectRotated.gif
    maxRectRotated.gif
    7.1 KB · Views: 943
Mathematics news on Phys.org
The solution is a little complicated and you might save yourself a lot of trouble by de-selecting any selected crop area when the user rotates the photograph and letting the user re-select the crop area once he has the desired rotation. One thing to consider is that if the crop area is in a corner before the rotation, its centre might be completely outside the photo boundary after a rotation. A lot of room for unexpected bugs in the programming here.

If you are determined to proceed, then here is a starter. First consider an imaginary line that passes diagonally through the crop area. (This is the red line R-R' in the attached diagram.) The equation for this line is:

y=Py-q/p*(x-Px)

The objective is to find where this line intersects the side of the photo. The side considered here is the lower edge which illustrated by the green line G-G' in the attached diagram. The equation for this line is:

y=(x-W/2*\sin(a))*tan(a)-W/2*cos(a)

At the intersection the above two equations are equal and can be solved for x to give:

x=\frac{Py+q/p*Px+W/2*(sin(a)*tan(a)+cos(a))}{tan(a)+q/p}

and the corresponding y value at the intersection is found by inserting this value for x into the equation for y above it. Now it is just a case of using Pythagorous to determine the distance from the intersect to the centre of the cropped area. You will have to repeat this process for all the other sides and find the intersection that is nearest to the centre of the cropped area and use this smallest value to determine the size of the cropped area. You will also have to check for the case when the intersections of the lines do not occur because they are parallel, before the calculation stage to avoid divide by zero errors.

Hope that helps.
 

Attachments

  • Rectangle.gif
    Rectangle.gif
    14.1 KB · Views: 858
Last edited:
Hello Yuiop,

Thank you so much for your reply. I understand your method, a clever approach. I see your point with Px,Py falling outside of the image area but since I will be recalculating the crop window continually as the user drags and rotates the image it should not happen.

Thanks a lot!
Mark
 
mhurkman said:
Hello Yuiop,

Thank you so much for your reply. I understand your method, a clever approach. I see your point with Px,Py falling outside of the image area but since I will be recalculating the crop window continually as the user drags and rotates the image it should not happen.

Thanks a lot!
Mark
You are very welcome :smile:

I have edited my post because I accidentally swapped over the equations for the two lines but that is fixed now. You probably figured that out from the variables used. Other than that, the equations should be good. I tested them out in some plotting software and the line for the edge of the photograph rotates correctly when theta is adjusted and the intersection point of the two lines is correctly predicted.
 
Thanks, I will be implementing this over the weekend. Will let you know how it turns out.
Cheers,
Mark
 
This looks like finding out the rectangular region after rotating the rectangle? If that is the case, could you please elaborate more on the values with some examples? It would be really good help if you can paste the implementation here.

Thanks,
Veeru.
 
Seemingly by some mathematical coincidence, a hexagon of sides 2,2,7,7, 11, and 11 can be inscribed in a circle of radius 7. The other day I saw a math problem on line, which they said came from a Polish Olympiad, where you compute the length x of the 3rd side which is the same as the radius, so that the sides of length 2,x, and 11 are inscribed on the arc of a semi-circle. The law of cosines applied twice gives the answer for x of exactly 7, but the arithmetic is so complex that the...
Is it possible to arrange six pencils such that each one touches the other five? If so, how? This is an adaption of a Martin Gardner puzzle only I changed it from cigarettes to pencils and left out the clues because PF folks don’t need clues. From the book “My Best Mathematical and Logic Puzzles”. Dover, 1994.
Back
Top