使用 Gdk Cairo Context 绘制椭圆形
我只想画椭圆形的周长。我用这个:
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();
但我经常得到一个填充的椭圆形。我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您对
scale()
的调用可能没有达到您的预期。我不确定您是否不小心将调用顺序弄错了,或者您是否不太了解 cairo 的转换是如何工作的。如果是后者:转换仅影响以下操作。它们仅以某种方式影响涉及坐标或尺寸的操作。在这种情况下,您可能想将其应用到圆弧。然而,它实际上只应用于笔画,而且很可能以您不希望的方式应用。
知道我提到的变换如何影响涉及坐标或大小的操作吗?嗯,这可能并不明显,但笔划确实隐含地涉及大小:即笔划大小。因此,弧线的描边大小在 x 轴上按
width * 0.5
缩放,在 y 轴上按height * 0.5
缩放。换句话说,笔划太大了,看起来就像填充一样。有趣的是,即使你的弧线实际上不受
scale()
的影响,这意味着你会留下一个圆形而不是椭圆形,但由于笔画的方式,你仍然会得到一个椭圆形缩放。因此,要解决您的问题:
arc ()< 之后调用
scale()
beforearc()
lines()
之前,这样你就不会再次遭遇可怕的中风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 andheight * 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:
scale()
beforearc()
arc()
but before you callstroke()
, so that you don't wind up with the monstrous stroke again