2010-11-13 73 views
0

我创建了5个圆形,使用3个数组(x,y和半径大小)随机选择x和y坐标和半径。但是,我需要圆圈根据是否与另一个圆圈重叠来动态更改颜色。所以如果5个圆圈中的一个完全不重叠,它应该被涂成黑色。重叠的圆应该是青色。如果两个圆的中心点之间的距离小于它们的半径之和,则认为两个圆重叠。如何检测重叠的圆形并相应地填充颜色?

这是我迄今为圈子类写的。 以下代码将成功绘制小程序窗口中的5个圆圈,并且距离已成功计算,但问题与着色有关。颜色填充似乎存在逻辑错误,我在这里看不到问题。有什么建议么?非常感谢。

public class Circles extends Applet { 

public void paint(Graphics page) 
{ 
    Random locator = new Random(); 
    int [] xpt = new int [5]; 
    int [] ypt = new int [5]; 
    int [] rad = new int [5]; 

    setPreferredSize (new Dimension(300, 300)); 
    for (int i = 0; i < xpt.length; i++){ 

     xpt[i] = locator.nextInt(100); //need to set a number or it goes into millions, cannot set it in Random() 
     ypt[i] = locator.nextInt(100); 
     rad[i] = locator.nextInt(100); 
     System.out.println("The #" + i + " x-point: " + xpt[i] + " y-point: " + ypt[i] + " radius: " + rad[i]); //for debugging purposes 

     for (int j = 0; j < xpt.length; j++){ 
      double xpoint1 = xpt[i]+rad[i]; 
      double ypoint1 = ypt[i]+rad[i]; 
      double xpoint2 = xpt[j]+rad[j]; 
      double ypoint2 = ypt[j]+rad[j]; 
      double radius1 = rad[i]; 
      double radius2 = rad[j]; 
      double theDistance = distance(xpoint1,ypoint1,xpoint2,ypoint2); 
      System.out.println("Comparing " + i + " to " + j); //for debugging and logic checking 
      if (i==j) 
       ; 
      else if (theDistance <= (radius1+radius2)) 
      { 
       page.setColor(Color.cyan); 
       page.fillOval(xpt[i], ypt[i], rad[i], rad[i]); 
       //page.fillOval(xpt[j], ypt[j], rad[j], rad[j]); 
       System.out.println("Overlap occurred. Colored " + i + " and " + j + " cyan."); 
       System.out.println("Center points: ("+ xpoint1 +", "+ ypoint1 +") and ("+ xpoint2 + ", "+ ypoint2 + ")."); 
      } 
      else 
      { 
       page.setColor(Color.black); 
       page.fillOval(xpt[i], ypt[i], rad[i], rad[i]); 
       //page.fillOval(xpt[j], ypt[j], rad[j], rad[j]); 
       System.out.println("No overlap. Made " + i + " and " + j + " black."); 
      } 
     } 
    } 
} 

public static double distance(
     double x1, double y1, double x2, double y2) { 
    return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); 

} 
} 

回答

1

为什么你+rad[]在这里?您不必添加半径来比较距离。

 double xpoint1 = xpt[i]+rad[i]; 
     double ypoint1 = ypt[i]+rad[i]; 
     double xpoint2 = xpt[j]+rad[j]; 
     double ypoint2 = ypt[j]+rad[j]; 
[...] 
     double theDistance = distance(xpoint1,ypoint1,xpoint2,ypoint2); 
[...] 
     page.fillOval(xpt[i], ypt[i], rad[i], rad[i]); 

您应该使用xpt/ypt的距离。不xpoint1,并使用-的价值和2 *半径大小......即:

 double xpoint1 = xpt[i]-rad[i]; 
     double ypoint1 = ypt[i]-rad[i]; 
     double xpoint2 = xpt[j]-rad[j]; 
     double ypoint2 = ypt[j]-rad[j]; 
[...] 
     double theDistance = distance(xpt[i],ypt[i],xpt[j],ypt[j]); 
[...] 
     page.fillOval(xpoint1 , ypoint1, 2*rad[i], 2*rad[i]); 
+0

但是当你使用 '' 'page.fillOval(XPT [I],展翅[I],弧度[I],弧度[1]);' '' 从平在角落的左上角,所以我必须将半径长度添加到x和y坐标以便找到绘制的圆的中心 – sxflynn 2010-11-13 04:47:02

+0

不,您误解了我。我已经用代码更新了答案,以便澄清。 – 2010-11-13 05:01:46

4

的XPOINT,ypoint等线路是不是做你的想法。

如果要查找两个圆是否重叠,则需要查找圆的中心之间的距离是大于还是小于它们的半径之和。

所以:

function circlesCollide(x1, y1, r1, x2, y2, r2){ 
    return (distance(x1, y1, x2, y2) <= (r1 + r2)); 
} 
+0

我想我在这里做 - 其他如果(theDistance <=(radius1 + radius2)) – sxflynn 2010-11-13 04:49:32

+0

xpoint,...线倾斜您的数据,我的功能是_all_你必须做的。 – 2010-11-13 04:55:58