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