Jump to content

Bitmap/Midpoint circle algorithm: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|Perl 6}}: update, add classes from Bitmap task to make runnable example)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 65:
 
</pre>
 
=={{header|ALGOL 68}}==
{{trans|Ada}}
Line 140 ⟶ 141:
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
</pre>
=={{header|BASIC256}}==
<lang basic256>fastgraphics
clg
color red
call DrawCircle(150,100,100)
refresh
color blue
call DrawCircle(200,200,50)
refresh
 
#Function DrawCircle
#1st param = X-coord of center
#2nd param = Y-coord of center
#3rd param = radius
Function DrawCircle(x0,y0,radius)
x=radius
y=0
decisionOver2=1-x
 
while x>=y
plot( x + x0, y + y0)
plot( y + x0, x + y0)
plot(-x + x0, y + y0)
plot(-y + x0, x + y0)
plot(-x + x0, -y + y0)
plot(-y + x0, -x + y0)
plot( x + x0, -y + y0)
plot( y + x0, -x + y0)
 
y++
 
if decisionOver2<=0 then
decisionOver2+=2*y+1
else
x--
decisionOver2+=2*(y-x)+1
end if
end while
return 0
End Function</lang>
{{Out}}
[http://s16.postimg.org/ge0ndfs9h/Output.jpg http://s16.postimg.org/ge0ndfs9h/Output.jpg]
 
=={{header|bash}}==
Line 263 ⟶ 222:
----##---------##----
------#########------</pre>
 
=={{header|BASIC256}}==
<lang basic256>fastgraphics
clg
color red
call DrawCircle(150,100,100)
refresh
color blue
call DrawCircle(200,200,50)
refresh
 
#Function DrawCircle
#1st param = X-coord of center
#2nd param = Y-coord of center
#3rd param = radius
Function DrawCircle(x0,y0,radius)
x=radius
y=0
decisionOver2=1-x
 
while x>=y
plot( x + x0, y + y0)
plot( y + x0, x + y0)
plot(-x + x0, y + y0)
plot(-y + x0, x + y0)
plot(-x + x0, -y + y0)
plot(-y + x0, -x + y0)
plot( x + x0, -y + y0)
plot( y + x0, -x + y0)
 
y++
 
if decisionOver2<=0 then
decisionOver2+=2*y+1
else
x--
decisionOver2+=2*(y-x)+1
end if
end while
return 0
End Function</lang>
{{Out}}
[http://s16.postimg.org/ge0ndfs9h/Output.jpg http://s16.postimg.org/ge0ndfs9h/Output.jpg]
 
=={{header|Batch File}}==
Line 592 ⟶ 594:
nil
</pre>
 
=={{header|Common Lisp}}==
Based upon the OCaml version.
Line 654 ⟶ 657:
( ))
</pre>
 
=={{header|D}}==
Uses the bitmap module from the Bitmap Task.
Line 924 ⟶ 928:
call draw_circle_toch(img%channel, c, radius, lum)
end subroutine draw_circle_sc</lang>
 
=={{header|FreeBASIC}}==
<lang FreeBASIC>' version 15-10-2016
Line 1,371 ⟶ 1,376:
PPM.Create("testpic.ppm", testpic);
END Main.</lang>
 
=={{header|Perl 6}}==
{{trans|C}}
We'll augment the Pixel and Bitmap classes from the [[Bitmap#Perl_6|Bitmap]] task.
 
<lang perl6>use MONKEY-TYPING;
 
class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
has Pixel @!data;
 
method fill(Pixel $p) {
@!data = $p.clone xx ($!width*$!height)
}
method pixel(
$i where ^$!width,
$j where ^$!height
--> Pixel
) is rw { @!data[$i + $j * $!width] }
 
method set-pixel ($i, $j, Pixel $p) {
self.pixel($i, $j) = $p.clone;
}
method get-pixel ($i, $j) returns Pixel {
self.pixel($i, $j);
}
}
 
