luabresenham

Bresenham's parabola algorithm, Lua


How can I get points of parabola as by the Bresenham's argorithm?

Now I have:

function getParabolaBresenham (x0, y0, a, b, c, deltaXmax)
    local pixels = {}
    local dy = 0
    local dx = 0
    local p = b

    while math.abs(dx) <= deltaXmax do
        local x1 = x0 + dx
        local y1 = a * x1 * x1 + b * x1 + c
        setPixel(pixels, x1, y1)
        dy = dy + 1
        if p < 0 then
            dx = dx + 1
            p = p + 2 * a
        else -- next column
            dy = dy + 1
            dx = dx + 1
            p = p + 2 * a - 2 * b
        end
    end
    return pixels
end

where x0, y0 - starting point of drawn parabola;

a, b, c, - parabola's values in function y = a * x * x + b * x + c

deltaXmax - the horizontal limit of parabola

(the deltaYmax will be helpful too)

Actually, I need to draw every second manhattan's pixel, but it will be helpful to fix the problem.


Solution

  • It looks like that I've solved it:

    Lua code to make parabola rasterization, the output is a list of integers as {x1, y1, x2, y2 ...}

    function getParabolaPoints (a, xMax)
      local x, y = 0, 0
      local points = {x, y}
      while x < xMax do
        local dx, dy = 0, 0
        if ((y+1)/a)^0.5 - x - 0.5 > 0 then dx = 1 end
        if a*(x+1)^2 - y - 0.5 >= 0 then dy = 1 end
        x = x + dx
        y = y + dy
        table.insert (points, x)
        table.insert (points, y)
      end
      return points
    end