我在Java中实现了Bresenham Circle绘图算法。但输出没有正确绘制!我找不到问题所在。
我的代码和输出图像如下。任何帮助表示赞赏。
public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glBegin (GL2.GL_POINTS);
double radius = 0.6;//sc.nextDouble();
double x =0.0;
double y = radius;
gl.glVertex2d(0.0,0.0);
gl.glVertex2d(x,y);
gl.glVertex2d(-x,y);
gl.glVertex2d(x,-y);
gl.glVertex2d(-x,-y);
gl.glVertex2d(y,x);
gl.glVertex2d(-y,x);
gl.glVertex2d(y,-x);
gl.glVertex2d(-y,-x);
double d = 5 - 4*radius;
while(x<y){
if(d<0){ //dE
x+=.01;
d+=(2*x + 3)*4;
}else{
x+=.01;
y-=.01;
d+=(2*x - 2*y +5)*4;
}
gl.glVertex2d(x,y);
gl.glVertex2d(-x,y);
gl.glVertex2d(x,-y);
gl.glVertex2d(-x,-y);
gl.glVertex2d(y,x);
gl.glVertex2d(-y,x);
gl.glVertex2d(y,-x);
gl.glVertex2d(-y,-x);
}
gl.glEnd();
}
答案 0 :(得分:1)
请注意,原始Bresenham's circle algorithm仅适用于整数。由于您的更新为x_{n+1}=x_n+eps
,因此您可以将y
更新修改为
y_{n+1}^2 = y_n^2 - 2*eps*n-eps*eps
推导与维基页面上的推导相同。
public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glBegin (GL2.GL_POINTS);
double radius = 0.6;//sc.nextDouble();
double x =0.0;
double y = radius;
gl.glVertex2d(0.0,0.0);
gl.glVertex2d(x,y);
gl.glVertex2d(-x,y);
gl.glVertex2d(x,-y);
gl.glVertex2d(-x,-y);
gl.glVertex2d(y,x);
gl.glVertex2d(-y,x);
gl.glVertex2d(y,-x);
gl.glVertex2d(-y,-x);
double eps = .01;
double eps2 = eps*eps;
while(x<y){
y = Math.sqrt(y*y-2*eps*x-eps2);
x+= eps;
gl.glVertex2d(x,y);
gl.glVertex2d(-x,y);
gl.glVertex2d(x,-y);
gl.glVertex2d(-x,-y);
gl.glVertex2d(y,x);
gl.glVertex2d(-y,x);
gl.glVertex2d(y,-x);
gl.glVertex2d(-y,-x);
}
gl.glEnd();
}
结果:
还要小心aspect ratio。此算法适用于比率1:1
。但是,如果您的宽高比为a:b
,则圆圈的等式将变为x^2/a^2+y^2/b^2=r^2
。您可以相应地更改更新。
答案 1 :(得分:1)
以下是使用Bresenham算法绘制自定义圆的示例。 完整代码可以在我的仓库中找到:https://github.com/Maiakov/algorithms/tree/master/Task40
/**
* Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point
* <p>
* computations at all.
*/
public class DrawCircleAlgorithm {
public static void drawCircle(int radius, int centerX, int centerY, Graphics g) {
int y = radius;
int x = 0;
int delta = calculateStartDelta(radius);
while (y >= x) {
drawPixelAndReflect(centerX, centerY, x, y, g);
if (delta < 0) {
delta = calculateDeltaForHorizontalPixel(delta, x);
} else {
delta = calculateDeltaForDiagonalPixel(delta, x, y);
y--;
}
x++;
}
}
private static int calculateStartDelta(int radius) {
return 3 - 2 * radius;
}
private static int calculateDeltaForHorizontalPixel(int oldDelta, int x) {
return oldDelta + 4 * x + 6;
}
private static int calculateDeltaForDiagonalPixel(int oldDelta, int x, int y) {
return oldDelta + 4 * (x - y) + 10;
}
private static void drawPixelAndReflect(int centerX, int centerY, int x, int y, Graphics g) {
g.drawLine(centerX + x, centerY + y, centerX + x, centerY + y);
g.drawLine(centerX + x, centerY - y, centerX + x, centerY - y);
g.drawLine(centerX - x, centerY + y, centerX - x, centerY + y);
g.drawLine(centerX - x, centerY - y, centerX - x, centerY - y);
g.drawLine(centerX - y, centerY + x, centerX - y, centerY + x);
g.drawLine(centerX - y, centerY - x, centerX - y, centerY - x);
g.drawLine(centerX + y, centerY + x, centerX + y, centerY + x);
g.drawLine(centerX + y, centerY - x, centerX + y, centerY - x);
}
}
答案 2 :(得分:0)
试试这个。我现在在计算机上没有java,所以让我们看看它是否有效。确保使用整数,因为您正常化的是块大小!
修改:添加整数。
public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glBegin (GL2.GL_POINTS);
double radius = 0.6;//sc.nextDouble();
double x =0.0;
double y = radius;
gl.glVertex2d(0.0,0.0);
gl.glVertex2d(x,y);
gl.glVertex2d(-x,y);
gl.glVertex2d(x,-y);
gl.glVertex2d(-x,-y);
gl.glVertex2d(y,x);
gl.glVertex2d(-y,x);
gl.glVertex2d(y,-x);
gl.glVertex2d(-y,-x);
double d = 3 - 2*radius;
while(x<y){
x++;
if(d<0){ //dE
d= d + 4x + 6;
}else{
y--;
d= d+ 4(x - y) + 10;
}
gl.glVertex2d(x,y);
gl.glVertex2d(-x,y);
gl.glVertex2d(x,-y);
gl.glVertex2d(-x,-y);
gl.glVertex2d(y,x);
gl.glVertex2d(-y,x);
gl.glVertex2d(y,-x);
gl.glVertex2d(-y,-x);
}
gl.glEnd();
}