1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
Rectangle r1 = new Rectangle(90, 50, 20, 20);
Rectangle r2 = new Rectangle(80, 80, 20, 20);
Rectangle tmp;
Rectangle r3 = new Rectangle(0, 0, 0, 0); // Rectangle entre r1 et r2
if (r1.x > r2.x) {
tmp = r2;
r2 = r1;
r1 = tmp;
} // Ca me permet d'être sur que r1.x est à gauche de r2.x
r3.width = Math.max(r2.x + r2.width - r1.x, r3.width);
r3.width = Math.max(r1.width, r3.width);
r3.width = Math.max(r2.width, r3.width); // Je récupère la plus grande largeur en fonction de tous les cas possibles.
if (r1.x + r1.width < r2.x) { // Si les deux rectangles se chevauchent sur x, si oui je retire la longueur des deux rectangles et je cale r3 sur la fin de r1
r3.width -= r1.width + r2.width;
r3.x = r1.x + r1.width;
} else { // Sinon je cale r3 sur r1
r3.x = r1.x;
}
if (r1.y > r2.y) {
tmp = r2;
r2 = r1;
r1 = tmp;
}
r3.height = Math.max(r2.y + r2.height - r1.y, r3.height);
r3.height = Math.max(r1.height, r3.height);
r3.height = Math.max(r2.height, r3.height);
// Je récupère la plus grande hauteur possible
if (((r1.x <= r2.x) && (r1.x + r1.width >= r2.x)) || ((r1.x >= r2.x) && (r2.x + r2.width >= r1.x))) { // Je regarde si r1 et r2 se chevauchent sur x, si oui je retire la hauteur des deux rectangles et je cale r3 sur la fin de r1
r3.height -= r1.height + r2.height;
r3.y = r1.y + r1.height;
} else { // Sinon je cale r3 sur r1
r3.y = r1.y;
} |
Partager