【问题标题】:How to detect overlapping circles and fill color accordingly?如何检测重叠的圆圈并相应地填充颜色?
【发布时间】:2010-11-13 04:05:37
【问题描述】:

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

这是我迄今为止为 circle 类所写的内容。 下面的代码会成功在一个小程序窗口中绘制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));

}
}

【问题讨论】:

    标签: java colors distance collision geometry


    【解决方案1】:

    xpoint、ypoint 等线没有按照您的想法进行。

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

    所以:

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

    【讨论】:

    • 我想我在这里做 - else if (theDistance
    • xpoint, ... 行正在扭曲您的数据,我的功能是所有您必须做的。
    【解决方案2】:

    你为什么在这里+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]);
    

    【讨论】:

    • 但是当你使用 '''page.fillOval(xpt[i], ypt[i], rad[i], rad[i]);''' 它从左上角绘制的角,所以我必须将半径长度添加到 x 和 y 坐标才能找到绘制圆的中心
    • 不,你误会我了。我已经用代码更新了答案以澄清。
    猜你喜欢
    • 2022-06-29
    • 2016-05-21
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2011-09-25
    相关资源
    最近更新 更多