1015 |
child.setParent(uiInputMap); |
child.setParent(uiInputMap); |
1016 |
} |
} |
1017 |
} |
} |
1018 |
|
|
1019 |
|
/** |
1020 |
|
* Subtracts a rectangle from another and return the area as an array |
1021 |
|
* of rectangles. |
1022 |
|
* Returns the areas of rectA which are not covered by rectB. |
1023 |
|
* If the rectangles do not overlap, or if either parameter is |
1024 |
|
* <code>null</code>, a zero-size array is returned. |
1025 |
|
* @param rectA The first rectangle |
1026 |
|
* @param rectB The rectangle to subtract from the first |
1027 |
|
* @return An array of rectangles representing the area in rectA |
1028 |
|
* not overlapped by rectB |
1029 |
|
*/ |
1030 |
|
public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB) |
1031 |
|
{ |
1032 |
|
if (rectA == null || rectB == null) |
1033 |
|
return new Rectangle[0]; |
1034 |
|
|
1035 |
|
Rectangle[] r = new Rectangle[4]; |
1036 |
|
int x1 = rectA.x; |
1037 |
|
int y1 = rectA.y; |
1038 |
|
int w1 = rectA.width; |
1039 |
|
int h1 = rectA.height; |
1040 |
|
int x2 = rectB.x; |
1041 |
|
int y2 = rectB.y; |
1042 |
|
int w2 = rectB.width; |
1043 |
|
int h2 = rectB.height; |
1044 |
|
|
1045 |
|
// (outer box = rectA) |
1046 |
|
// ------------- |
1047 |
|
// |_____0_____| |
1048 |
|
// | |rectB| | |
1049 |
|
// |_1|_____|_2| |
1050 |
|
// | 3 | |
1051 |
|
// ------------- |
1052 |
|
int H0 = (y2 > y1) ? y2 - y1 : 0; // height of box 0 |
1053 |
|
int H3 = (y2 + h2 < y1 + h1) ? y1 + h1 - y2 - h2 : 0; // height box 3 |
1054 |
|
int W1 = (x2 > x1) ? x2 - x1 : 0; // width box 1 |
1055 |
|
int W2 = (x1 + w1 > x2 + w2) ? x1 + w1 - x2 - w2 : 0; // w. box 2 |
1056 |
|
int H12 = (H0 + H3 < h1) ? h1 - H0 - H3 : 0; // height box 1 & 2 |
1057 |
|
|
1058 |
|
if (H0 > 0) |
1059 |
|
r[0] = new Rectangle(x1, y1, w1, H0); |
1060 |
|
else |
1061 |
|
r[0] = null; |
1062 |
|
|
1063 |
|
if (W1 > 0 && H12 > 0) |
1064 |
|
r[1] = new Rectangle(x1, y1 + H0, W1, H12); |
1065 |
|
else |
1066 |
|
r[1] = null; |
1067 |
|
|
1068 |
|
if (W2 > 0 && H12 > 0) |
1069 |
|
r[2] = new Rectangle(x2 + w2, y1 + H0, W2, H12); |
1070 |
|
else |
1071 |
|
r[2] = null; |
1072 |
|
|
1073 |
|
if (H3 > 0) |
1074 |
|
r[3] = new Rectangle(x1, y1 + H0 + H12, w1, H3); |
1075 |
|
else |
1076 |
|
r[3] = null; |
1077 |
|
|
1078 |
|
// sort out null objects |
1079 |
|
int n = 0; |
1080 |
|
for (int i = 0; i < 4; i++) |
1081 |
|
if (r[i] != null) |
1082 |
|
n++; |
1083 |
|
Rectangle[] out = new Rectangle[n]; |
1084 |
|
for (int i = 3; i >= 0; i--) |
1085 |
|
if (r[i] != null) |
1086 |
|
out[--n] = r[i]; |
1087 |
|
|
1088 |
|
return out; |
1089 |
|
} |
1090 |
|
|
1091 |
|
/** |
1092 |
|
* Calculates the intersection of two rectangles. |
1093 |
|
* |
1094 |
|
* @param x upper-left x coodinate of first rectangle |
1095 |
|
* @param x upper-left y coodinate of first rectangle |
1096 |
|
* @param w width of first rectangle |
1097 |
|
* @param h height of first rectangle |
1098 |
|
* @param rect a Rectangle object of the second rectangle |
1099 |
|
* @throws a NullPointerException if rect is null. |
1100 |
|
* |
1101 |
|
* @return a rectangle corresponding to the intersection of the |
1102 |
|
* two rectangles. A zero rectangle is returned if the rectangles |
1103 |
|
* do not overlap. |
1104 |
|
*/ |
1105 |
|
public static Rectangle computeIntersection(int x, int y, int w, int h, |
1106 |
|
Rectangle rect) |
1107 |
|
{ |
1108 |
|
int x2 = (int) rect.getX(); |
1109 |
|
int y2 = (int) rect.getY(); |
1110 |
|
int w2 = (int) rect.getWidth(); |
1111 |
|
int h2 = (int) rect.getHeight(); |
1112 |
|
|
1113 |
|
int dx = (x > x2) ? x : x2; |
1114 |
|
int dy = (y > y2) ? y : y2; |
1115 |
|
int dw = (x + w < x2 + w2) ? (x + w - dx) : (x2 + w2 - dx); |
1116 |
|
int dh = (y + h < y2 + h2) ? (y + h - dy) : (y2 + h2 - dy); |
1117 |
|
|
1118 |
|
if (dw >= 0 && dh >= 0) |
1119 |
|
return new Rectangle(dx, dy, dw, dh); |
1120 |
|
|
1121 |
|
return new Rectangle(0, 0, 0, 0); |
1122 |
|
} |
1123 |
|
|
1124 |
|
/** |
1125 |
|
* Calculates the union of two rectangles. |
1126 |
|
* |
1127 |
|
* @param x upper-left x coodinate of first rectangle |
1128 |
|
* @param x upper-left y coodinate of first rectangle |
1129 |
|
* @param w width of first rectangle |
1130 |
|
* @param h height of first rectangle |
1131 |
|
* @param rect a Rectangle object of the second rectangle |
1132 |
|
* @throws a NullPointerException if rect is null. |
1133 |
|
* |
1134 |
|
* @return a rectangle corresponding to the union of the |
1135 |
|
* two rectangles. A rectangle encompassing both is returned if the |
1136 |
|
* rectangles do not overlap. |
1137 |
|
*/ |
1138 |
|
public static Rectangle computeUnion(int x, int y, int w, int h, |
1139 |
|
Rectangle rect) |
1140 |
|
{ |
1141 |
|
int x2 = (int) rect.getX(); |
1142 |
|
int y2 = (int) rect.getY(); |
1143 |
|
int w2 = (int) rect.getWidth(); |
1144 |
|
int h2 = (int) rect.getHeight(); |
1145 |
|
|
1146 |
|
int dx = (x < x2) ? x : x2; |
1147 |
|
int dy = (y < y2) ? y : y2; |
1148 |
|
int dw = (x + w > x2 + w2) ? (x + w - dx) : (x2 + w2 - dx); |
1149 |
|
int dh = (y + h > y2 + h2) ? (y + h - dy) : (y2 + h2 - dy); |
1150 |
|
|
1151 |
|
if (dw >= 0 && dh >= 0) |
1152 |
|
return new Rectangle(dx, dy, dw, dh); |
1153 |
|
|
1154 |
|
return new Rectangle(0, 0, 0, 0); |
1155 |
|
} |
1156 |
|
|
1157 |
|
/** |
1158 |
|
* Tests if a rectangle contains another. |
1159 |
|
* @param a first rectangle |
1160 |
|
* @param b second rectangle |
1161 |
|
* @return true if a contains b, false otherwise |
1162 |
|
* @throws NullPointerException |
1163 |
|
*/ |
1164 |
|
public static boolean isRectangleContainingRectangle(Rectangle a, Rectangle b) |
1165 |
|
{ |
1166 |
|
// Note: zero-size rects inclusive, differs from Rectangle.contains() |
1167 |
|
return b.width >= 0 && b.height >= 0 && b.width >= 0 && b.height >= 0 |
1168 |
|
&& b.x >= a.x && b.x + b.width <= a.x + a.width && b.y >= a.y |
1169 |
|
&& b.y + b.height <= a.y + a.height; |
1170 |
|
} |
1171 |
} |
} |