如何逐像素绘制任意方向的椭圆?

发布于 2024-09-04 22:45:04 字数 253 浏览 12 评论 0原文

我必须逐像素绘制任意大小和方向的椭圆。绘制一个长轴和短轴与 x 轴和 y 轴对齐的椭圆似乎很容易,但将椭圆旋转任意角度似乎更棘手。最初,我认为绘制未旋转的椭圆并对每个点应用旋转矩阵可能会起作用,但似乎这可能会导致舍入错误,而且我需要相当高的精度。

我对这种方法的怀疑是否正确?我怎样才能更准确地完成这个任务?

我正在用 C++ 编程(尽管这并不重要,因为这是一个更面向算法的问题)。

编辑:正如大卫指出的,我想我可能真的想知道如何进行像素插值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我偏爱纯白色 2024-09-11 22:45:04

使用:

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

其中a是逆时针旋转的角度,(x, y)是新坐标,(X, Y)是老的。

您应该使用浮点数来保持精度。只需检查每个点,应用转换,然后瞧。

编辑:经过一番搜索,这里有一些来自微软的代码: http://research.microsoft.com/en-us/um/people/awf/graphics/bres-ellipse.html 绘制光栅圆锥曲线截面。

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.

司马昭之心 2024-09-11 22:45:04

Bresenham(因其画线算法而闻名)也有一种绘制椭圆的算法。您可以尝试在 Google 上搜索bresenham ellipse

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

好久不见√ 2024-09-11 22:45:04

使用 Bresenham 方法绘制轴对齐的椭圆,但对绘制的椭圆应用剪切。您还需要修改轴的长度。 剪切椭圆也是椭圆。 此方法保留 Bresenham使用水平线段绘制填充椭圆的优点。为此,您需要一个函数,该函数将椭圆的轴和旋转规范映射到一组不同的轴和剪切力。在线提供解决方案 http://scratch.mit.edu/projects/50039326/讨论该方法以及所涉及数学的描述http://scratch.mit.edu/discuss/topic/94194/

映射由 Nathan Dinsmore 发现(MIT Scratch 站点 的用户 nXIII)

Use the Bresenham method of drawing axis-aligned ellipses, but apply a shear to the drawn ellipse. You will also need to modify the lengths of the axes. A sheared ellipse is also an ellipse. This method preserves the Bresenham advantage of drawing filled ellipses using horizontal line segments. What you need in order to do this is the function which maps a specification of an ellipse in terms of axes and rotation into a different set of axes and a shear. A solution is available online at http://scratch.mit.edu/projects/50039326/ with a discussion about the method and a description of the math involved at http://scratch.mit.edu/discuss/topic/94194/

The mapping was discovered by Nathan Dinsmore (user nXIII at the MIT Scratch site)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文