gpt4 book ai didi

java - Bresenham画圆算法Java实现

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:41 31 4
gpt4 key购买 nike

我已经用 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();
}

enter image description here

最佳答案

注意原来的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();
}

结果:

enter image description here

还要注意 aspect ratio .此算法适用于比率 1:1。但是,如果您的宽高比是 a:b,您的圆方程将变为 x^2/a^2+y^2/b^2=r^2。您可以相应地更改更新。

关于java - Bresenham画圆算法Java实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35383299/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com