Draw random circles, red any circle that doesn't intersect another circle

I have a Java Swing assignment with the following goals:

  • When the program starts, it draws 20 unfilled circles with a radius and location of each randomly determined.
  • If the perimeter line of the circle does NOT intersect any other circle, draw a circle outline in RED. If it crosses at least one other circle, draw it in BLACK.
  • Add a JButton, which each time you press creates a new set of circles, as described above.

I have completed goals number 1 and number 3 above, but I am set for goal number 2.

Before introducing the code, let me talk about the math behind this. There are two ways: a circle cannot intersect with another circle:

  • The circles are too far apart to divide the perimeter, i.e. the distance between their centers is greater than the sum of their radii (d> r1 + r2). An example .
  • One circle is completely inside another circle, and their perimeters do not touch, i.e. the distance between their centers is less than the difference between their radii (d <| r1 - r2 |). An example .

What I have so far:

  • To compare circles, they must be specified before they are drawn, so I used a for loop to store 20 values ​​in arrays for central coordinates (int [] x, int [] y) and radius (double [] radius).
  • inested for-loops , , ( 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;
            }
        }
    }
}
+4
1

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

Source: https://habr.com/ru/post/1652779/


All Articles