【问题标题】:sqrt-based filled ellipse pixel drawing function基于sqrt的填充椭圆像素绘制函数
【发布时间】:2015-05-25 18:11:31
【问题描述】:

我正在尝试在基于 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

【问题讨论】:

  • @The Blue Dog,对于这个主题的相似之处感到抱歉,但在这里我明确要求绘制填充椭圆的代码,在另一篇文章中我只收到了普通椭圆的回复......也许因为我要求 2 件事(填充椭圆和普通椭圆)最好为填充椭圆创建一个新主题?
  • 我正在努力翻译它,如果我有一些问题,我可以在这里发布我未完成的代码吗?还是我必须编辑我的问题?

标签: vb.net lua draw pixels ellipse


【解决方案1】:

这是我过去为我的 CPU 渲染器设计的。它非常高效,也非常简单。

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

draw point 函数在指定的 x x y 点处绘制一个像素。

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

【讨论】:

  • 嗨,唐纳德!感谢您的代码,正是我想要的。有 2 个小问题,我不知道如何更改代码以使其正常工作.. 第一个问题:如果我想制作一个 5 x 5 像素(10、10、5、5)的圆圈,那么圆是 11 x 11 像素.. 第二个问题:如果我想绘制这样的椭圆“drawaxisalignedellipse(0,0,5,5)”然后椭圆在画布外绘制,左侧和顶部应该开始绘制从边境对吗?
  • 原因是因为高度和宽度是从中心定义的,而不是从另一侧定义的。椭圆的位置定义在中心。对不起,我应该这么说的。我这样做是因为这是定义对象位置和大小的最自然和最有用的方法。如果您想以标准编程方式执行此操作,可以说 x=x-w y=y-h w=floor(w/2) h=floor(h/2)。
  • 如果您不介意,我可以再问您一件事吗? “非”填充椭圆的代码也那么简单吗?我的代码很长.. 问候!
猜你喜欢
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多