基于sqrt的填充椭圆像素绘制函数

时间:2015-05-25 18:11:32

标签: vb.net lua draw pixels ellipse

我正在尝试在Lua或基于VB的代码中创建一个函数来绘制/绘制填充椭圆。 我对这个数学知之甚少,我可以帮忙。

我用Google搜索了有关使用代码绘制省略号的所有内容,但我找不到一个简单的工作示例在VB或Lua中用于填充。

在这个网站的上一篇文章中,我确实得到了一个关于如何绘制正常椭圆的答案,但没有找到任何填充的椭圆,这就是我为一个填充的主题创建一个新主题的原因。

以下是我访问过的一些网站,但我找不到一种方法来制作一个填充的椭圆而不重绘已绘制的像素...

https://sites.google.com/site/ruslancray/lab/projects/bresenhamscircleellipsedrawingalgorithm/bresenham-s-circle-ellipse-drawing-algorithm

http://groups.csail.mit.edu/graphics/classes/6.837/F98/Lecture6/circle.html

http://www.blitzbasic.com/codearcs/codearcs.php?code=2817

http://hackipedia.org/Algorithms/Graphics/pdf/A%20Fast%20Bresenham%20Type%20Algorithm%20For%20Drawing%20Ellipses%20by%20John%20Kennedy.pdf

https://scratch.mit.edu/projects/49873666/

http://www.sourcecodesworld.com/source/show.asp?ScriptID=112

以下是普通椭圆的代码(感谢VB版的“Johnny Strings”):

function DrawEllipse(xc,yc,w,h)
    local w2  = w * w
    local h2  = h * h
    local fw2 = 4 * w2
    local fh2 = 4 * h2

    xc = xc + w
    yc = yc + h

    local x  = 0
    local y  = h
    local s  = 2 * h2 + w2 * (1 - h)
    while h2 * x <= w2 * y do
        dot(xc + x, yc + y)
        dot(xc - x, yc + y)
        dot(xc + x, yc - y)
        dot(xc - x, yc - y)
        redraw()inkey()
        color(int(rnd()*255),int(rnd()*255),int(rnd()*255))
        if s >= 0 then
            s = s + fw2 * (1 - y)
            y = y - 1
        end
        s = s + h2 * ((4 * x) + 6)
        x = x + 1
    end
    x = w
    y = 0
    s = 2 * w2 + h2 * (1 - w)
    while w2 * y <= h2 * x do
        dot(xc + x, yc + y)
        dot(xc - x, yc + y)
        dot(xc + x, yc - y)
        dot(xc - x, yc - y)
        redraw()inkey()
        color(int(rnd()*255),int(rnd()*255),int(rnd()*255))
        if s >= 0 then
            s = s + fh2 * (1 - x)
            x = x - 1
        end
        s = s + w2 * ((4 * y) + 6)
        y = y + 1
    end
end

1 个答案:

答案 0 :(得分:1)

这是我过去为我的CPU渲染器提出的。它非常有效且非常简单。

它依赖于椭圆的数学定义,因此椭圆以x,y为中心绘制,其宽度和高度由中心定义,而不是从另一侧定义。

绘制点功能在指定的x x点处绘制像素。

local function drawaxisalignedellipse(x,y,w,h)
    --n Defines the bounds of the horizontal lines which fill the ellipse.
    local n=w
    local w2=w*w
    local h2=h*h

    --draws the center horizontal line.
    for i=x-w,x+w do
        drawpoint(i,y)
    end

    for j=1,h do
        --The current top and bottom rows.
        local ra,rb=y+j,y-j

        --This loop removes 1 from n until it is within the shape
        while w2*(h2-j*j)<h2*n*n and n~=0 do n=n-1 end

        --Draws horizontal line from -n to n across the ellipse
        for i=x-n,x+n do
            drawpoint(i,ra)
            drawpoint(i,rb)
        end
    end
end