绘制随机圆圈,将不与另一个圆圈相交的任何圆圈涂成红色

Draw random circles, coloring in red any circle not intersecting another circle

我有一个 Java Swing 作业,其中 objectives:

  1. 当程序启动时,它会绘制 20 个未填充的圆,每个圆的半径和位置是随机确定的。
  2. 如果一个圆的周线不与任何其他圆相交,则用红色绘制圆的轮廓。如果它确实与至少一个其他圆圈相交,请将其绘制为黑色。
  3. 添加一个 JButton,每次按下它时,都会创建一组新的圆圈,如上所述。

我已经完成了上面的 objective#1 和#3,但我在 objective#2 上遇到了困难。

在展示代码之前,让我先介绍一下我对代码背后数学原理的理解。一个圆不能与另一个圆相交的方式有两种:

  1. 圆之间的距离太远,无法共用一个圆周点,即圆心之间的距离大于半径之和 (d > r1 + r2)。 Example.
  2. 一个圆完全在另一个圆的里面,并且它们的周长不相交,即它们的圆心之间的距离小于它们的半径之差(d < |r1 - r2|)。 Example.

到目前为止我得到了什么:

  1. 要比较圆,必须在绘制之前指定它们,所以我使用for循环将20个值存储在数组中,用于中心坐标(int[] x,int[] y)和半径(双 [] 半径)。
  2. 接下来,我使用嵌套的 for 循环遍历数组并比较两个圆,除非圆与自身进行比较(索引 j = 索引 k)。如果圆圈相交,则 g.setColor(Color.RED)。如果没有,g.setColor(Color.BLACK).

当我执行我的代码时,没有任何重叠的圆圈被正确地涂成红色。然而,一些重叠的圆圈也是红色的。我假设它们在绘制时 不重叠 ,但此后相交。如何修复代码以及时解决这种差异? (问题区域位于底部附近,在 IntersectingCircles class)

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;

public class ButtonFrame extends JFrame 
{
    private final JButton resetButton = new JButton("Reset");

    public ButtonFrame()
    {
        super("Drawing Random Circles");
        setLayout(new BorderLayout());

        IntersectingCircles intersectingCircles = new IntersectingCircles();

        this.add(intersectingCircles, BorderLayout.CENTER);
        this.add(resetButton, BorderLayout.SOUTH);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(1400, 1400);

        ButtonHandler handler = new ButtonHandler();
        resetButton.addActionListener(handler);
    }

    private class ButtonHandler implements ActionListener
    {       
        @Override
        public void actionPerformed(ActionEvent event)
        {
            reset();
        }
    }

    public static void main(String[] args)
    {
        ButtonFrame buttonFrame = new ButtonFrame();
        buttonFrame.setVisible(true);
    }

    public void reset()
    {
        ButtonFrame buttonFrame = new ButtonFrame();
        buttonFrame.setVisible(true);
    }
}

class IntersectingCircles extends JPanel
{   
    private static final JButton resetButton = new JButton("Reset Circles");
    private static final JFrame frame = new JFrame("Intersecting Circles");

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        this.setBackground(Color.WHITE);

        int[] x = new int[20];
        int[] y = new int[20];
        int[] diameter = new int[20];
        double[] radius = new double[20]; 

        for (int i = 0; i < 20; i++)
        {
            int xCoord = (int)(Math.random() * 600);
            int yCoord = (int)(Math.random() * 600);
            int circleSize = (int)(Math.random() * 550);
            x[i] = xCoord;
            y[i] = yCoord;
            diameter[i] = circleSize;
            radius[i] = circleSize / 2.0;
        }

        for (int j = 0; j < 20; j++)
        {   
            for (int k = 0; k < 20; k++)
            {   
                if (k != j)
                {
                    if (((Math.sqrt((x[k] - x[j]) * (x[k] - x[j]) + (y[k] - y[j])
                        * (y[k] - y[j]))) > (radius[j] + radius[k])) ||
                        ((Math.sqrt((x[k] - x[j]) * (x[k] - x[j]) + (y[k] - y[j])
                        * (y[k] - y[j]))) < (Math.abs(radius[j] - radius[k]))))
                        g.setColor(Color.RED);
                    else
                        g.setColor(Color.BLACK);

                g.drawOval(x[j], y[j], diameter[j], diameter[j]);
                }
                else
                    continue;
            }
        }
    }
}

您在循环内的 if 语句中存在逻辑错误 - 您可以设置黑色,然后将其他对圆恢复为红色。可能的解决方案草案:

  for (int j = 0; j < 20; j++)
    {   
        g.setColor(Color.RED);  //set non-intersect state
        for (int k = j + 1; k < 20; k++)  //avoid excessive work
        {   
                if (intersect test)
                  {
                    g.setColor(Color.BLACK);
                    break; //can stop here
                  };
            g.drawOval(x[j], y[j], diameter[j], diameter[j]);
        }
}