📄 circle.java
字号:
package ergo.ui;
// $Id: Circle.java,v 1.4 1999/08/15 01:40:28 sigue Exp $
/*
* Copyright (C) 1999 Carl L. Gay and Antranig M. Basman.
* See the file copyright.txt, distributed with this software,
* for further information.
*/
/**
* Bresenham circle drawing.
*/
public class Circle {
CircleOdd odd = new CircleOdd();
CircleEven even = new CircleEven();
public void emptycirc (RawImage imag1, int color, int diameter) {
if (diameter % 2 == 1) {
odd.emptycirc(imag1, color, diameter);
}
else {
even.emptycirc(imag1, color, diameter);
}
}
public void fillcirc (RawImage imag1, int color, int diameter) {
if (diameter % 2 == 1) {
odd.fillcirc(imag1, color, diameter);
}
else {
even.fillcirc(imag1, color, diameter);
}
}
}
/**
* Deals with odd-sized extents(bitmaps).
*/
class CircleOdd {
private int d, x, y, r;
private int r2p1;
private int xorig, yorig;
private int cellSize, cellSize2;
private RawImage imag;
public void init (RawImage imag1) {
imag = imag1;
cellSize = imag.width;
cellSize2 = cellSize / 2;
xorig = yorig = cellSize2;
imag.setorig(xorig, yorig);
}
private void brezinit (int diameter) {
r = diameter / 2;
r2p1 = diameter;
d = 5 - diameter;
x = 0;
y = -r - 1;
}
private boolean breznext () {
if (r2p1 == 1)
return false;
boolean plipy = false;
if (d >= 0) {
d = d + 4 * (x + y) + 14;
++y;
plipy = true;
}
else
d = d + 4 * x + 8;
++x;
if ((x + y) > -1)
return false;
if ((x + y) == -1 && !plipy)
return false;
return true;
}
private void setband (int color, int x1, int y1) {
imag.setband(color, -x1-1, x1, y1);
}
public void emptycirc (RawImage imag1, int color, int diameter) {
init(imag1);
brezinit(diameter);
do {
imag.setpixel(color, x, y); imag.setpixel(color, y, x);
imag.setpixel(color, -x - 1, y); imag.setpixel(color, y, -x - 1);
imag.setpixel(color, x, -y - 1); imag.setpixel(color, -y - 1, x);
imag.setpixel(color, -x - 1, -y - 1); imag.setpixel(color, -y - 1, -x - 1);
} while (breznext());
}
public void fillcirc (RawImage imag1, int color, int diameter) {
init(imag1);
brezinit(diameter);
// setband(color, radius, 0);
int lasty = y;
int lastx = x;
// 1st time round is for free.
do {
// now, circle is drawn from position (0, -r); so up to band, we
// pass rotated 90o anticlockwise.... this draws 'fat half' of circle
setband(color, -y - 1, x);
setband(color, -y - 1, -x - 1);
if (y != lasty) { // if we took an upward step, draw the top and bottom
// slivers, too, as the last 'jutty out' bit we got up to....
setband(color, lastx, lasty);
setband(color, lastx, -lasty - 1);
}
lastx = x;
lasty = y;
} while (breznext()); // end breznext
setband(color, lastx, lasty); // Since breznext does not give us "symmetric" pixel,
setband(color, lastx, -lasty - 1); // Explicitly draw line before it.
}
} // end class circleOdd
/**
* Deals with even-sized extents(bitmaps).
*/
class CircleEven {
private int d, x, y, r;
private int xorig, yorig;
private int cellSize, cellSize2;
private RawImage imag;
public void init(RawImage imag1) {
imag = imag1;
cellSize = imag.width;
cellSize2 = cellSize / 2;
xorig = yorig = cellSize2;
imag.setorig(xorig, yorig);
}
private void brezinit (int diameter) {
r = diameter / 2;
d = 5 - 4 * r;
x = 0;
y = -r;
}
private boolean breznext () {
boolean plipy = false;
if (d >= 0) {
d = d + 8 * (x + y) + 20;
++y;
plipy = true;
}
else
d = d + 8 * x + 12;
++x;
if ((x + y) > 0)
return false;
if ((x + y) == 0 && !plipy)
return false;
return true;
}
private void setband (int color, int x1, int y1) {
imag.setband(color, -x1, x1, y1);
}
public void emptycirc (RawImage imag1, int color, int diameter) {
init(imag1);
brezinit(diameter);
imag.setpixel(color, 0, -r);
imag.setpixel(color, 0, r);
imag.setpixel(color, -r, 0); imag.setpixel(color, r, 0);
while (breznext()) {
imag.setpixel(color, x, y); imag.setpixel(color, y, x);
imag.setpixel(color, -x, y); imag.setpixel(color, y, -x);
imag.setpixel(color, x, -y); imag.setpixel(color, -y, x);
imag.setpixel(color, -x, -y); imag.setpixel(color, -y, -x);
}
}
public void fillcirc (RawImage imag1, int color, int diameter) {
init(imag1);
brezinit(diameter);
setband(color, r, 0); // Draw single 0th band
int lasty = y;
int lastx = x;
while (breznext()) {
// now, circle is drawn from position (0, -r); so up to band, we
// pass rotated 90o anticlockwise.... this draws 'fat half' of circle
setband(color, -y, x);
setband(color, -y, -x);
if (y != lasty) { // if we took an upward step, draw the top and bottom
// slivers, too, as the last 'jutty out' bit we got up to....
setband(color, lastx, lasty);
setband(color, lastx, -lasty);
}
lastx = x;
lasty = y;
} // end breznext
setband(color, lastx, lasty); // Since breznext does not give us "symmetric" pixel,
setband(color, lastx, -lasty); // Explicitly draw line before it.
}
} // end class CircleEven
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -