我正在遍历一个圆(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;
}