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

  • SEARCH
  • Home
  • 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 941327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:06:10+00:00 2026-05-15T22:06:10+00:00

i have a problem with rotating a rectangle and place it on a canvas

  • 0

i have a problem with rotating a rectangle and place it on a canvas in a certain way. Here is what i try to achieve:

Rotated Rectangles http://www.freeimagehosting.net/uploads/79844652d2.jpg

The big rectangle in the picture is my canvas. The smaller rectangle is my rectangle i want to rotate. When i rotate the rectangle (dotted rectangle), it gets clipped of course. To avoid this, i want to reposition the rectangle like in the picture on the right side.

This is how i tried it so far:

Rectangle rect = new Rectangle();
rect.Width = 100;
rect.Height = 50;
int angle = 30;
rect.RenderTransform = new RotateTransform(angle, rect.Width/2, rect.Height/2);
canvas.Children.Add(rect);

double x = Math.Cos(30) * (rect.Width / 2) + Math.Sin(30) * (rect.Height / 2) - rect.Width / 2;
double y = Math.Sin(30) * (-rect.Width / 2) + Math.Cos(30) * (rect.Height / 2) - rect.Height / 2;
Canvas.SetLeft(rect, x);
Canvas.SetTop(rect, y);

I thought the best way to do this is to calculate the x and y offset and position the rectangle by Canvas.SetLeft and Canvas.SetTop. But i have problems figuring out how to do the math. (The y calculation seems to work).

Actually i want to place several rectangles on the canvas at random positions. The rotation angle can be a value between -45 and 45 degree and the rectangle sizes can be random values, too. But the rectangles should always be fully visible in the canvas so i need to know the offsets for the bounds of the position coordinates. (The rectangles could overlap themselves.)

I hope you understand my problem. It would be nice if you can help me out.

  • 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-05-15T22:06:11+00:00Added an answer on May 15, 2026 at 10:06 pm

    The solution is actually a lot simpler than you may have guessed. Instead of using RenderTransform, use LayoutTransform instead. It takes the same kinds of transformations but instead of applying them to the rendered output of the element during the rendering pass, it alters the layout space of the element during the layout pass. So the following XAML produces the result you showed in the second example in your screen shot.

    <Canvas Width="640" Height="480">
        <Rectangle Fill="Blue" Width="200" Height="80">
            <Rectangle.LayoutTransform>
                <RotateTransform Angle="-45"/>
            </Rectangle.LayoutTransform>
        </Rectangle>
    </Canvas>
    

    Note that this does not work in Silverlight because Silverlight doesn’t support LayoutTransform.

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

Sidebar

Related Questions

I have a problem with rotating. I know that I can rotate a Texture2D
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have a problem just with rendering a simple rotating cube with SlimDX (DirectX
I have a subview layering problem where I have a rotating arrow and a
Here is the problem: I have many sets of points, and want to come
I have a problem with a UIView-subclass object that I am rotating using Core
I have a big problem when I try to rotate my projectiles along with
I have a problem. I am rotating an div 45 degrees. I then want
I need to solve a problem by rotating an image, but I have this
I have problem with my query on C, I’m using the oci8 driver. This

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.