正如here所示,我正在尝试链接圆圈,并在每个链接的中点处绘制一个正方形。但问题是一些正方形重叠。
是否有任何方法可以检测到这些重叠并重新定位或移动这些方块,以便它们保留在各自的链接上并且不会重叠。
这是代码
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;
}