How to calc sides of rectangle inside a rotated rectangle

  • Context: Undergrad 
  • Thread starter Thread starter mhurkman
  • Start date Start date
  • Tags Tags
    Rectangle
Click For Summary

Discussion Overview

The discussion revolves around calculating the dimensions of the largest possible rectangle that can fit inside a rotated rectangle, specifically for a crop function in image editing software. Participants explore mathematical approaches and considerations related to the geometry of the rectangles involved.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Mark seeks assistance with a formula to determine the sides of a rectangle that fits within a rotated rectangle, given specific parameters such as the center coordinates and aspect ratio.
  • Yuiop suggests a method involving the intersection of lines representing the crop area and the edges of the outer rectangle, providing equations for these lines and the process to find the intersection points.
  • Mark acknowledges Yuiop's approach and expresses confidence in recalculating the crop window to avoid issues with the center falling outside the image area.
  • Yuiop corrects an earlier mistake in the equations provided and confirms that the equations function correctly when tested with varying angles of rotation.
  • Veeru asks for further clarification and examples regarding the implementation of the proposed solution.

Areas of Agreement / Disagreement

Participants generally agree on the approach suggested by Yuiop, but there is no consensus on the implementation details or potential pitfalls, as some concerns about the crop area falling outside the image remain unresolved.

Contextual Notes

There are unresolved aspects regarding the handling of edge cases, such as when the intersection points do not occur due to parallel lines, and the specific implementation details of the proposed equations.

Who May Find This Useful

Individuals interested in image processing, geometric calculations, or programming related to graphical user interfaces may find this discussion relevant.

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: 970
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:

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

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:

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

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

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

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: 887
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.
 

Similar threads

Replies
8
Views
6K
Replies
8
Views
3K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
8
Views
12K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
1
Views
3K