How to calc sides of rectangle inside a rotated rectangle

  • Thread starter mhurkman
  • Start date
  • Tags
    Rectangle
In summary, the conversation discusses the need for a formula to calculate the sides of the largest possible rectangle inside a rotated rectangle for image editing software. The solution involves finding the intersection points of two lines and implementing the formula in the programming. The conversation also mentions potential bugs and the need to continually recalculate the crop window as the user drags and rotates the image.
  • #1
mhurkman
3
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: 850
Mathematics news on Phys.org
  • #2
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: 790
Last edited:
  • #3
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
 
  • #4
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.
 
  • #5
Thanks, I will be implementing this over the weekend. Will let you know how it turns out.
Cheers,
Mark
 
  • #6
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.
 

1. How do I find the length and width of a rectangle inside a rotated rectangle?

To find the length and width of a rectangle inside a rotated rectangle, you can use the Pythagorean theorem. First, calculate the diagonal length of the rotated rectangle by using the formula a^2 + b^2 = c^2, where a and b are the length and width of the rotated rectangle, and c is the diagonal length. Then, use the diagonal length as the hypotenuse and one of the sides of the inner rectangle as the adjacent side to calculate the length and width of the inner rectangle.

2. Can I use trigonometric functions to calculate the sides of a rectangle inside a rotated rectangle?

Yes, you can use trigonometric functions such as sine, cosine, and tangent to calculate the sides of a rectangle inside a rotated rectangle. However, this method may be more complex compared to using the Pythagorean theorem.

3. Is there a formula for finding the sides of a rectangle inside a rotated rectangle for any given rotation angle?

Yes, there is a general formula for finding the sides of a rectangle inside a rotated rectangle for any given rotation angle. It involves using basic trigonometric functions and the coordinates of the vertices of the rotated rectangle. You can find this formula by searching online or in geometry textbooks.

4. Can I use the same method to find the sides of a rectangle inside a rotated square?

Yes, you can use the same method to find the sides of a rectangle inside a rotated square. The only difference is that the rotated square has all equal sides, so you only need to find one side length and then use it to calculate the other sides.

5. Are there any online calculators or tools that can help me find the sides of a rectangle inside a rotated rectangle?

Yes, there are many online calculators and tools available that can help you find the sides of a rectangle inside a rotated rectangle. Some of these tools may also provide visual representations of the calculation process to help you better understand the concept. However, it is always recommended to understand the formula and calculations behind the tool rather than solely relying on it.

Similar threads

Replies
8
Views
4K
  • Calculus and Beyond Homework Help
Replies
3
Views
4K
  • Precalculus Mathematics Homework Help
Replies
8
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
3K
Replies
1
Views
525
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Replies
5
Views
4K
  • Precalculus Mathematics Homework Help
Replies
3
Views
3K
Replies
1
Views
3K
Back
Top