【发布时间】:2013-01-12 01:15:44
【问题描述】:
我正在遍历圆 (r) 的可能半径,当我找到潜在的候选者时,我基本上想将它们推入堆栈。我只需要记住最后两个半径和它们对应的圆的颜色,所以我创建了四个整数来存储这些变量。但是,我包含的打印语句总是为两个变量生成 0 值。
//small stack to hold values of previous circles
int small_radius = 0;
int small_color = 0;
int med_radius = 0;
int med_color = 0;
//iterate through possible radii
while (r<max){
//check for possibility of a circle
if(detectCircle(x,y,r,img,g)){
//confirm it is a circle and store its color
int large_color = confirmCircle(x,y,r,img,g);
if(large_color != -1){
//if it is a circle, compare the medium circle against the current one and the small one
//check if the current circle and small circle do not immediately surround the medium circle
boolean matches_small = (med_radius-1 == small_radius && med_color == small_color);
boolean matches_large = (r-1 == med_radius && large_color == med_color);
if(!matches_small && !matches_large){
//if it is a circle of single line thickness, draw it
System.out.println("med_radius: "+med_radius+" small_radius: "+small_radius);
drawCircle(x,y,r,img,g);
}
//now push the current circle onto the stack.
small_radius = med_radius;
small_color = med_color;
med_radius = r;
med_color = large_color;
}
}
r++;
}
编辑:对于那些想知道 confirmCircle 是什么样子的人,这里是,但它不应该有所作为。
static int confirmCircle(int cx, int cy, int r, BufferedImage img, Graphics2D g) {
int color = img.getRGB(cx,cy+r);
int f = 1-r;
int ddF_x = 1;
int ddF_y = -2 * r;
int x = 0;
int y = r;
while(x < y) {
if(f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if(img.getRGB(cx+x,cy+y) != color){color = -1;}
if(img.getRGB(cx-x,cy+y) != color){color = -1;}
if(img.getRGB(cx+x,cy-y) != color){color = -1;}
if(img.getRGB(cx-x,cy-y) != color){color = -1;}
if(img.getRGB(cx+y,cy+x) != color){color = -1;}
if(img.getRGB(cx-y,cy+x) != color){color = -1;}
if(img.getRGB(cx+y,cy-x) != color){color = -1;}
if(img.getRGB(cx-y,cy-x) != color){color = -1;}
}
return color;
}
【问题讨论】:
-
调试器指向什么?你试过用吗?
-
没有
confirmCircle的代码,就无法知道问题出在哪里。如果它每次都返回 -1,你会得到你看到的结果。我们需要更多信息才能提供重要帮助。 -
您的 println 语句在分配 med_radius 之前。它应该使用 r 来显示 med_radius 吗?由于 med_radius 等于 r。
-
@JonathanDrapeau 如果返回 -1,则不调用打印语句。
-
奇怪.. 我刚刚测试了你的代码(最大值 = 10,伪造方法只返回将进入循环的值,并强制布尔值为假,因此它保证打印)并且打印了所有内容像它应该的那样递增......
标签: java variables loops scope