A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
|
|
@ -0,0 +1,24 @@
|
|||
NB.*getBresenhamCircle v Returns points for a circle given center and radius
|
||||
NB. y is: y0 x0 radius
|
||||
getBresenhamCircle=: monad define
|
||||
'y0 x0 radius'=. y
|
||||
x=. 0
|
||||
y=. radius
|
||||
f=. -. radius
|
||||
pts=. 0 2$0
|
||||
while. x <: y do.
|
||||
pts=. pts , y , x
|
||||
if. f >: 0 do.
|
||||
y=. <:y
|
||||
f=. f + _2 * y
|
||||
end.
|
||||
x=. >:x
|
||||
f =. f + >: 2 * x
|
||||
end.
|
||||
offsets=. (,|."1) (1 _1 {~ #: i.4) *"1"1 _ pts
|
||||
~.,/ (y0,x0) +"1 offsets
|
||||
)
|
||||
|
||||
NB.*drawCircles v Draws circle(s) (x) on image (y)
|
||||
NB. x is: 2-item list of boxed (y0 x0 radius) ; (color)
|
||||
drawCircles=: (1&{:: ;~ [: ; [: <@getBresenhamCircle"1 (0&{::))@[ setPixels ]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
myimg=: 0 255 0 makeRGB 25 25 NB. 25 by 25 green image
|
||||
myimg=: (12 12 12 ; 255 0 0) drawCircles myimg NB. draw red circle with radius 12
|
||||
viewRGB ((12 12 9 ,: 12 12 6) ; 0 0 255) drawCircles myimg NB. draw two more concentric circles
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
import java.awt.Color;
|
||||
|
||||
public class MidPointCircle {
|
||||
private BasicBitmapStorage image;
|
||||
|
||||
public MidPointCircle(final int imageWidth, final int imageHeight) {
|
||||
this.image = new BasicBitmapStorage(imageWidth, imageHeight);
|
||||
}
|
||||
|
||||
private void drawCircle(final int centerX, final int centerY, final int radius) {
|
||||
int d = (5 - r * 4)/4;
|
||||
int x = 0;
|
||||
int y = radius;
|
||||
Color circleColor = Color.white;
|
||||
|
||||
do {
|
||||
image.setPixel(centerX + x, centerY + y, circleColor);
|
||||
image.setPixel(centerX + x, centerY - y, circleColor);
|
||||
image.setPixel(centerX - x, centerY + y, circleColor);
|
||||
image.setPixel(centerX - x, centerY - y, circleColor);
|
||||
image.setPixel(centerX + y, centerY + x, circleColor);
|
||||
image.setPixel(centerX + y, centerY - x, circleColor);
|
||||
image.setPixel(centerX - y, centerY + x, circleColor);
|
||||
image.setPixel(centerX - y, centerY - x, circleColor);
|
||||
if (d < 0) {
|
||||
d += 2 * x + 1;
|
||||
} else {
|
||||
d += 2 * (x - y) + 1;
|
||||
y--;
|
||||
}
|
||||
x++;
|
||||
} while (x <= y);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
SetAttributes[drawcircle, HoldFirst];
|
||||
drawcircle[img_, {x0_, y0_}, r_, color_: White] :=
|
||||
Module[{f = 1 - r, ddfx = 1, ddfy = -2 r, x = 0, y = r,
|
||||
pixels = {{0, r}, {0, -r}, {r, 0}, {-r, 0}}},
|
||||
While[x < y,
|
||||
If[f >= 0, y--; ddfy += 2; f += ddfy];
|
||||
x++; ddfx += 2; f += ddfx;
|
||||
pixels = Join[pixels, {{x, y}, {x, -y}, {-x, y}, {-x, -y},
|
||||
{y, x}, {y, -x}, {-y, x}, {-y, -x}}]];
|
||||
img = ReplacePixelValue[img, {x0, y0} + # -> color & /@ pixels]]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
img = ExampleData[{"TestImage", "Lena"}];
|
||||
drawcircle[img, {250, 250}, 100]
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
INTERFACE Circle;
|
||||
|
||||
IMPORT Bitmap;
|
||||
|
||||
PROCEDURE Draw(
|
||||
img: Bitmap.T;
|
||||
center: Bitmap.Point;
|
||||
radius: CARDINAL;
|
||||
color: Bitmap.Pixel);
|
||||
|
||||
END Circle.
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
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.
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
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}, 10, Bitmap.Black);
|
||||
PPM.Create("testpic.ppm", testpic);
|
||||
END Main.
|
||||
|
|
@ -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
|
||||
;;
|
||||
Loading…
Add table
Add a link
Reference in a new issue