如何检测重叠圆圈并相应填充颜色?

时间:2010-11-13 04:05:38

标签: java colors distance collision geometry

我使用3个数组(x,y和半径大小)创建了5个带有随机x和y坐标和半径的圆。但是,我需要圆圈根据它们是否与另一个圆重叠来动态改变颜色。因此,如果5个圆圈中的一个根本不重叠,则应将其涂成黑色。重叠的圆圈应为青色。如果两个圆的中心点之间的距离小于它们的半径之和,则认为它们重叠。

这是我到目前为止为圆圈课所写的内容。 以下代码将在applet窗口中成功绘制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));

}
}

2 个答案:

答案 0 :(得分:4)

xpoint,ypoint等行没有按你的想法行事。

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

所以:

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

答案 1 :(得分: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]);
相关问题