Bresenham算法绘制圆形的java代码
时间: 2024-05-10 12:21:43 浏览: 80
以下是使用Bresenham算法绘制圆形的Java代码示例:
```
import java.awt.Graphics;
public class BresenhamCircle {
private int xc, yc, r;
public BresenhamCircle(int xc, int yc, int r) {
this.xc = xc;
this.yc = yc;
this.r = r;
}
public void draw(Graphics g) {
int x = 0, y = r;
int d = 3 - 2 * r;
while (x <= y) {
plot(g, x, y);
if (d < 0)
d = d + 4 * x + 6;
else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
private void plot(Graphics g, int x, int y) {
g.drawLine(xc + x, yc + y, xc + x, yc + y);
g.drawLine(xc - x, yc + y, xc - x, yc + y);
g.drawLine(xc + x, yc - y, xc + x, yc - y);
g.drawLine(xc - x, yc - y, xc - x, yc - y);
g.drawLine(xc + y, yc + x, xc + y, yc + x);
g.drawLine(xc - y, yc + x, xc - y, yc + x);
g.drawLine(xc + y, yc - x, xc + y, yc - x);
g.drawLine(xc - y, yc - x, xc - y, yc - x);
}
}
```
在这个示例中,我们创建了一个名为BresenhamCircle的类,该类接受圆心坐标和半径作为参数,并使用Bresenham算法绘制圆形。draw方法用于在Graphics对象上绘制圆形,而plot方法用于绘制圆形的八个象限。
阅读全文