Problem rotating a circle

Started by
3 comments, last by edd1986 18 years ago
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 :)
Advertisement
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...
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]
to rotate the circle (top, left, width, height) around some point p (x, y) by angle phi:

// obtain center of circlecenterx = left + width / 2;centery = top + height / 2;// rotate center of circle around p by phix = p.x + (centerx - p.x) * cos(phi);y = p.y + (centery - p.y) * sin(phi);// calculate new top left point for drawOvaltopleft.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:
// pointsPoint a; // Top leftPoint b; // Top rightPoint c; // Bottom rightPoint d; // Bottom left// obtain center of circlecenterx = (a.x + b.x) / 2;centery = (a.y + c.y) / 2;// rotate the centerx = p.x + (centerx - p.x) * cos(phi);y = p.y + (centery - p.y) * sin(phi);// do this for every point a,b,c,da.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.

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?

This topic is closed to new replies.

Advertisement