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

标签 java arrays swing geometry jpanel

我有一个 Java Swing 任务,目标如下:

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

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

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

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

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

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

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

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]);
        }
}

关于java - 绘制随机圆圈,将不与另一个圆圈相交的任何圆圈涂成红色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39196674/

相关文章:

java - 将文件中的文本读取到 JTextField 数组中

java - Android facebook sdk 使用graph api

java - 删除 JComboBox 中的向下箭头

python:如何将元组列表转换为numpy数组

c - 将结构体数组作为指针传递给函数 (C)

java - 监听和响应面板内的事件?

java - 在 Java Swing 中打印的单元测试

java - Java 中类似 JSON 的对象语法

java - 安卓+OpenCV : process camera frames in a service

python - 将单个 numpy 数组的值添加到其他 numpy 数组中的所有列