我正在迭代圆圈(r)的可能半径,当我找到潜在的候选人时,我基本上想把它们推到一个堆栈上。我只需要记住最后两个半径和它们对应的圆的颜色,所以我创建了四个整数来存储这些变量。但是,我包含的print语句总是为两个变量产生值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;
}
答案 0 :(得分:1)
此示例中没有代码分配给四个变量中的任何一个。
如果您正在confirmCircle()
内进行分配,那么问题在于,当您在Java中传递值时,您期望int
是传递引用。
换句话说:
int a = 4;
public void changeA(int someVar) {
someVar++;
}
changeA(a);
System.out.println(a); //prints 4, not 5.