views:

210

answers:

2

Hi,

I have to draw an ellipse of arbitrary size and orientation pixel by pixel. It seems pretty easy to draw an ellipse whose major and minor axes align with the x and y axes, but rotating the ellipse by an arbitrary angle seems trickier. Initially I though it might work to draw the unrotated ellipse and apply a rotation matrix to each point, but it seems as though that could cause errors do to rounding, and I need rather high precision.

Is my suspicion about this method correct? How could I accomplish this task more precisely?

I'm programming in C++ (although that shouldn't really matter since this is a more algorithm-oriented question).

Edit: as David pointed out, I guess I may really be wondering how to do pixel interpolation.

+3  A: 

Use:

x = X cos(a) - Y sin(a)
y = Y cos(a) + X sin(a)

Where a is the angle of anticlockwise rotation, (x, y) are the new coordinates, and (X, Y) are the old.

You should use floats to preserve precision. Just go through every point, apply the transformation, and voilà.

Edit: after some searching, here's some code from Microsoft: http://research.microsoft.com/en-us/um/people/awf/graphics/bres-ellipse.html that draws rastered conic sections.

David Titarenco
I believe this runs the usual risk with "forward" transformations: that the aliasing will cause you to "skip" pixels in the transformed coordinates.
dmckee
Yeah, that's why I've been looking for a different method. Are there any good ways to deal with aliasing?
amc
Yeah, I mean, it all depends on what library you use to do your drawing. But aliasing will screw up any type of rotation. Your question should be "how do I do pixel interpolation?" :)
David Titarenco
@amc: I read working solution once, but it's in a book somewhere... I'll dig it up and post it if I find time and no one eats me to it.
dmckee
Awesome, thanks for your help. At least now I know what to search for. :D
amc
Interpolation is extremely general, but is not the only solution, and can be slow by comparison to other methods. The classic example of how to *not* have to interpolate are the Bresenham methods for lines and circles. I assume a similar technique exist for this case...
dmckee
Added a link to some fun code by MS, it's pretty verbose so it should be easy to follow :}
David Titarenco
Thanks a lot! That's pretty awesome.
amc
+1  A: 

Bresenham (famous for his line drawing algorithm) also has an algorithm for drawing an ellipse. You can try to google bresenham ellipse.

Martin Liversage