Bitmap/Midpoint circle algorithm: Difference between revisions

m (Added BASIC256 sample....)
Line 1,258:
loop x y m
;;</lang>
 
=={{header|Phix}}==
{{Trans|Go}}
Requires new_image() from [[Bitmap#Phix|Bitmap]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]].
Included as demo\rosetta\Bitmap_Circle.exw, results may be verified with demo\rosetta\viewppm.exw
<lang Phix>constant red = 0xff2020,
yellow = 0xffdf20
 
function SetPx(sequence img, atom x, atom 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, atom y, atom 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)</lang>
 
=={{header|PicoLisp}}==
7,806

edits