sqrt-based filled ellipse pixel drawing function

夙愿已清 提交于 2019-12-12 01:47:23

问题


I'm trying to make a function in Lua or VB based code to draw / plot a filled ellipse. I don't have much knowledge about this math and I can use some help.

I Googled everything there is to Google about drawing ellipses with code but I can't find a good simple working example in VB or Lua for a filled one.

On a previous post on this site I did get an answer about how to draw a normal ellipse but nothing came up for a filled one, that's why i make a new topic for a filled one.

Here are a few websites I visited but I can't find a way to make a filled ellipse without redrawing already drawed pixels...

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

Here is the code I have for a normal ellipse (thanks to "Johnny Strings" for the VB version):

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:


Here's what I came up with for my CPU renderer in the past. It's very efficient and very simple too.

It relies on the mathematical definition of the ellipse, so the ellipse is drawn centered at x,y and has the width and height defined from the center, not from the other side.

The draw point function draws a pixel at the x by y point specified.

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


来源:https://stackoverflow.com/questions/30443871/sqrt-based-filled-ellipse-pixel-drawing-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!