首页 \ 问答 \ 按Xaolin Wu的描述绘制一个反锯齿圆(Drawing an antialiased circle as described by Xaolin Wu)

按Xaolin Wu的描述绘制一个反锯齿圆(Drawing an antialiased circle as described by Xaolin Wu)

我希望实施由吴小林在Siggraph '91发表在他的论文“An Efficient Antialiasing Technique”中的“Fast Anti-Aliased Circle Generator”例程。

这是我使用Python 3和PySDL2编写的代码:

def draw_antialiased_circle(renderer, position, radius):
    def _draw_point(renderer, offset, x, y):
        sdl2.SDL_RenderDrawPoint(renderer, offset.x - x, offset.y + y)
        sdl2.SDL_RenderDrawPoint(renderer, offset.x + x, offset.y + y)
        sdl2.SDL_RenderDrawPoint(renderer, offset.x - x, offset.y - y)
        sdl2.SDL_RenderDrawPoint(renderer, offset.x + x, offset.y - y)

    i = 0
    j = radius
    d = 0
    T = 0

    sdl2.SDL_SetRenderDrawColor(renderer, 255, 255, 255, sdl2.SDL_ALPHA_OPAQUE)
    _draw_point(renderer, position, i, j)

    while i < j + 1:
        i += 1
        s = math.sqrt(max(radius * radius - i * i, 0.0))
        d = math.floor(sdl2.SDL_ALPHA_OPAQUE * (math.ceil(s) - s) + 0.5)

        if d < T:
            j -= 1

        T = d

        if d > 0:
            alpha = d
            sdl2.SDL_SetRenderDrawColor(renderer, 255, 255, 255, alpha)
            _draw_point(renderer, position, i, j)
            if i != j:
                _draw_point(renderer, position, j, i)

        if (sdl2.SDL_ALPHA_OPAQUE - d) > 0:
            alpha = sdl2.SDL_ALPHA_OPAQUE - d
            sdl2.SDL_SetRenderDrawColor(renderer, 255, 255, 255, alpha)
            _draw_point(renderer, position, i, j + 1)
            if i != j + 1:
                _draw_point(renderer, position, j + 1, i)

这是我相信在他的论文中描述的一个天真的实现,除了我将半径值分配给j而不是i因为我误解了某些内容或者他的论文中存在错误。 事实上,他用半径值初始化ij0 ,然后定义循环条件i <= j ,当半径为0时它只能是真的。 这种改变使我对所描述的做了一些小的修改,而且if d > Tif d < T ,我也改变了if d > T只是因为它看起来不对。

除了每个八分圆的开始和结束时,这个实现大部分工作得很好,在那里出现了一些毛刺。

圈

上面的圆的半径为1.正如您可以在每个八分圆的开始处所看到的那样(例如在(0,1)区域中),循环内绘制的像素不与在第一个像素之前绘制的第一个像素对齐循环开始。 另外,在每个八分区结束时(例如(sqrt(2) / 2, sqrt(2) / 2)区域)都会出错。 我设法通过将if d < T条件改为if d <= T来消除这个最后一个问题,但是同样的问题会在每个八分圆的开始处出现。

问题1 :我做错了什么?

问题2 :如果我希望输入位置和半径是浮点,会不会有任何疑问


I'm tyring to implement the “Fast Anti-Aliased Circle Generator” routine that was described by Xiaolin Wu in his paper “An Efficient Antialiasing Technique” from Siggraph '91.

This is the code that I wrote using Python 3 and PySDL2:

def draw_antialiased_circle(renderer, position, radius):
    def _draw_point(renderer, offset, x, y):
        sdl2.SDL_RenderDrawPoint(renderer, offset.x - x, offset.y + y)
        sdl2.SDL_RenderDrawPoint(renderer, offset.x + x, offset.y + y)
        sdl2.SDL_RenderDrawPoint(renderer, offset.x - x, offset.y - y)
        sdl2.SDL_RenderDrawPoint(renderer, offset.x + x, offset.y - y)

    i = 0
    j = radius
    d = 0
    T = 0

    sdl2.SDL_SetRenderDrawColor(renderer, 255, 255, 255, sdl2.SDL_ALPHA_OPAQUE)
    _draw_point(renderer, position, i, j)

    while i < j + 1:
        i += 1
        s = math.sqrt(max(radius * radius - i * i, 0.0))
        d = math.floor(sdl2.SDL_ALPHA_OPAQUE * (math.ceil(s) - s) + 0.5)

        if d < T:
            j -= 1

        T = d

        if d > 0:
            alpha = d
            sdl2.SDL_SetRenderDrawColor(renderer, 255, 255, 255, alpha)
            _draw_point(renderer, position, i, j)
            if i != j:
                _draw_point(renderer, position, j, i)

        if (sdl2.SDL_ALPHA_OPAQUE - d) > 0:
            alpha = sdl2.SDL_ALPHA_OPAQUE - d
            sdl2.SDL_SetRenderDrawColor(renderer, 255, 255, 255, alpha)
            _draw_point(renderer, position, i, j + 1)
            if i != j + 1:
                _draw_point(renderer, position, j + 1, i)

This is a naive implementation of what I believe is being described in his paper, at the exception that I assigned the radius value to j instead of i because either I misunderstand something or there's a mistake in his paper. Indeed, he initializes i with the radius value, j with 0, and then defines the loop condition i <= j which can only be true when the radius is 0. This change led me to make some other minor modifications from what's described, and I also changed if d > T to if d < T simply because it looked broken otherwise.

This implementation works mostly well except at the start and the end of each octant, where some glitches appear.

circle

The circle above has a radius of 1. As you can see at the start of each octant (such as in the (0, 1) area), the pixels drawn within the loop are not aligned to the first pixel that is drawn before the loop starts. Also something goes awfully wrong towards the end of each octant (such as in the (sqrt(2) / 2, sqrt(2) / 2) area). I managed to make this last issue disappear by changing the if d < T condition to if d <= T, but the same problem then shows up at the start of each octant.

Question 1: What am I doing wrong?

Question 2: Would there be any gotcha if I wanted the input position and radius to be floating points?


原文:https://stackoverflow.com/questions/37589165
更新时间:2024-02-03 16:02

最满意答案

运行时是我认为的标准NuGet 包的一部分。


The runtime is part of a standard NuGet package going forward I believe.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。