Bitmap/Midpoint circle algorithm: Difference between revisions

Modula-3
m (→‎{{header|Vedit macro language}}: fixed typo and +lang (vedit?))
(Modula-3)
Line 232:
call draw_circle_toch(img%channel, c, radius, lum)
end subroutine draw_circle_sc</lang>
 
=={{header|Modula-3}}==
<lang modula3>INTERFACE Circle;
 
IMPORT Bitmap;
 
PROCEDURE Draw(
img: Bitmap.T;
center: Bitmap.Point;
radius: CARDINAL;
color: Bitmap.Pixel);
END Circle.</lang>
<lang modula3>MODULE Circle;
 
IMPORT Bitmap;
 
PROCEDURE Draw(
img: Bitmap.T;
center: Bitmap.Point;
radius: CARDINAL;
color: Bitmap.Pixel) =
VAR f := 1 - radius;
ddfx := 0;
ddfy := - 2 * radius;
x := 0;
y := radius;
BEGIN
Bitmap.SetPixel(img, Bitmap.Point{center.x, center.y + radius}, color);
Bitmap.SetPixel(img, Bitmap.Point{center.x, center.y - radius}, color);
Bitmap.SetPixel(img, Bitmap.Point{center.x + radius, center.y}, color);
Bitmap.SetPixel(img, Bitmap.Point{center.x - radius, center.y}, color);
WHILE x < y DO
IF f >= 0 THEN
y := y - 1;
ddfy := ddfy + 2;
f := f + ddfy;
END;
x := x + 1;
ddfx := ddfx + 2;
f := f + ddfx + 1;
Bitmap.SetPixel(img, Bitmap.Point{center.x + x, center.y + y}, color);
Bitmap.SetPixel(img, Bitmap.Point{center.x - x, center.y + y}, color);
Bitmap.SetPixel(img, Bitmap.Point{center.x + x, center.y - y}, color);
Bitmap.SetPixel(img, Bitmap.Point{center.x - x, center.y - y}, color);
Bitmap.SetPixel(img, Bitmap.Point{center.x + y, center.y + x}, color);
Bitmap.SetPixel(img, Bitmap.Point{center.x - y, center.y + x}, color);
Bitmap.SetPixel(img, Bitmap.Point{center.x + y, center.y - x}, color);
Bitmap.SetPixel(img, Bitmap.Point{center.x - y, center.y - x}, color);
END;
END Draw;
 
BEGIN
END Circle.</lang>
 
Example (outputs a [[http://www.rosettacode.org/wiki/Write_ppm_file|PPM]] image):
<lang modula3>MODULE Main;
 
IMPORT Circle, Bitmap, PPM;
 
VAR testpic: Bitmap.T;
 
BEGIN
testpic := Bitmap.NewImage(32, 32);
Bitmap.Fill(testpic, Bitmap.White);
Circle.Draw(testpic, Bitmap.Point{16, 16}, 5, Bitmap.Black);
PPM.Create("testpic.ppm", testpic);
END Main.</lang>
 
== {{Header|OCaml}} ==
Anonymous user