Sign in to follow this  

Problem rotating a circle

This topic is 4272 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi i am writing a simple graphics program in java. I have car object which comprises of two quads and two circles, for the body, roof and two wheels respectively. My circle objects contain four points, (-2,2),(-2,-2),(2,-2),(2,2) This is because the fillOval function of the java graphics api requires the top left corner of the square surrounding the circle, followed by width and height. A picture of how the circle details are stored: I define the width by subracting the x component of the point at (2,2) from the x component of (-2,2) - giving 4 And the height by subtract the y component of the point at (2,2) from the y compnent of (2,-2) - again giving 4. The centre of the car is defined by the centre of the body of the car. To rotate i translate all the points to the origin and then do the rotations. This is where the problem occurs when trying to rotate the circle. I don't think i can explain it very well so hopefully this picture will help you understand: I realise the problem is the way i am working out the height and width of the circle for use with the fillOval function - however i can't for the life of me think how to do this properly. I am thinking something to do with holding the radius of the circle - but i am not very maths minded at all and can't get any further than this. Any advice would be greatly appreciated. Thanks :)

Share this post


Link to post
Share on other sites
Encapsulation is your Friend

I would write a function that takes takes a Center and Radius, then using that information draws a circle by calling the normal java function (the one with corner coordinates)
Once it works, you can just use that and forget about all this other ugly stuff...

Share this post


Link to post
Share on other sites
Does this mean i do not need to hold any data about the four verticies, just the centre and the cirles radius? I am not sure how i update the radius.

Say i start with centre at 0,0 and radius of 2. I translate the wheel to the origin and then scale by 10. How do i work out the new radius?

[Edited by - edd1986 on April 4, 2006 11:09:32 AM]

Share this post


Link to post
Share on other sites
to rotate the circle (top, left, width, height) around some point p (x, y) by angle phi:


// obtain center of circle
centerx = left + width / 2;
centery = top + height / 2;

// rotate center of circle around p by phi
x = p.x + (centerx - p.x) * cos(phi);
y = p.y + (centery - p.y) * sin(phi);

// calculate new top left point for drawOval
topleft.x = (x - width / 2);
topleft.y = (y - height / 2);

fillOval(topleft.x, topleft.y, width, height);




This of course assumes, your world coordinates are same as screen coordinates, with x coordinate increasing from left to right, and y coordinate increasing top to bottom.


If you want to rotate the circle by using points, as mentioned above, you do not change the points. You just translate them. fillOval assumes axis-aligned rectangle.

So to apply the above method to your actual coordinates:


// points
Point a; // Top left
Point b; // Top right
Point c; // Bottom right
Point d; // Bottom left

// obtain center of circle
centerx = (a.x + b.x) / 2;
centery = (a.y + c.y) / 2;

// rotate the center
x = p.x + (centerx - p.x) * cos(phi);
y = p.y + (centery - p.y) * sin(phi);

// do this for every point a,b,c,d
a.x = x + (a.x - centerx);
a.y = y + (a.y - centery);

b.x = x + (b.x - centerx);
b.y = y + (b.y - centery);
// and so on for c,d




It does get rather ugly, so some better representation of circle for this purpose is a better way to go.

Share this post


Link to post
Share on other sites
Thanks for the help however I am still confused. I start with a circle using the points described above, giving a radius of 2. Before i display this circle on the screen i need to scale it, which would change the radius, but i am not sure how it would change?

Share this post


Link to post
Share on other sites

This topic is 4272 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this