0

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);
            }
        }       
    }
}
adidashawn6
  • 49
  • 1
  • 6
  • SO is not really a debugging service provider... Maybe you should point in more detail where the error could be, what other solutions did you try, etc... be more *specific* – nem035 Jan 23 '15 at 18:06
  • 3
    Two circles intersect if and only if the distance between their centers is <= the sum of their radii. – Louis Wasserman Jan 23 '15 at 18:09
  • 1
    what about a circle within a circle where the perimeters do not intersect? – adidashawn6 Jan 23 '15 at 18:18
  • @adidashawn6 For circle within circle then the distance of their centers must be in between the sum and difference of their radii – gtgaxiola Jan 23 '15 at 18:31
  • doesn't this handle both scenarios? if (d > radius[i] + radius[h] || d < (Math.abs(radius[i] - radius[h]))) – adidashawn6 Jan 23 '15 at 18:33
  • 1
    You're using `if(i < h)` in the inside loop, so only comparing `i` with higher `h` indexes. Could that be the problem? You may want `i != h` instead. – Hugo Sousa Jan 23 '15 at 18:36
  • Hugo, you are DA MAN!!! that was all it took. Now, every time i run it, the circles seem to be colored correctly. Thank you so much man! – adidashawn6 Jan 23 '15 at 18:41
  • @adidashawn6 Great :) I just added an answer, then. – Hugo Sousa Jan 23 '15 at 18:45
  • See also [Collision detection with complex shapes](http://stackoverflow.com/a/14575043/418556) for a working example. – Andrew Thompson Jan 23 '15 at 23:30

1 Answers1

1

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
    {
        ...
Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28
  • 2
    It is sufficient to compare circles i=0..19 with circles j=i+1..20, but this must not be combined with plotting them. If comparing i and j indicates an overlap, an array intersects[i] and intersects[j] should be set to true, and this should be used for determining the colour. It isn't good practice to combine property evaluation with rendering. – laune Jan 23 '15 at 18:49