
Algebra PreAlgebra and Basic Algebra Math Forum 
 LinkBack  Thread Tools  Display Modes 
January 19th, 2011, 03:43 PM  #1 
Newbie Joined: Jan 2011 Posts: 2 Thanks: 0  How to calc sides of rectangle inside a rotated rectangle
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 have added an image for clarification. [attachment=0:6ih0xzbi]maxRectRotated.gif[/attachment:6ih0xzbi] 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 Any help would be greatly appreciated. Thanks, Mark 
January 19th, 2011, 10:35 PM  #2 
Global Moderator Joined: Dec 2006 Posts: 20,746 Thanks: 2133 
How do you know that the point (Px, Py) is inside the outer rectangle?

January 20th, 2011, 06:55 AM  #3 
Newbie Joined: Jan 2011 Posts: 2 Thanks: 0  Re: How to calc sides of rectangle inside a rotated rectangl
Hello Skipjack, Thank you for your reply. The point Px,Py is defined to be a point inside the outer rectangle and will be constrained that way by the user interface. In fact, it is my intention to use the formula to implement that constraint among other uses. Basically, the inner rectangle is a fixed orientation crop window, the outer rectangle is an image the user can freely rotate and drag. But since the crop window must always fully cover image I want the crop to autoresize as the image is dragged further away or is rotated. So, in essence Px,Py is really (0,0) and it is the image center (X,Y) coordinates that change but for the purpose of discussion I define the image center (X,Y) to be (0,0) and the crop window center as (Px,Py). I have scoured the internet for a solution to this problem but alas. I would greatly appreciate any insights you can offer. Thanks, Mark 
February 7th, 2011, 10:28 AM  #4 
Newbie Joined: Feb 2011 Posts: 2 Thanks: 0  Re: How to calc sides of rectangle inside a rotated rectangl
Hi Mark I've been working on the exact same thing lately, and have also been all over the internet looking for a solution, and i actually found it. So i might as well share with you Code: Shape.prototype.withinBounds = function(vector) { var v0 = vector, j = this.points.length1, res = [], v1, v2, test, max; for (var i = 0, l = j+1; i < l; i++) { v1 = this.points[i]; v2 = this.points[j]; if (v1 && v2) { test = (v1.x * v2.y  v1.y * v2.x  v0.x * v2.y + v0.y * v2.x + v0.x * v1.y  v0.y * v1.x); res.push(~~test); } j = i; } max = Math.max.apply(Math, res); return max < 0; }; My shape object has an array of vectors, in the points array, 4 points, one for each corner, starting from the top left. Its a shape, because it can hold any number of points, and this will still work. Also, when rotated. I have a Rect class that inherits this stuff. This wont give you the intersection points, only if the point/vector is inside the shape/rectangle. Hope you can use it Regards, Jeppe 
February 7th, 2011, 10:40 AM  #5 
Newbie Joined: Feb 2011 Posts: 2 Thanks: 0  Re: How to calc sides of rectangle inside a rotated rectangl
Oh, and if you want to see the rest of my code, just let me know.


Tags 
calc, inside, rectangle, rotated, sides 
Search tags for this page 
rotated rectangle inside rectangle,formula for rectangle inscribed in rectangle,calculator retangle inside of a rectangle
Click on a term to search for related topics.

Thread Tools  
Display Modes  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Positioning rotated rectangle  JackRnl  Algebra  6  January 13th, 2013 12:11 PM 
Points of a rotated rectangle  nmrugg  Algebra  12  December 14th, 2010 07:52 PM 
Rotated rectangle solving for corners?  anothermathperson  Algebra  5  February 25th, 2010 08:16 AM 
Getting Largest Rectangle Inside a polygon  chamira  Computer Science  0  May 8th, 2008 09:13 PM 
Sides of a Rectangle  morning_mood  Algebra  4  September 29th, 2007 10:02 AM 