Question

Math - get bounds of rotated rectangle

Asked by: Slick812

Hello, , I am rotating a Rectangle (90 degree corners) to an angle from zero to 360 degrees, for a computer programming graphics operation, I'll call this the Rotated Rectangle.
I need to find the width and height of a "Bounds" rectangle around the rotated rectangle (containing all), , this Bounds Rectangle will have sides that are parralell to the x and y axis and will have it's width and height equal to the Rotated Rectangle at 0 degrees and 180 degrees.
The data that I start with is the width and height of the Rotated Rectangle and the Angle of Rotation, what I need is a formular or way to get the Width and Height of the Bounds Rect, after the rotation.
Lets say "Ang" is the angle of rotation in degrees, and the rrWidth and rrHeight is the Width and Height of the Rotated Rectangle, so bWidth and bHeight are the results I need, as the width and height of the Bounds Rectangle. . .

so I need some math like

bWidth = Cos Ang x rrWidth
bHeight = Sin Ang x rrHeight

but incorperating the the sides of the Rotated Rectangle to get a container size for the rotated rectangle

a picture may help to see the spatial relations, look at -

http://www.angelfire.com/hi5/delphizeus/rotate.html

I can get some "Partial" solutions, for a single quadrent of the rotation, but since the corners of the rotated rect change their left and right and upper and lower reference, I can not seem to get a good way to do this

any help will be apreciated !

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-06-17 at 12:22:23ID21029386
Tags

rectangle

,

rotated

Topic

Math & Science

Participating Experts
4
Points
328
Comments
16

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Rotation
    When we rotate an object about an axiz parallel to co-ordinate axis the original co-ordinates of object changes slightly (where they should not) . How we can solve this problem.
  2. Rotate a Line object... need help with math
    I am trying to solve this problem. I am working in VB. I want to Rotate a line XDegrees. The X1, Y1 is the rotation point. I want to give an angle between 0-360; + or - . then I want to refresh the line. Since that X1, Y1 are the rotation points logically they stay the sam...
  3. Math (rotation)
    Hello, I need a formula for finding where a pixel would be if it were to be rotated at any given angle. Also, I need with that an explanation that will explain how/why it works, as clear as you can, so that it can be understood by someone with low math skills. It may be expla...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: grg99Posted on 2004-06-17 at 12:32:07ID: 11338097

Think of it this way: the width is going to be the cosine of the diagonal's angle, the height is the sine.

The diagonal's starting angle is arctan( height / width ).

That's about it!

 

by: Gregory_Scot_GreerPosted on 2004-06-17 at 12:46:22ID: 11338259

Hi Slick812,

bWidth = fabs( Cos( Ang ) *  rrWidth ) + fabs( Sin( Ang ) * rrHeight )
bHeight = fabs( Cos( Ang ) * rrHeight ) + fabs( Sin( Ang ) * rrWidth )

Test this out and I think you will find that this is the solution that you are looking for.

P.S.  fabs stands for floating point absolute value if you are using C++.  * is multiplication.

 

by: Slick812Posted on 2004-06-17 at 12:47:51ID: 11338271

I tried that many times and it is good for the width in the upper right quadrent, however it is not good for the bounds height, at least in the ways that I have tried it, I also got the angle of the Right side of the rotated rect and tried to get that into the formula, but I lost it as it went into a diferent quadrent

 

by: Slick812Posted on 2004-06-17 at 12:49:45ID: 11338302

comment above was for grg99

 

by: grg99Posted on 2004-06-17 at 14:10:02ID: 11339186

Maybe you just need to take the absolute value   fabs()   of the result?

 

by: Slick812Posted on 2004-06-17 at 14:10:39ID: 11339196

Gregory_Scot_Greer, thanks, your formulas seem to work, I will have time to fully test them later for all quadrents
also, I meant to ask, but forgot
would you know a way (formula) to get an "Offset" to paint this rotated rect onto the bounding rect (bitmap),
at 0 degrees it would be x = 0 and y = 0
at 90 degrees it would be x = 0 and y = rrWidth
at 180 degrees it would be x = rrWidth and y = -rrHeight
??

 

by: Slick812Posted on 2004-06-17 at 14:15:26ID: 11339237

grg99, no it does not follow directly any value for that diagonal, that I could see, absolute or not, , especially for the height

 

by: _TAD_Posted on 2004-06-17 at 14:30:14ID: 11339353



If this is a computer program then the easiest way is to simply do a matrix transformation in order to find the corner points.  Then it's just a matter of using the appropriate corner points as end points to your bounding rectangle.

corners: (1,2,3,4)
equate to
bounding rec: (top, left, bottom, right)

 

by: _TAD_Posted on 2004-06-17 at 14:33:10ID: 11339374


To be more conceptual, the LEFT side of the bounding rectangle is the Min(X) value of the rotated rectangle.

the bottom side of the bounding rectangle is the Min(Y) value of the rotated rectangle.

Top and right are just the Max vaules

 

