adidashawn6
adidashawn6

Reputation: 49

Determine if circles intersect

I am working on a project where I have to draw 20 circles with random starting points and random sizes. Then I have to determine if any of the circles intersect. If a circle intersects with another, I have to color that circle green. And if the circle does not intersect with another, the color needs to be red. I have all of the code... I think... but when I run it, I still get some circles that should be green, but are red instead. Here is my code. Any help will be greatly appreciated.

import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Random;
import javax.swing.JFrame;
import java.awt.*;


public class IntersectingCircles extends JPanel
{
    private int[] xAxis = new int [20]; // array to hold x axis points
    private int[] yAxis = new int [20]; // array to hold y axis points
    private int[] radius = new int [20]; // array to hold radius length


    public static void main (String[] args)
    {
        JFrame frame = new JFrame("Random Circles");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new IntersectingCircles());

        frame.pack();
        frame.setVisible(true);
    }

    public IntersectingCircles()
    {
        setPreferredSize(new Dimension(1300, 800)); // set window size
        Random random = new Random();

        for (int i = 0; i < 20; i++)
        {
            xAxis[i] = random.nextInt(800) + 100;
            yAxis[i] = random.nextInt(500) + 100;
            radius[i] = random.nextInt(75) + 10;
        }
    }

    public void paintComponent(Graphics g)
    {   
        for (int i = 0; i < 20; i++)
        {   
            int color = 0;

            for (int h = 0; h < 20; h++)
            {               
                if(i < h)
                {
                    double x1 = 0, x2 = 0, y1 = 0, y2 = 0, d = 0;


                    x1 = (xAxis[i] + radius[i]);
                    y1 = (yAxis[i] + radius[i]);
                    x2 = (xAxis[h] + radius[h]);
                    y2 = (yAxis[h] + radius[h]);

                    d = (Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1)*(y2 - y1))));

                    if (d > radius[i] + radius[h] || d < (Math.abs(radius[i] - radius[h])))
                    {
                        color = 0;
                    }

                    else
                    {
                        color = 1;
                        break;
                    }
                }
            }

            if (color == 0)
            {
                g.setColor(Color.RED);
                g.drawOval(xAxis[i], yAxis[i], radius[i] * 2, radius[i] * 2);
            }

            else
            {
                g.setColor(Color.GREEN);
                g.drawOval(xAxis[i], yAxis[i], radius[i] * 2, radius[i] * 2);
            }
        }       
    }
}

Upvotes: 0

Views: 907

Answers (1)

Hugo Sousa
Hugo Sousa

Reputation: 1936

In the inside for loop, you are only comparing circles of i index with circles with h index, but only those with i < h, because of the condition:

for (int h = 0; h < 20; h++)
{               
    if(i < h)
    {
        ...

So, instead you should compare every i circle with every h circle, except if they are the same. You want instead:

for (int h = 0; h < 20; h++)
{               
    if(i != h) //note the change here
    {
        ...

Upvotes: 1

Related Questions