RosettaCodeData/Task/Bitmap-Midpoint-circle-algo.../Phix/bitmap-midpoint-circle-algo...

41 lines
1.1 KiB
Plaintext

-- demo\rosetta\Bitmap_Circle.exw (runnable version)
include ppm.e -- red, yellow, new_image(), write_ppm() -- (covers above requirements)
function SetPx(sequence img, atom x, y, integer colour)
if x>=1 and x<=length(img)
and y>=1 and y<=length(img[x]) then
img[x][y] = colour
end if
return img
end function
function Circle(sequence img, atom x, y, r, integer colour)
atom x1 = -r,
y1 = 0,
err = 2-2*r
if r>=0 then
-- Bresenham algorithm
while 1 do
img = SetPx(img, x-x1, y+y1, colour)
img = SetPx(img, x-y1, y-x1, colour)
img = SetPx(img, x+x1, y-y1, colour)
img = SetPx(img, x+y1, y+x1, colour)
r = err
if r>x1 then
x1 += 1
err += x1*2 + 1
end if
if r<=y1 then
y1 += 1
err += y1*2 + 1
end if
if x1>=0 then exit end if
end while
end if
return img
end function
sequence img = new_image(400,300,yellow)
img = Circle(img, 200, 150, 100, red)
write_ppm("Circle.ppm",img)