Jump to content

Bitmap/Midpoint circle algorithm: Difference between revisions

Added zkl
(Updated D entry)
(Added zkl)
Line 1,519:
SetVid(3); \restore normal text mode
]</lang>
 
=={{header|zkl}}==
Image cribbed from the BBC BASIC entry. Algorithm from Wikipedia article.
[[Image:circle_bbc.gif|right]]
<lang zkl>class PPM{
fcn init(width,height,rgb=0){
sz:=width*height;
var [const]
data=sz.pump(Data(sz*3),T(Void,rgb.toBigEndian(3))), // initialize to Black (RGB=000)
w=width, h=height;
}
fcn fill(rgb){
sz:=data.len()/3;
data.clear(); sz.pump(data,T(Void,rgb.toBigEndian(3)));
}
fcn __sGet(x,y){ data.toBigEndian(y*w*3 + x,3); } //ppm[x,y]
fcn __sSet(rbg,x,y){ data[y*w*3 + x*3,3]=rbg.toBigEndian(3); } //ppm[x,y]=rgb
fcn write(out){
out.write("P6\n#rosettacode PPM\n%d %d\n255\n".fmt(w,h));
out.write(data);
}
fcn circle(x0,y0,r,rgb){
x:=r; y:=0; radiusError:=1-x;
while(x >= y){
__sSet(rgb, x + x0, y + y0);
__sSet(rgb, y + x0, x + y0);
__sSet(rgb,-x + x0, y + y0);
__sSet(rgb,-y + x0, x + y0);
self[-x + x0, -y + y0]=rgb; // or do it this way, __sSet gets called as above
self[-y + x0, -x + y0]=rgb;
self[ x + x0, -y + y0]=rgb;
self[ y + x0, -x + y0]=rgb;
y+=1;
if (radiusError<0) radiusError+=2*y + 1;
else{ x-=1; radiusError+=2*(y - x + 1); }
}
}
}</lang>
<lang zkl>ppm:=PPM(200,200,0xFF|FF|FF);
ppm.circle(100,100,40,00); // black circle
ppm.circle(100,100,80,0xFF|00|00); // red circle
 
ppm.write(File("foo.ppm","wb"));</lang>
 
{{omit from|PARI/GP}}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.