0

As seen here, i am trying to link the circles and draw a square somewhere at midpoints on each of the links. But the problem is that some of the squares overlap.

Is there any way that i can detect those overlaps and re-position or move those squares so that they remain on their respective link and do not overlap.

Here is the code

    int x1 = 100, x2 = 500, aIncr = 50, bIncr = 50;
    int y1, y2;
    y1 = y2 = 100;

    g.setColor(Color.GRAY);

    for (int i = 0; i < column1.length; i++) {
        g.fillOval(x1, y1, column1[i], column1[i]);
        y1 += aIncr;

    }

    for (int i = 0; i < column2.length; i++) {
        g.fillOval(x2, y2, column2[i], column2[i]);
        y2 += bIncr;
    }

    y1 = y2 = 100;
    int squareWidth = 20;

    int x, y;
    for (int i = 0; i < column1.length; i++) {
        y2 = 100;
        for (int j = 0; j < column2.length; j++) {
            g.drawLine(x1 + column1[i] / 2, y1 + column1[i] / 2, x2 + column2[j] / 2, y2 + column2[j] / 2);

            x = ((x1 + column1[i] / 2) + (x2 + column2[j] / 2)) / 2 - squareWidth / 2;
            y = ((y1 + column1[i] / 2) + (y2 + column2[j] / 2)) / 2 - squareWidth / 2;

            g.fillRect(x, y, squareWidth, squareWidth);
            y2 += bIncr;
        }
        y1 += aIncr;
    }
smprj
  • 63
  • 1
  • 3
  • 9
  • 3
    A better solution would be to take advantage of the Shape's API (Ellipse and Rectangle in particular), these have helpful methods for detecting intersections (and other things) – MadProgrammer Jul 04 '14 at 21:49
  • 1) Look at [Shape based collision detection](http://stackoverflow.com/a/14575043/418556). It uses Java 2D `Area` objects to test for intersection. 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Jul 04 '14 at 23:29

0 Answers0