# Rotate rectangle containing rectangles

Hello,

I have been trying for a couple days now, and i hope someone can help.

What i have is a graphicspath, which is a rectangle, and inside this rectangle i draw 10 smaller rectangles again each its own graphicspath, that i need to rotate along with the bigger rectangle. I have been able to get the rectangle to rotate 90 degrees and then recreate all the smaller rectangles perfectly fine, but what i really need to do is rotate them all by any angle say 27 degrees or 108 degrees.

To do the rotation i have been using the Matrix RotateAt method, the bigger rectangle rotates on the center point of the bounding rectangle, the smaller rectangles rotate at pathpoints(0).

image A shows current state before rotation
image B shows what i am currently getting after rotation (rotated 45 degrees)
image C shows what i need to get too after rotation.

I just cannot seem to see what i need to do in my head.

I have been using VB and C#.Net for years, just never any GDI.

Hope this makes sense, any help would be greatly appreciated.

Thanks

Andy
a.bmp
b.bmp
c.bmp
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
What you want is to set the origin to the big box center:

originX = boxWidth/2
originY = boxHeight/2

Good luck
0
Middle School Assistant TeacherCommented:
Do you really need to rotate each GraphicsPath?...or can you just rotate the entire GRAPHICS before you draw them?

Can you give more details on how they are created and used/drawn?

Simple example of rotating the Graphics:
``````using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private Boolean rotated = false;
List<GraphicsPath> paths = new List<GraphicsPath>();

private void Form1_Load(object sender, EventArgs e)
{
GraphicsPath gp = new GraphicsPath();
gp.AddRectangle(new Rectangle(new Point(250, 25), new Size(100, 200)));
for (int i = 0; i < 10; i++)
{
gp = new GraphicsPath();
gp.AddRectangle(new Rectangle(new Point(250, 25 + (i * 15)), new Size(50, 10)));
}
}

private void button1_Click(object sender, EventArgs e)
{
rotated = !rotated;
this.Refresh();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics G = e.Graphics;
if (rotated)
{
G.RotateTransform(27f);
}
foreach (GraphicsPath path in paths)
{
G.DrawPath(Pens.Black, path);
}
}

}
}
``````
BeforeRotation.jpg
AfterRotation.jpg
0
Commented:
Note unlike what Idle Mind said ,you don't have to tarnsform(rotate) each graphic path, but rather the only one that is used:

private void button1_Click(object sender, EventArgs e)
{
Matrix mat = new Matrix();
mat.RotateAt(108, new PointF(_bigRect.Width / 2, _bigRect.Height / 2));

_gp.Transform(mat);
this.Refresh();
}

where _bigRect is the big rect Rectangle used to create the big rect,
while _gp is graphics path.
Note you can easily store the center (origin) instead of the actual rectangle.

I do have to say Idle_Mind approach would work. The only case where it wouldn't be correct for you is if you have multiple graphics path drawn using the same Graphics object, and you want to apply different transformations for them. It is still possible to do it, but probably not worth it.
0
Middle School Assistant TeacherCommented:
If you want it to rotate around the CENTER of the big rectangle (instead of around the top left of the form) then you could do:

G.TranslateTransform(mainRC.Left + mainRC.Width / 2, mainRC.Top + mainRC.Height / 2);
G.RotateTransform(27f);
G.TranslateTransform(-(mainRC.Left + mainRC.Width / 2), -(mainRC.Top + mainRC.Height / 2));

Full code:
``````using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private Boolean rotated = false;
List<GraphicsPath> paths = new List<GraphicsPath>();
private Rectangle mainRC = new Rectangle(new Point(250, 25), new Size(100, 200));

private void Form1_Load(object sender, EventArgs e)
{
GraphicsPath gp = new GraphicsPath();
for (int i = 0; i < 10; i++)
{
gp = new GraphicsPath();
gp.AddRectangle(new Rectangle(new Point(250, 25 + (i * 15)), new Size(50, 10)));
}
}

private void button1_Click(object sender, EventArgs e)
{
rotated = !rotated;
this.Refresh();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics G = e.Graphics;
if (rotated)
{
G.TranslateTransform(mainRC.Left + mainRC.Width / 2, mainRC.Top + mainRC.Height / 2);
G.RotateTransform(27f);
G.TranslateTransform(-(mainRC.Left + mainRC.Width / 2), -(mainRC.Top + mainRC.Height / 2));
}
foreach (GraphicsPath path in paths)
{
G.DrawPath(Pens.Black, path);
}
}

}
}
``````
0

Experts Exchange Solution brought to you by