by: Gregory_Scot_GreerPosted on 2004-06-17 at 15:37:00ID: 11339723

Sorry Slick812,

  You have not supplied enough information for your second question, so I can only give you a general answer.  If you want absolute coordinate mapping on the bounding rectangle, you will have to specify which point the rotating rectangle is rotating about.  If you want relative coordinates, it would require that the formula change as you rotate to other quadrants i.e.
the lower left corner of the rotating rectangle would be found at:
When you are rotating for 0 to 90:

bX = Sin( Ang ) * rrHeight
bY = 0;

From 90 to 180, the point would be:

bX = ( Sin( Ang ) * rrHeight ) - ( Cos( Ang ) * rrWidth )
bY = -Cos( Ang ) * rrHeight

From 180 to 270, the point would be:

bX = -( Cos( Ang ) * rrWidth )
bY = -( Sin( Ang ) * rrWidth ) - ( Cos( Ang ) * rrHeight )

From 270 to 360, the point would be:

bX = 0
bY = -( Sin( Ang ) * rrWidth )

If you could give a better description of the mapping that you are attempting to make, I could give you a better formula.

 

by: Slick812Posted on 2004-06-17 at 20:53:49ID: 11341093

Gregory_Scot_Greer , , I have given that info in the GIF image, the x = 0 and y = 0 , is the rotational center for the rotated rectangle, the x = 0 and  the y = 0 is the Top Left cormer of the rotated rect, , and the TOP line of the rotated rect is "Angle" measurement, which is the x axis at degree = 0

it seems that your attempt may do the job?
to late tonight for me to run it, I'll set it up tomorrow, thanks

 

by: Slick812Posted on 2004-06-17 at 21:00:18ID: 11341200

_TAD_ , I have used that type of calculation before, but in this method, I Do NOT have the rotated rectangle's "corner" co-ordinates after the rotation,
The data that I start with is the width and height of the Rotated Rectangle and the Angle of Rotation, so I would have to calculate the four corner x and y co-ordinates and then do a min and max from that, I do not need the co-ordinates, so I would rather not figure them if possible

 

by: Gregory_Scot_GreerPosted on 2004-06-17 at 22:56:58ID: 11341684

Slick812,

  Sorry.  I see that you did supply enough information.  The new coordinates after rotation are:

bX = ( ( Cos( Ang ) *  rrX ) - ( ( Sin( Ang ) * rrY )
bY = ( ( Cos( Ang ) * rrY ) + ( ( Sin( Ang ) * rrX )

Hope this helps

 

by: Slick812Posted on 2004-06-18 at 12:19:49ID: 11347681

OK, Gregory_Scot_Greer, I tried both your formulas (From 90 to 180, the point would be, , , , and the last post)     for the OffSet and they Did NOT give me the correct palcement of the rotated Rect on the bitmap, not even close. . .

So I have had it, all I can take with trigonometry, (banging head on monitor) and I am out of time, , so I decided to just let the user drag and drop the rotated rect into the container. . .

I'd like to THANK every one for their time and comments. .

I will give the points to  Gregory_Scot_Greer, since he was the only one to post a formula, and I am using the one he gave for the bounds

 

by: GwynforWebPosted on 2004-06-19 at 08:51:43ID: 11351400

I did not notice this question but the accepted ansewer is not correct, the form of the formula depends on tne angle. I will look later.

 

by: Slick812Posted on 2004-06-19 at 09:14:04ID: 11351491

GwynforWeb, I can offer points for additional Info, but the accepted ansewer, seems to give me the width and height close enough to give me a workable container, but I did not have the time to do the trig and acually check for for acuarcy, if you want to know more about what I need, I use the windows

SetWorldTransform(hDC, XForm1);
to set the transformation for the device context

and the XForm1 is set  like

  XForm1.eM11 :=  Cos1;
  XForm1.eM12 := -Sin1;
  XForm1.eM21 := Sin1;
  XForm1.eM22 := Cos1;
  if Angle < 91 then
    begin
    XForm1.eDx := 0.0;
    XForm1.eDy := Sin1 * Size1.cx;
    end else
    if Angle < 181 then
    begin
    XForm1.eDx := -(Cos1 * Size1.cx);
    XForm1.eDy := -(Cos1 * Size1.cy);
    end else
    if Angle < 271 then
    begin
    XForm1.eDx := -(Cos1 * Size1.cx);
    XForm1.eDy := 0.0;
    end else
    begin
    XForm1.eDy := -(Cos1 * Size1.cx);
    XForm1.eDx := (Cos1 * Size1.cy);
    end;

but this only works in the first rotation quadrent
 if Angle < 91 then

so I do not really use the placement that this provides
The variables Cos1 and Sin1 are the Sin and Cos values from the angle used

I do not have time or inclination now to spend very much more time on this, but if you have info that will work the first time I try it, I can give 200 points for a width and height formula, maybe more if you have anything for the  SetWorldTransform( )

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...