augment class Pixel { method Str { "$.R $.G $.B" } }
augment class Bitmap {
method P3 {
join "\n", «P3 "$.width $.height" 255»,
do for ^$.height { join ' ', @.data[]»[$_] }
}
method raster-circle ( $x0, $y0, $r, Pixel $value ) {
my $f = 1 - $r;
my $ddF_x = 0;
my $ddF_y = -2 * $r;
my ($x, $y) = 0, $r;
self.set-pixel($x0, $y0 + $r, $value);
self.set-pixel($x0, $y0 - $r, $value);
self.set-pixel($x0 + $r, $y0, $value);
self.set-pixel($x0 - $r, $y0, $value);
while $x < $y {
if $f >= 0 {
$y--;
$ddF_y += 2;
$f += $ddF_y;
}
$x++;
$ddF_x += 2;
$f += $ddF_x + 1;
self.set-pixel($x0 + $x, $y0 + $y, $value);
self.set-pixel($x0 - $x, $y0 + $y, $value);
self.set-pixel($x0 + $x, $y0 - $y, $value);
self.set-pixel($x0 - $x, $y0 - $y, $value);
self.set-pixel($x0 + $y, $y0 + $x, $value);
self.set-pixel($x0 - $y, $y0 + $x, $value);
self.set-pixel($x0 + $y, $y0 - $x, $value);
self.set-pixel($x0 - $y, $y0 - $x, $value);
}
}
}</lang>
 
== {{Header|OCaml}} ==
 
<lang ocaml>let raster_circle ~img ~color ~c:(x0, y0) ~r =
let plot = put_pixel img color in
let x = 0
and y = r
and m = 5 - 4 * r
in
let rec loop x y m =
plot (x0 + x) (y0 + y);
plot (x0 + y) (y0 + x);
plot (x0 - x) (y0 + y);
plot (x0 - y) (y0 + x);
plot (x0 + x) (y0 - y);
plot (x0 + y) (y0 - x);
plot (x0 - x) (y0 - y);
plot (x0 - y) (y0 - x);
let y, m =
if m > 0
then (y - 1), (m - 8 * y)
else y, m
in
if x <= y then
let x = x + 1 in
let m = m + 8 * x + 4 in
loop x y m
in
loop x y m
;;</lang>
 
=={{header|Phix}}==
Line 1,803 ⟶ 1,714:
bm
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|C}}
We'll augment the Pixel and Bitmap classes from the [[Bitmap#Perl_6|Bitmap]] task.
 
<lang perl6>use MONKEY-TYPING;
 
class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
has Pixel @!data;
 
method fill(Pixel $p) {
@!data = $p.clone xx ($!width*$!height)
}
method pixel(
$i where ^$!width,
$j where ^$!height
--> Pixel
) is rw { @!data[$i + $j * $!width] }
 
method set-pixel ($i, $j, Pixel $p) {
self.pixel($i, $j) = $p.clone;
}
method get-pixel ($i, $j) returns Pixel {
self.pixel($i, $j);
}
}
 
augment class Pixel { method Str { "$.R $.G $.B" } }
augment class Bitmap {
method P3 {
join "\n", «P3 "$.width $.height" 255»,
do for ^$.height { join ' ', @.data[]»[$_] }
}
method raster-circle ( $x0, $y0, $r, Pixel $value ) {
my $f = 1 - $r;
my $ddF_x = 0;
my $ddF_y = -2 * $r;
my ($x, $y) = 0, $r;
self.set-pixel($x0, $y0 + $r, $value);
self.set-pixel($x0, $y0 - $r, $value);
self.set-pixel($x0 + $r, $y0, $value);
self.set-pixel($x0 - $r, $y0, $value);
while $x < $y {
if $f >= 0 {
$y--;
$ddF_y += 2;
$f += $ddF_y;
}
$x++;
$ddF_x += 2;
$f += $ddF_x + 1;
self.set-pixel($x0 + $x, $y0 + $y, $value);
self.set-pixel($x0 - $x, $y0 + $y, $value);
self.set-pixel($x0 + $x, $y0 - $y, $value);
self.set-pixel($x0 - $x, $y0 - $y, $value);
self.set-pixel($x0 + $y, $y0 + $x, $value);
self.set-pixel($x0 - $y, $y0 + $x, $value);
self.set-pixel($x0 + $y, $y0 - $x, $value);
self.set-pixel($x0 - $y, $y0 - $x, $value);
}
}
}</lang>
 
== {{Header|OCaml}} ==
 
<lang ocaml>let raster_circle ~img ~color ~c:(x0, y0) ~r =
let plot = put_pixel img color in
let x = 0
and y = r
and m = 5 - 4 * r
in
let rec loop x y m =
plot (x0 + x) (y0 + y);
plot (x0 + y) (y0 + x);
plot (x0 - x) (y0 + y);
plot (x0 - y) (y0 + x);
plot (x0 + x) (y0 - y);
plot (x0 + y) (y0 - x);
plot (x0 - x) (y0 - y);
plot (x0 - y) (y0 - x);
let y, m =
if m > 0
then (y - 1), (m - 8 * y)
else y, m
in
if x <= y then
let x = x + 1 in
let m = m + 8 * x + 4 in
loop x y m
in
loop x y m
;;</lang>
 
=={{header|REXX}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.