使用 Gdk Cairo Context 绘制椭圆形

发布于 2024-12-26 04:17:45 字数 241 浏览 3 评论 0原文

我只想画椭圆形的周长。我用这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老子叫无熙 2025-01-02 04:17:45

好吧,您对 scale() 的调用可能没有达到您的预期。我不确定您是否不小心将调用顺序弄错了,或者您是否不太了解 cairo 的转换是如何工作的。如果是后者:

转换仅影响以下操作。它们仅以某种方式影响涉及坐标或尺寸的操作。在这种情况下,您可能想将其应用到圆弧。然而,它实际上只应用于笔画,而且很可能以您不希望的方式应用。

知道我提到的变换如何影响涉及坐标或大小的操作吗?嗯,这可能并不明显,但笔划确实隐含地涉及大小:即笔划大小。因此,弧线的描边大小在 x 轴上按 width * 0.5 缩放,在 y 轴上按 height * 0.5 缩放。换句话说,笔划太大了,看起来就像填充一样。

有趣的是,即使你的弧线实际上不受 scale() 的影响,这意味着你会留下一个圆形而不是椭圆形,但由于笔画的方式,你仍然会得到一个椭圆形缩放。

因此,要解决您的问题:

  • 在调用 arc ()< 之后调用 scale() before arc()
  • 重置缩放因子/code> 但在调用 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 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文