Mercurial > projects > dwt-win
annotate dwt/graphics/Rectangle.d @ 174:f906dbcacee5
Minor changes to be more compatible to dwt-linux, formatting in toString, Display.DEBUG=true
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 02 Mar 2008 23:39:01 +0100 |
parents | bf9fe45b4422 |
children | ab60f3309436 |
rev | line source |
---|---|
174
f906dbcacee5
Minor changes to be more compatible to dwt-linux, formatting in toString, Display.DEBUG=true
Frank Benoit <benoit@tionex.de>
parents:
4
diff
changeset
|
1 /******************************************************************************* |
4 | 2 * Copyright (c) 2000, 2005 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwt.graphics.Rectangle; | |
14 | |
15 public import dwt.graphics.Point; | |
16 public import dwt.internal.SerializableCompatibility; | |
17 | |
18 import dwt.DWT; | |
19 import dwt.DWTError; | |
20 import tango.text.convert.Format; | |
21 | |
22 /** | |
23 * Instances of this class represent rectangular areas in an | |
24 * (x, y) coordinate system. The top left corner of the rectangle | |
25 * is specified by its x and y values, and the extent of the | |
26 * rectangle is specified by its width and height. | |
27 * <p> | |
28 * The coordinate space for rectangles and points is considered | |
29 * to have increasing values downward and to the right from its | |
30 * origin making this the normal, computer graphics oriented notion | |
31 * of (x, y) coordinates rather than the strict mathematical one. | |
32 * </p> | |
33 * <p> | |
34 * The hashCode() method in this class uses the values of the public | |
35 * fields to compute the hash value. When storing instances of the | |
36 * class in hashed collections, do not modify these fields after the | |
37 * object has been inserted. | |
38 * </p> | |
39 * <p> | |
40 * Application code does <em>not</em> need to explicitly release the | |
41 * resources managed by each instance when those instances are no longer | |
42 * required, and thus no <code>dispose()</code> method is provided. | |
43 * </p> | |
44 * | |
45 * @see Point | |
46 */ | |
47 | |
48 public final class Rectangle : SerializableCompatibility { | |
49 | |
50 /** | |
51 * the x coordinate of the rectangle | |
52 */ | |
53 public int x; | |
54 | |
55 /** | |
56 * the y coordinate of the rectangle | |
57 */ | |
58 public int y; | |
59 | |
60 /** | |
61 * the width of the rectangle | |
62 */ | |
63 public int width; | |
64 | |
65 /** | |
66 * the height of the rectangle | |
67 */ | |
68 public int height; | |
69 | |
70 //static final long serialVersionUID = 3256439218279428914L; | |
71 | |
72 /** | |
73 * Construct a new instance of this class given the | |
74 * x, y, width and height values. | |
75 * | |
76 * @param x the x coordinate of the origin of the rectangle | |
77 * @param y the y coordinate of the origin of the rectangle | |
78 * @param width the width of the rectangle | |
79 * @param height the height of the rectangle | |
80 */ | |
81 public this (int x, int y, int width, int height) { | |
82 this.x = x; | |
83 this.y = y; | |
84 this.width = width; | |
85 this.height = height; | |
86 } | |
87 | |
88 /** | |
89 * Destructively replaces the x, y, width and height values | |
90 * in the receiver with ones which represent the union of the | |
91 * rectangles specified by the receiver and the given rectangle. | |
92 * <p> | |
93 * The union of two rectangles is the smallest single rectangle | |
94 * that completely covers both of the areas covered by the two | |
95 * given rectangles. | |
96 * </p> | |
97 * | |
98 * @param rect the rectangle to merge with the receiver | |
99 * | |
100 * @exception IllegalArgumentException <ul> | |
101 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> | |
102 * </ul> | |
103 */ | |
104 public void add (Rectangle rect) { | |
105 if (rect is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
106 int left = x < rect.x ? x : rect.x; | |
107 int top = y < rect.y ? y : rect.y; | |
108 int lhs = x + width; | |
109 int rhs = rect.x + rect.width; | |
110 int right = lhs > rhs ? lhs : rhs; | |
111 lhs = y + height; | |
112 rhs = rect.y + rect.height; | |
113 int bottom = lhs > rhs ? lhs : rhs; | |
114 x = left; y = top; width = right - left; height = bottom - top; | |
115 } | |
116 | |
117 /** | |
118 * Returns <code>true</code> if the point specified by the | |
119 * arguments is inside the area specified by the receiver, | |
120 * and <code>false</code> otherwise. | |
121 * | |
122 * @param x the x coordinate of the point to test for containment | |
123 * @param y the y coordinate of the point to test for containment | |
124 * @return <code>true</code> if the rectangle contains the point and <code>false</code> otherwise | |
125 */ | |
126 public bool contains (int x, int y) { | |
127 return (x >= this.x) && (y >= this.y) && ((x - this.x) < width) && ((y - this.y) < height); | |
128 } | |
129 | |
130 /** | |
131 * Returns <code>true</code> if the given point is inside the | |
132 * area specified by the receiver, and <code>false</code> | |
133 * otherwise. | |
134 * | |
135 * @param pt the point to test for containment | |
136 * @return <code>true</code> if the rectangle contains the point and <code>false</code> otherwise | |
137 * | |
138 * @exception IllegalArgumentException <ul> | |
139 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> | |
140 * </ul> | |
141 */ | |
142 public bool contains (Point pt) { | |
143 if (pt is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
144 return contains(pt.x, pt.y); | |
145 } | |
146 | |
147 /** | |
148 * Compares the argument to the receiver, and returns true | |
149 * if they represent the <em>same</em> object using a class | |
150 * specific comparison. | |
151 * | |
152 * @param object the object to compare with this object | |
153 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise | |
154 * | |
155 * @see #hashCode() | |
156 */ | |
157 public override int opEquals (Object object) { | |
158 if (object is this) return true; | |
159 if( auto r = cast(Rectangle) object ){ | |
160 return (r.x is this.x) && (r.y is this.y) && (r.width is this.width) && (r.height is this.height); | |
161 } | |
162 return false; | |
163 } | |
164 | |
165 /** | |
166 * Returns an integer hash code for the receiver. Any two | |
167 * objects that return <code>true</code> when passed to | |
168 * <code>equals</code> must return the same value for this | |
169 * method. | |
170 * | |
171 * @return the receiver's hash | |
172 * | |
173 * @see #equals(Object) | |
174 */ | |
175 public override hash_t toHash () { | |
176 return x ^ y ^ width ^ height; | |
177 } | |
178 | |
179 /** | |
180 * Destructively replaces the x, y, width and height values | |
181 * in the receiver with ones which represent the intersection of the | |
182 * rectangles specified by the receiver and the given rectangle. | |
183 * | |
184 * @param rect the rectangle to intersect with the receiver | |
185 * | |
186 * @exception IllegalArgumentException <ul> | |
187 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> | |
188 * </ul> | |
189 * | |
190 * since 3.0 | |
191 */ | |
192 public void intersect (Rectangle rect) { | |
193 if (rect is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
194 if (this is rect) return; | |
195 int left = x > rect.x ? x : rect.x; | |
196 int top = y > rect.y ? y : rect.y; | |
197 int lhs = x + width; | |
198 int rhs = rect.x + rect.width; | |
199 int right = lhs < rhs ? lhs : rhs; | |
200 lhs = y + height; | |
201 rhs = rect.y + rect.height; | |
202 int bottom = lhs < rhs ? lhs : rhs; | |
203 x = right < left ? 0 : left; | |
204 y = bottom < top ? 0 : top; | |
205 width = right < left ? 0 : right - left; | |
206 height = bottom < top ? 0 : bottom - top; | |
207 } | |
208 | |
209 /** | |
210 * Returns a new rectangle which represents the intersection | |
211 * of the receiver and the given rectangle. | |
212 * <p> | |
213 * The intersection of two rectangles is the rectangle that | |
214 * covers the area which is contained within both rectangles. | |
215 * </p> | |
216 * | |
217 * @param rect the rectangle to intersect with the receiver | |
218 * @return the intersection of the receiver and the argument | |
219 * | |
220 * @exception IllegalArgumentException <ul> | |
221 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> | |
222 * </ul> | |
223 */ | |
224 public Rectangle intersection (Rectangle rect) { | |
225 if (rect is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
226 if (this is rect) return new Rectangle (x, y, width, height); | |
227 int left = x > rect.x ? x : rect.x; | |
228 int top = y > rect.y ? y : rect.y; | |
229 int lhs = x + width; | |
230 int rhs = rect.x + rect.width; | |
231 int right = lhs < rhs ? lhs : rhs; | |
232 lhs = y + height; | |
233 rhs = rect.y + rect.height; | |
234 int bottom = lhs < rhs ? lhs : rhs; | |
235 return new Rectangle ( | |
236 right < left ? 0 : left, | |
237 bottom < top ? 0 : top, | |
238 right < left ? 0 : right - left, | |
239 bottom < top ? 0 : bottom - top); | |
240 } | |
241 | |
242 /** | |
243 * Returns <code>true</code> if the rectangle described by the | |
244 * arguments intersects with the receiver and <code>false</code> | |
245 * otherwise. | |
246 * <p> | |
247 * Two rectangles intersect if the area of the rectangle | |
248 * representing their intersection is not empty. | |
249 * </p> | |
250 * | |
251 * @param x the x coordinate of the origin of the rectangle | |
252 * @param y the y coordinate of the origin of the rectangle | |
253 * @param width the width of the rectangle | |
254 * @param height the height of the rectangle | |
255 * @return <code>true</code> if the rectangle intersects with the receiver, and <code>false</code> otherwise | |
256 * | |
257 * @exception IllegalArgumentException <ul> | |
258 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> | |
259 * </ul> | |
260 * | |
261 * @see #intersection(Rectangle) | |
262 * @see #isEmpty() | |
263 * | |
264 * @since 3.0 | |
265 */ | |
266 public bool intersects (int x, int y, int width, int height) { | |
267 return (x < this.x + this.width) && (y < this.y + this.height) && | |
268 (x + width > this.x) && (y + height > this.y); | |
269 } | |
270 | |
271 /** | |
272 * Returns <code>true</code> if the given rectangle intersects | |
273 * with the receiver and <code>false</code> otherwise. | |
274 * <p> | |
275 * Two rectangles intersect if the area of the rectangle | |
276 * representing their intersection is not empty. | |
277 * </p> | |
278 * | |
279 * @param rect the rectangle to test for intersection | |
280 * @return <code>true</code> if the rectangle intersects with the receiver, and <code>false</code> otherwise | |
281 * | |
282 * @exception IllegalArgumentException <ul> | |
283 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> | |
284 * </ul> | |
285 * | |
286 * @see #intersection(Rectangle) | |
287 * @see #isEmpty() | |
288 */ | |
289 public bool intersects (Rectangle rect) { | |
290 if (rect is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
291 return rect is this || intersects (rect.x, rect.y, rect.width, rect.height); | |
292 } | |
293 | |
294 /** | |
295 * Returns <code>true</code> if the receiver does not cover any | |
296 * area in the (x, y) coordinate plane, and <code>false</code> if | |
297 * the receiver does cover some area in the plane. | |
298 * <p> | |
299 * A rectangle is considered to <em>cover area</em> in the | |
300 * (x, y) coordinate plane if both its width and height are | |
301 * non-zero. | |
302 * </p> | |
303 * | |
304 * @return <code>true</code> if the receiver is empty, and <code>false</code> otherwise | |
305 */ | |
306 public bool isEmpty () { | |
307 return (width <= 0) || (height <= 0); | |
308 } | |
309 | |
310 /** | |
311 * Returns a string containing a concise, human-readable | |
312 * description of the receiver. | |
313 * | |
314 * @return a string representation of the rectangle | |
315 */ | |
316 public override char[] toString () { | |
174
f906dbcacee5
Minor changes to be more compatible to dwt-linux, formatting in toString, Display.DEBUG=true
Frank Benoit <benoit@tionex.de>
parents:
4
diff
changeset
|
317 return Format( "Rectangle {{{}, {}, {}, {}}", x, y, width, height ); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ |
4 | 318 } |
319 | |
320 /** | |
321 * Returns a new rectangle which represents the union of | |
322 * the receiver and the given rectangle. | |
323 * <p> | |
324 * The union of two rectangles is the smallest single rectangle | |
325 * that completely covers both of the areas covered by the two | |
326 * given rectangles. | |
327 * </p> | |
328 * | |
329 * @param rect the rectangle to perform union with | |
330 * @return the union of the receiver and the argument | |
331 * | |
332 * @exception IllegalArgumentException <ul> | |
333 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> | |
334 * </ul> | |
335 * | |
336 * @see #add(Rectangle) | |
337 */ | |
338 public Rectangle makeUnion (Rectangle rect) { | |
339 if (rect is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
340 int left = x < rect.x ? x : rect.x; | |
341 int top = y < rect.y ? y : rect.y; | |
342 int lhs = x + width; | |
343 int rhs = rect.x + rect.width; | |
344 int right = lhs > rhs ? lhs : rhs; | |
345 lhs = y + height; | |
346 rhs = rect.y + rect.height; | |
347 int bottom = lhs > rhs ? lhs : rhs; | |
348 return new Rectangle (left, top, right - left, bottom - top); | |
349 } | |
350 | |
351 } |