Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Hello friends,
I have one problem relating to rotate ellipse by given Center,
Suppose I have one ellipse and what should be is to rotate that ellipse by point given by user and ellipse should be rotate around that given point.
I have tried
g.RotateTransform(…)
g.TranslateTransform(…)

How could it be possible please any buddy can suggest……
Thanks…….
Posted
Comments
Sergey Alexandrovich Kryukov 14-Mar-11 16:10pm    
I already saw the exact same Question. Is it you who duplication or there is a school where many people got the same assignments? The problem is difficult, but the way, I would say, ill-posed. ("Ill-posed problems" is the whole branch of mathematics.)
--SA

Use Matrix to define a rotate point. Example...

C#
e.Graphics.DrawEllipse(Pens.Black, 50, 50, 50, 100);
Matrix X = new Matrix();
// The Point to rotate
PointF pt = new PointF(90.0f, 80.0f);
// Rotate by 45 degrees
X.RotateAt(45, pt, MatrixOrder.Append);
e.Graphics.Transform = X;

e.Graphics.DrawEllipse(Pens.Red, 50, 50, 50, 100);


You can see the difference of the two ellipse
 
Share this answer
 
Here's a simple code from msdn on how to translate and rotate a graph ...

C#
public void MultiplyTransformMatrixOrder(PaintEventArgs e)
{
// Create transform matrix.
Matrix transformMatrix = new Matrix();
// Translate matrix, prepending translation vector.
transformMatrix.Translate(200.0F, 100.0F);
// Rotate transformation matrix of graphics object,
//  prepending rotation matrix.
e.Graphics.RotateTransform(30.0F);
// Multiply (append to) transformation matrix of
//  graphics object to translate graphics transformation.
e.Graphics.MultiplyTransform(transformMatrix, MatrixOrder.Append);
// Draw rotated, translated ellipse.
e.Graphics.DrawEllipse(new Pen(Color.Blue, 3), -80, -40, 160, 80);
}
 
Share this answer
 
Comments
Olivier Levrey 14-Mar-11 8:46am    
Stop voting 4 on correct and complete answers.
Sergey Alexandrovich Kryukov 14-Mar-11 15:33pm    
Olivier, how did you know that?
I recently questioned Piccadilly's consistency and used to blame posting of some irrelevant or unverified information, but nobody should ever be blamed without a firm proof of the fact...
--SA
Olivier Levrey 15-Mar-11 5:04am    
Yesterday he answered many questions and all other answers except his received a 4. So I guessed he dit this and didn't agree with that. His answers are not wrong (and I even gave points to a few of them), I just want him to give fair points.
By the way, I didn't vote this one (because of what I said before) but I will now because it doesn't deserve a 1. I don't understand who did this??
Voted 5.
Piccadilly Yum Yum 14-Mar-11 8:58am    
sorry ...
Try something like:

C#
//center of the rotation
PointF center = new PointF(...);
//angle in degrees
float angle = 45.0f;
//use a rotation matrix
using (Matrix rotate = new Matrix())
{
    //used to restore g.Transform previous state
    GraphicsContainer container = g.BeginContainer();

    //create the rotation matrix
    rotate.RotateAt(angle, center);
    //add it to g.Transform
    g.Transform = rotate;

    //draw what you want
    ...

    //restore g.Transform state
    g.EndContainer(container);
}


If you want to add several transforms, then you can do:
//gets current transform
Matrix transform = g.Transform;
//add the rotation
transform.Multiply(rotate, MatrixOrder.Append);
//use the new transform
g.Transform = transform;
 
Share this answer
 
v2
Comments
Pritesh Aryan 14-Mar-11 8:30am    
ya it is working thanks..........
Olivier Levrey 14-Mar-11 8:31am    
You are welcome.
Sergey Alexandrovich Kryukov 14-Mar-11 15:35pm    
Exactly the right way, my 5.
I did not understand the title of the Question, but it should be noted: rendering like that should be done on Control, not image object; and double buffering should be applied, and all rendering should be done inside Paint event (or in overloaded OnPaint).
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900