Drawing Oval with Gdk Cairo Context

922 views Asked by At

I want to draw only the circumference of an oval. I use this:

gc->save();
gc->translate( xc, yc );
gc->arc( 0.0, 0.0, 1.0, 0.0, 2.0*M_PI );
gc->scale( width*0.5, height*0.5 );
gc->stroke();
gc->restore();

but I constantly get a filled oval. What am I doing wrong?

1

There are 1 answers

0
guff On

Well, your call to scale() is probably not doing what you intended. I'm not sure if you accidentally put the calls in the wrong order, or if you don't quite understand how cairo's transformations work. In case it's the latter:

Transformations only affect the following operations. And they only affect operations involving coordinates or sizes somehow. In this case, you likely wanted to apply it to the arc. However, it's actually only getting applied to the stroke, and likely in a way you did not intend.

Know how I mentioned transforms affect operations involving coordinates or sizes? Well, it might not be obvious, but stroke does implicitly involve sizes: namely, the stroke size. So your arc's stroke size gets scaled by width * 0.5 on the x axes and height * 0.5 on the y axes. In other words, the stroke is so friggin' huge it looks like a fill.

Interestingly, even though your arc was actually unaffected by scale(), which means you would have been left with a circle instead of an oval, you still wound up with an oval because of the way the stroke was scaled.

So, to fix your issue:

  • call scale() before arc()
  • reset the scaling factor after you call arc() but before you call stroke(), so that you don't wind up with the monstrous stroke again