JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
Rectangle.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.nativewindow.util;
30
31import java.util.List;
32
33public class Rectangle implements Cloneable, RectangleImmutable {
34 int x;
35 int y;
36 int width;
37 int height;
38
39 public Rectangle() {
40 this(0, 0, 0, 0);
41 }
42
43 public Rectangle(final int x, final int y, final int width, final int height) {
44 this.x=x;
45 this.y=y;
46 this.width=width;
47 this.height=height;
48 }
49 public Rectangle(final RectangleImmutable s) {
50 set(s);
51 }
52
53 @Override
54 public Object cloneMutable() {
55 return clone();
56 }
57
58 @Override
59 protected Object clone() {
60 try {
61 return super.clone();
62 } catch (final CloneNotSupportedException ex) {
63 throw new InternalError();
64 }
65 }
66
67 @Override
68 public final int getX() { return x; }
69 @Override
70 public final int getY() { return y; }
71 @Override
72 public final int getWidth() { return width; }
73 @Override
74 public final int getHeight() { return height; }
75
76 public final Rectangle set(final int x, final int y, final int width, final int height) {
77 this.x = x;
78 this.y = y;
79 this.width = width;
80 this.height = height;
81 return this;
82 }
83 public final Rectangle set(final RectangleImmutable s) {
84 this.x = s.getX();
85 this.y = s.getY();
86 this.width = s.getWidth();
87 this.height = s.getHeight();
88 return this;
89 }
90 public final Rectangle setX(final int x) { this.x = x; return this; }
91 public final Rectangle setY(final int y) { this.y = y; return this; }
92 public final Rectangle setWidth(final int width) { this.width = width; return this; }
93 public final Rectangle setHeight(final int height) { this.height = height; return this; }
94
95 @Override
96 public final Rectangle union(final RectangleImmutable r) {
97 return union(r.getX(), r.getY(), r.getX() + r.getWidth() - 1, r.getY() + r.getHeight() - 1);
98 }
99 @Override
100 public final Rectangle union(final int rx1, final int ry1, final int rx2, final int ry2) {
101 final int x1 = Math.min(x, rx1);
102 final int y1 = Math.min(y, ry1);
103 final int x2 = Math.max(x + width - 1, rx2);
104 final int y2 = Math.max(y + height - 1, ry2);
105 return new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
106 }
107 @Override
108 public final Rectangle union(final List<RectangleImmutable> rectangles) {
109 int x1=Integer.MAX_VALUE, y1=Integer.MAX_VALUE;
110 int x2=Integer.MIN_VALUE, y2=Integer.MIN_VALUE;
111 for(int i=rectangles.size()-1; i>=0; i--) {
112 final RectangleImmutable vp = rectangles.get(i);
113 x1 = Math.min(x1, vp.getX());
114 x2 = Math.max(x2, vp.getX() + vp.getWidth()); // exclusive
115 y1 = Math.min(y1, vp.getY());
116 y2 = Math.max(y2, vp.getY() + vp.getHeight()); // exclusive
117 }
118 return new Rectangle(x1, y1, x2 - x1, y2 - y1);
119 }
120
121 @Override
123 return intersection(r.getX(), r.getY(), r.getX() + r.getWidth() - 1, r.getY() + r.getHeight() - 1);
124 }
125 @Override
126 public final Rectangle intersection(final int rx1, final int ry1, final int rx2, final int ry2) {
127 final int x1 = Math.max(x, rx1);
128 final int y1 = Math.max(y, ry1);
129 final int x2 = Math.min(x + width - 1, rx2);
130 final int y2 = Math.min(y + height - 1, ry2);
131 final int ix, iy, iwidth, iheight;
132 if( x2 < x1 ) {
133 ix = 0;
134 iwidth = 0;
135 } else {
136 ix = x1;
137 iwidth = x2 - x1 + 1;
138 }
139 if( y2 < y1 ) {
140 iy = 0;
141 iheight = 0;
142 } else {
143 iy = y1;
144 iheight = y2 - y1 + 1;
145 }
146 return new Rectangle (ix, iy, iwidth, iheight);
147 }
148 @Override
149 public final float coverage(final RectangleImmutable r) {
150 final RectangleImmutable isect = intersection(r);
151 final float sqI = isect.getWidth()*isect.getHeight();
152 final float sqT = width*height;
153 return sqI / sqT;
154 }
155
156 @Override
157 public final boolean contains(final RectangleImmutable r) {
158 final int x2 = x + width - 1;
159 final int y2 = y + height - 1;
160 final int rx1 = r.getX();
161 final int ry1 = r.getY();
162 final int rx2 = rx1 + r.getWidth() - 1;
163 final int ry2 = ry1 + r.getHeight() - 1;
164 if( rx1 < x || ry1 < y ||
165 rx2 > x2 || ry2 > y2 ) {
166 return false;
167 }
168 return true;
169 }
170
171 /**
172 * Scale this instance's components,
173 * i.e. multiply them by the given scale factors.
174 * @param sx scale factor for x
175 * @param sy scale factor for y
176 * @return this instance for scaling
177 */
178 public final Rectangle scale(final int sx, final int sy) {
179 x *= sx ;
180 y *= sy ;
181 width *= sx ;
182 height *= sy ;
183 return this;
184 }
185
186 /**
187 * Scale this instance's components,
188 * i.e. multiply them by the given scale factors (rounded).
189 * @param sx scale factor for x
190 * @param sy scale factor for y
191 * @return this instance for scaling
192 */
193 public final Rectangle scale(final float sx, final float sy) {
194 x = (int)( x * sx + 0.5f );
195 y = (int)( y * sy + 0.5f );
196 width = (int)( width * sx + 0.5f );
197 height = (int)( height * sy + 0.5f );
198 return this;
199 }
200
201 /**
202 * Inverse scale this instance's components,
203 * i.e. divide them by the given scale factors.
204 * @param sx inverse scale factor for x
205 * @param sy inverse scale factor for y
206 * @return this instance for scaling
207 */
208 public final Rectangle scaleInv(final int sx, final int sy) {
209 x /= sx ;
210 y /= sy ;
211 width /= sx ;
212 height /= sy ;
213 return this;
214 }
215
216 /**
217 * Inverse scale this instance's components,
218 * i.e. divide them by the given scale factors (rounded).
219 * @param sx inverse scale factor for x
220 * @param sy inverse scale factor for y
221 * @return this instance for scaling
222 */
223 public final Rectangle scaleInv(final float sx, final float sy) {
224 x = (int)( x / sx + 0.5f );
225 y = (int)( y / sy + 0.5f );
226 width = (int)( width / sx + 0.5f );
227 height = (int)( height / sy + 0.5f );
228 return this;
229 }
230
231 @Override
232 public int compareTo(final RectangleImmutable d) {
233 {
234 final int sq = width*height;
235 final int xsq = d.getWidth()*d.getHeight();
236
237 if(sq > xsq) {
238 return 1;
239 } else if(sq < xsq) {
240 return -1;
241 }
242 }
243 {
244 // FIXME: Invalid, position needs to be compared differently
245 final int sq = x*y;
246 final int xsq = d.getX()*d.getY();
247
248 if(sq > xsq) {
249 return 1;
250 } else if(sq < xsq) {
251 return -1;
252 }
253 }
254 return 0;
255 }
256
257 @Override
258 public boolean equals(final Object obj) {
259 if(this == obj) { return true; }
260 if (obj instanceof Rectangle) {
261 final Rectangle rect = (Rectangle)obj;
262 return (y == rect.y) && (x == rect.x) &&
263 (height == rect.height) && (width == rect.width);
264 }
265 return false;
266 }
267
268 @Override
269 public int hashCode() {
270 final int sum1 = x + height;
271 final int sum2 = width + y;
272 final int val1 = sum1 * (sum1 + 1)/2 + x;
273 final int val2 = sum2 * (sum2 + 1)/2 + y;
274 final int sum3 = val1 + val2;
275 return sum3 * (sum3 + 1)/2 + val2;
276 }
277
278 @Override
279 public String toString() {
280 return "[ "+x+" / "+y+" "+width+" x "+height+" ]";
281 }
282}
283
Rectangle(final int x, final int y, final int width, final int height)
Definition: Rectangle.java:43
final Rectangle scale(final int sx, final int sy)
Scale this instance's components, i.e.
Definition: Rectangle.java:178
final Rectangle scaleInv(final float sx, final float sy)
Inverse scale this instance's components, i.e.
Definition: Rectangle.java:223
final int getX()
x-position, left of rectangle.
Definition: Rectangle.java:68
final float coverage(final RectangleImmutable r)
Returns the coverage of given rectangle w/ this this one, i.e.
Definition: Rectangle.java:149
final Rectangle scaleInv(final int sx, final int sy)
Inverse scale this instance's components, i.e.
Definition: Rectangle.java:208
final Rectangle setWidth(final int width)
Definition: Rectangle.java:92
final int getY()
y-position, top of rectangle.
Definition: Rectangle.java:70
final Rectangle union(final int rx1, final int ry1, final int rx2, final int ry2)
Returns a new Rectangle instance containing the union of this rectangle and the given coordinates.
Definition: Rectangle.java:100
Rectangle(final RectangleImmutable s)
Definition: Rectangle.java:49
final Rectangle intersection(final RectangleImmutable r)
Returns a new Rectangle instance containing the intersection of this rectangle and the given rectangl...
Definition: Rectangle.java:122
final Rectangle intersection(final int rx1, final int ry1, final int rx2, final int ry2)
Returns a new Rectangle instance containing the intersection of this rectangle and the given coordina...
Definition: Rectangle.java:126
final Rectangle scale(final float sx, final float sy)
Scale this instance's components, i.e.
Definition: Rectangle.java:193
final Rectangle setY(final int y)
Definition: Rectangle.java:91
int compareTo(final RectangleImmutable d)
Definition: Rectangle.java:232
final Rectangle union(final RectangleImmutable r)
Returns a new Rectangle instance containing the union of this rectangle and the given rectangle.
Definition: Rectangle.java:96
final boolean contains(final RectangleImmutable r)
Definition: Rectangle.java:157
final Rectangle setHeight(final int height)
Definition: Rectangle.java:93
boolean equals(final Object obj)
Checks whether two rect objects are equal.
Definition: Rectangle.java:258
final Rectangle setX(final int x)
Definition: Rectangle.java:90
final Rectangle union(final List< RectangleImmutable > rectangles)
Calculates the union of the given rectangles, stores it in this instance and returns this instance.
Definition: Rectangle.java:108
Immutable Rectangle interface, with its position on the top-left.
int getX()
x-position, left of rectangle.
int getY()
y-position, top of rectangle.