Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8769283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:18:05+00:00 2026-06-13T17:18:05+00:00

I have a rectangle that has to be rotated always the same amount of

  • 0

I have a rectangle that has to be rotated always the same amount of degrees. Lets call this angle alpha ().

The width (w) and height (h) of this rectangle can vary. The rectangle has always to fit rotated inside the big rectangle. It must be scaled up or down to fit inside the gray rectangle.

NOTE: Alpha is the angle between w and the horizontal line.

So, there are 3 kinds of rectangles where

w > h
w < h  or
w = h

See the picture below.

enter image description here

What I know:

  1. The big rectangle has width of R and height of K and I know both values;
  2. w and h are unknown;
  3. the rectangle is always rotated degrees;
  4. I know the value of w/h. I call this “ratioWH”;
  5. red rectangle is always centered horizontally and vertically on the gray rectangle

what I need to know:

  1. the maximum values of w and h that will fit the gray rectangle for each case of w and h.
  2. the coordinates of point P, assuming that 0,0 is at the upper left of the gray rectangle.

This is what I did so far, but this is not giving the correct values:

CGPoint P = CGPointZero;

if (ratioWH > 0) { // means w > h

    maxH = R / (ratioWH * fabsf(cosf(theta)) + fabsf(sinf(theta)));
    maxW = maxH * ratioWH;

    // P.x = 0.0f;  // P.x is already zero
    CGFloat marginY = (K - maxW * fabsf(sinf(theta)) - maxH * fabsf(cosf(theta))) / 2.0f;
    P.y = marginY +  maxW * fabsf(sinf(theta));

} else { // w <= h

    maxW  = K / (fabsf(cosf(theta) / ratioImagemXY) + fabsf(sinf(theta)));
    maxH = maxW / ratioWH;


    P.x = (R - maxW * fabsf(cosf(theta)) - maxH * fabsf(sinf(theta))) / 2.0f;
    P.y = maxW * fabsf(sinf(theta));

} 

any clues? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:18:06+00:00Added an answer on June 13, 2026 at 5:18 pm

    The way I see it is like this… You work out the total width and total height of the rectangle. For that, you simply walk along two edges. Like this:

    dx = w * cos(theta) + h * sin(theta)
    dy = h * cos(theta) + w * sin(theta)
    

    These could be negative, so special handling would apply if the rectangle is rotated into other quadrants. This will happen later.

    You now just need the ratio between the width and height. This is where you decide whether to scale by the vertical amount or the horizontal amount. It’s nothing to do with w and h — it’s actually about where the rectangle ends up as a result of rotation. That’s what dx and dy are for.

    rectratio = abs( dx / dy )
    viewratio = R / K
    

    If rectratio comes out larger than viewratio that means the rotated rectangle’s horizontal footprint needs to be scaled. Otherwise you scale by the vertical footprint.

    if rectratio > viewratio
        scale = R / abs(dx)
    else
        scale = K / abs(dy)
    end
    

    And the scale itself is applied to the original width and height

    sw = scale * w
    sh = scale * h
    

    So now you can compute the corners of your rectangle. It doesn’t matter where you start.

    x[0] = 0
    x[1] = x[0] + sw * cos(theta)
    x[2] = x[1] + sh * sin(theta)
    x[3] = x[2] - sw * cos(theta)
    
    y[0] = 0
    y[1] = y[0] - sw * sin(theta)
    y[2] = y[1] + sh * cos(theta)
    y[3] = y[2] + sw * sin(theta)
    

    I’ve assumed image co-ordinates given that (0,0) is top-left, so increasing y moves down. So, if I haven’t made a mistake in my math, the above gives you the rectangle vertices (in clockwise order).

    The last thing to do is normalise them… This means finding the min value of px and py. Call them pxmin and pymin. I don’t need to show code for that. The idea is to calculate an offset for your rectangle such that the view area is defined by the rectangle (0,0) to (R,K).

    First we need to find the left and right value of the subview that completely contains our rotated rectangle… Remember the ratio before:

    if( rectratio > viewratio )
        // view is too tall, so centre vertically:
        left = 0
        top = (K - scale * abs(dy)) / 2.0
    else
        // view is too wide, so centre horizontally:
        left = (R - scale * abs(dx)) / 2.0
        top = 0
    end
    

    left and top are now the ‘minimum’ co-ordinate of our subview that exactly contains the rectangle (floating point rounding errors exempted). So:

    left += pxmin
    top += pymin
    

    Now they are the offset required to shift the rectangle to where it’s wanted. All you do is add left and top to all your rectangle co-ordinates, and you are done. The position of P is px[0] and py[0]. If you rotated by 90 degrees or more, it won’t be the top-left vertex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rectangle that I dynamically draw in a Window. Said Window has
I have a user control that has 5 rectangle shapes in it. I would
I have a rectangle class that has 2 points, the center axes point and
If I have a simple class that has a rectangle: package { import flash.display.Sprite;
I have a rectangle that I've created and set its individual properties like so
How can I draw a rectangle that have a black border and white background?
I have a point in a rectangle that I need to rotate an arbitrary
It seems that the WPF Rectangle shape does not have the Click event defined.
I have the following paint event (form) that is drawing the rectangle: void LogicSimulationViewerForm_Paint(object
I have noticed that doing actions like implementing an interface, that a small rectangle

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.