Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,2 +1,4 @@
|
|||
Using the data storage type defined [[Basic_bitmap_storage|on this page]] for raster images, write an implementation of the '''midpoint circle algorithm''' (also known as '''Bresenham's circle algorithm'''). <BR>
|
||||
Using the data storage type defined [[Basic_bitmap_storage|on this page]] for raster images,
|
||||
write an implementation of the '''midpoint circle algorithm'''
|
||||
(also known as '''Bresenham's circle algorithm'''). <BR>
|
||||
([[wp:Midpoint_circle_algorithm|definition on Wikipedia]]).
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import bitmap: Image, RGB;
|
||||
|
||||
void circle(Color)(Image!Color img, in int x0, in int y0,
|
||||
in int radius, in Color color) pure nothrow {
|
||||
in int radius, in Color color)
|
||||
pure nothrow @nogc @safe {
|
||||
int f = 1 - radius;
|
||||
int ddfX = 1;
|
||||
int ddfY = -2 * radius;
|
||||
|
|
@ -32,9 +33,9 @@ void circle(Color)(Image!Color img, in int x0, in int y0,
|
|||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
void main() @safe {
|
||||
auto img = new Image!RGB(25, 25);
|
||||
img.clear(RGB.white);
|
||||
circle(img, 12, 12, 12, RGB.black);
|
||||
img.textualShow();
|
||||
img.textualShow;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +1,36 @@
|
|||
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
|
||||
;;
|
||||
use MONKEY_TYPING;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
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
|
||||
;;
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/* Plot three circles. */
|
||||
|
||||
CIRCLE: PROCEDURE OPTIONS (MAIN);
|
||||
declare image (-20:20, -20:20) character (1);
|
||||
declare j fixed binary;
|
||||
|
||||
image = '.';
|
||||
image(0,*) = '-';
|
||||
image(*,0) = '|';
|
||||
image(0,0) = '+';
|
||||
|
||||
CALL DRAW_CIRCLE (0, 0, 11);
|
||||
CALL DRAW_CIRCLE (0, 0, 8);
|
||||
CALL DRAW_CIRCLE (0, 0, 19);
|
||||
|
||||
do j = hbound(image,1) to lbound(image,1) by -1;
|
||||
put skip edit (image(j,*)) (a(1));
|
||||
end;
|
||||
|
||||
draw_circle: procedure (x0, y0, radius); /* 14 May 2010. */
|
||||
declare ( x0, y0, radius ) fixed binary;
|
||||
declare ( ddfx, ddfy, x, y, f ) fixed binary;
|
||||
declare debug bit (1) aligned static initial ('0'b);
|
||||
|
||||
f = 1-radius;
|
||||
ddfx = 1;
|
||||
ddfy = -2*radius;
|
||||
x = 0;
|
||||
y = radius;
|
||||
image(x0, y0+radius) = '*'; /* Octet 0. */
|
||||
image(x0+radius, y0) = '*'; /* Octet 1. */
|
||||
image(x0, y0-radius) = '*'; /* Octet 2. */
|
||||
image(x0-radius, y0) = '*'; /* Octet 3. */
|
||||
|
||||
do while (x < y);
|
||||
if f >= 0 then
|
||||
do; y = y - 1; ddfy = ddfy +2; f = f + ddfy; end;
|
||||
x = x + 1;
|
||||
ddfx = ddfx + 2;
|
||||
f = f + ddfx;
|
||||
image(x0+x, y0+y) = '0'; /* Draws octant 0. */
|
||||
image(x0+y, y0+x) = '1'; /* Draws octant 1. */
|
||||
image(x0+y, y0-x) = '2'; /* Draws octant 2. */
|
||||
image(x0+x, y0-y) = '3'; /* Draws octant 3. */
|
||||
image(x0-x, y0-y) = '4'; /* Draws octant 4. */
|
||||
image(x0-y, y0-x) = '5'; /* Draws octant 5. */
|
||||
image(x0-y, y0+x) = '6'; /* Draws octant 6. */
|
||||
image(x0-x, y0+y) = '7'; /* Draws octant 7. */
|
||||
end;
|
||||
end draw_circle;
|
||||
|
||||
END CIRCLE;
|
||||
|
|
@ -1,49 +1,43 @@
|
|||
/*REXX pgm plots 3 circles using midpoint/Bresenham's circle algorithm. */
|
||||
EoE = 200 /*EOE = End Of Earth, er, plot. */
|
||||
image. = 'fa'x /*fill the array with middle-dots*/
|
||||
pChar = '*'
|
||||
do j=-EoE to +EoE /*draw grid from lowest──>highest*/
|
||||
image.j.0 = '─' /*draw the horizontal axis. */
|
||||
image.0.j = '│' /* " " verical " */
|
||||
end /*j*/
|
||||
image.0.0='┼' /*"draw" the axis origin. */
|
||||
minX=0; maxX=0
|
||||
minY=0; maxY=0
|
||||
call draw_circle 0, 0, 8, '#'
|
||||
call draw_circle 0, 0, 11, '$'
|
||||
call draw_circle 0, 0, 19, '@'
|
||||
border=2
|
||||
minX=minX-border*2; maxX=maxX+border*2
|
||||
minY=minY-border ; maxY=maxY+border
|
||||
do y=maxY by -1 to minY; aRow=
|
||||
do x=minX to maxX
|
||||
aRow=aRow || image.x.y
|
||||
end /*x*/
|
||||
say aRow
|
||||
end /*y*/
|
||||
@. = '·' /*fill the array with middle-dots*/
|
||||
minX=0; maxX=0; minY=0; maxY=0 /*initialize minimums & maximums.*/
|
||||
call drawCircle 0, 0, 8, '#' /*plot 1st circle with pound char*/
|
||||
call drawCircle 0, 0, 11, '$' /* " 2nd " " dollar " */
|
||||
call drawCircle 0, 0, 19, '@' /* " 3rd " " commer. at*/
|
||||
border=2 /*BORDER: shows N extra grid pts*/
|
||||
minX=minX-border*2; maxX=maxX+border*2 /*adjust min&max X to show border*/
|
||||
minY=minY-border ; maxY=maxY+border /* " " " Y " " " */
|
||||
if @.0.0==@. then @.0.0='┼' /*maybe define plot's axis origin*/
|
||||
/* [↓] define horizontal grid. */
|
||||
do gx=minX to maxX; if @.gx.0==@. then @.gx.0='─'; end /*gx*/
|
||||
do gy=minY to maxY; if @.0.gy==@. then @.0.gy='│'; end /*gy*/
|
||||
/* [↑] define the vertical grid.*/
|
||||
do y=maxY by -1 to minY; aRow= /* [↓] draw grid from top to bot.*/
|
||||
do x=minX to maxX /* [↓] " " " left──►right*/
|
||||
aRow=aRow || @.x.y /*build a grid row, char by char.*/
|
||||
end /*x*/ /* [↑] a grid row should be done*/
|
||||
say aRow /*display signal row of the grid.*/
|
||||
end /*y*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*───────────────────────────────────DRAW_CIRCLE subroutine─────────────*/
|
||||
draw_circle: procedure expose image. minX maxX minY maxY
|
||||
parse arg xx, yy, r, point; f=1-r; ddfx=1; ddfy=-2*r; y=r
|
||||
_=yy+r; image.xx._='*'
|
||||
_=xx+r; image._.yy='*'
|
||||
_=yy-r; image.xx._='*'
|
||||
_=xx-r; image._.yy='*'
|
||||
do x=0 while x<y
|
||||
if f>=0 then do; y=y-1; ddfy=ddfy+2; f=f+ddfy; end
|
||||
ddfx=ddfx+2; f=f+ddfx
|
||||
x_=xx+x; y_=yy+y; call plotXY x_, y_, point
|
||||
x_=xx+y; y_=yy+x; call plotXY x_, y_, point
|
||||
x_=xx+y; y_=yy-x; call plotXY x_, y_, point
|
||||
x_=xx+x; y_=yy-y; call plotXY x_, y_, point
|
||||
x_=xx-x; y_=yy-y; call plotXY x_, y_, point
|
||||
x_=xx-y; y_=yy-x; call plotXY x_, y_, point
|
||||
x_=xx-y; y_=yy+x; call plotXY x_, y_, point
|
||||
x_=xx-x; y_=yy+y; call plotXY x_, y_, point
|
||||
end /*x*/
|
||||
/*───────────────────────────────────DRAWCIRCLE subroutine──────────────*/
|
||||
drawCircle: procedure expose @. minX maxX minY maxY
|
||||
parse arg xx,yy,r,plotChar; f=1-r; fx=1; fy=-2*r; y=r
|
||||
|
||||
do x=0 while x<y /*════════════════════════════════════════*/
|
||||
if f>=0 then do; y=y-1; fy=fy+2; f=f+fy; end
|
||||
fx=fx+2; f=f+fx
|
||||
call plotPoint xx+x, yy+y, plotChar
|
||||
call plotPoint xx+y, yy+x, plotChar
|
||||
call plotPoint xx+y, yy-x, plotChar
|
||||
call plotPoint xx+x, yy-y, plotChar
|
||||
call plotPoint xx-y, yy+x, plotChar
|
||||
call plotPoint xx-x, yy+y, plotChar
|
||||
call plotPoint xx-x, yy-y, plotChar
|
||||
call plotPoint xx-y, yy-x, plotChar
|
||||
end /*x*/ /* [↑] place plot points ══► plot*/
|
||||
return
|
||||
/*──────────────────────────────────PLOTXY subroutine───────────────────*/
|
||||
plotXY: procedure expose image. minX maxX minY maxY; parse arg xx, yy, p
|
||||
image.xx.yy=p; minX=min(minX,xx); maxX=max(maxX,xx)
|
||||
minY=min(minY,yy); maxY=max(maxY,yy)
|
||||
/*──────────────────────────────────PLOTPOINT subroutine────────────────*/
|
||||
plotPoint: procedure expose @. minX maxX minY maxY
|
||||
parse arg xx,yy,@.xx.yy; minX=min(minX,xx); maxX=max(maxX,xx)
|
||||
minY=min(minY,yy); maxY=max(maxY,yy)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue