Mercurial > projects > dwt-addons
comparison dwtx/draw2d/geometry/Point.d @ 98:95307ad235d9
Added Draw2d code, still work in progress
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 03 Aug 2008 00:52:14 +0200 |
parents | |
children | 1082a0fc2bb8 |
comparison
equal
deleted
inserted
replaced
96:b492ba44e44d | 98:95307ad235d9 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2008 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 dwtx.draw2d.geometry.Point; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import dwtx.draw2d.PositionConstants; | |
18 import dwtx.draw2d.geometry.Translatable; | |
19 import dwtx.draw2d.geometry.Dimension; | |
20 import dwtx.draw2d.geometry.Rectangle; | |
21 | |
22 static import dwt.graphics.Point; | |
23 | |
24 import tango.text.convert.Format; | |
25 | |
26 /** | |
27 * Represents a point (x, y) in 2-dimensional space. This class provides various methods | |
28 * for manipulating this Point or creating new derived geometrical Objects. | |
29 */ | |
30 public class Point | |
31 : Cloneable, /+java.io.Serializable,+/ Translatable | |
32 { | |
33 | |
34 static final long serialVersionUID = 1; | |
35 /**A singleton for use in short calculations*/ | |
36 public static const Point SINGLETON; | |
37 | |
38 static this(){ | |
39 SINGLETON = new Point(); | |
40 } | |
41 | |
42 /**x value*/ | |
43 public int x; | |
44 /**y value*/ | |
45 public int y; | |
46 | |
47 /** | |
48 * Constructs a Point at location (0,0). | |
49 * @since 2.0 | |
50 */ | |
51 public this() { } | |
52 | |
53 /** | |
54 * Constructs a Point at the same location as the given Point. | |
55 * @param copy Point from which the initial values are taken. | |
56 * @since 2.0 | |
57 */ | |
58 public this(Point copy) { | |
59 x = copy.x; | |
60 y = copy.y; | |
61 } | |
62 | |
63 /** | |
64 * Constructs a Point at the same location as the given DWT Point. | |
65 * @param copy Point from which the initial values are taken. | |
66 * @since 2.0 | |
67 */ | |
68 public this(dwt.graphics.Point.Point copy) { | |
69 x = copy.x; | |
70 y = copy.y; | |
71 } | |
72 | |
73 | |
74 /** | |
75 * Constructs a Point at the specified x and y locations. | |
76 * | |
77 * @param x x value | |
78 * @param y y value | |
79 * @since 2.0 | |
80 */ | |
81 public this(int x, int y) { | |
82 this.x = x; | |
83 this.y = y; | |
84 } | |
85 | |
86 /** | |
87 * Constructs a Point at the specified x and y locations. | |
88 * @param x x value | |
89 * @param y y value | |
90 * @since 2.0 | |
91 */ | |
92 public this(double x, double y) { | |
93 this.x = cast(int)x; | |
94 this.y = cast(int)y; | |
95 } | |
96 | |
97 /** | |
98 * Test for equality. | |
99 * @param o Object being tested for equality | |
100 * @return true if both x and y values are equal | |
101 * @since 2.0 | |
102 */ | |
103 public override int opEquals(Object o) { | |
104 if ( auto p = cast(Point)o ) { | |
105 return p.x is x && p.y is y; | |
106 } | |
107 return false; | |
108 } | |
109 | |
110 /** | |
111 * @return a copy of this Point | |
112 * @since 2.0 | |
113 */ | |
114 public Point getCopy() { | |
115 return new Point(x, y); | |
116 } | |
117 | |
118 /** | |
119 * Calculates the difference in between this Point and the one specified. | |
120 * @param pt The Point being subtracted from this Point | |
121 * @return A new Dimension representing the difference | |
122 * @since 2.0 | |
123 */ | |
124 public Dimension getDifference(Point pt) { | |
125 return new Dimension(this.x - pt.x, this.y - pt.y); | |
126 } | |
127 | |
128 /** | |
129 * Calculates the distance from this Point to the one specified. | |
130 * @param pt The Point being compared to this | |
131 * @return The distance | |
132 * @since 2.0 | |
133 */ | |
134 public double getDistance(Point pt) { | |
135 return Math.sqrt(getPreciseDistance2(pt)); | |
136 } | |
137 | |
138 /** | |
139 * Calculates the distance squared between this Point and the one specified. If | |
140 * the distance squared is larger than the maximum integer value, then | |
141 * <code>Integer.MAX_VALUE</code> will be returned. | |
142 * @param pt The reference Point | |
143 * @return distance<sup>2</sup> | |
144 * @since 2.0 | |
145 */ | |
146 public int getDistance2(Point pt) { | |
147 long i = pt.x - x; | |
148 long j = pt.y - y; | |
149 long result = i * i + j * j; | |
150 if (result > Integer.MAX_VALUE) | |
151 return Integer.MAX_VALUE; | |
152 return cast(int)result; | |
153 } | |
154 | |
155 private double getPreciseDistance2(Point pt) { | |
156 double i = pt.preciseX() - preciseX(); | |
157 double j = pt.preciseY() - preciseY(); | |
158 return i * i + j * j; | |
159 } | |
160 | |
161 /** | |
162 * Calculates the orthogonal distance to the specified point. The orthogonal distance is | |
163 * the sum of the horizontal and vertical differences. | |
164 * @param pt The reference Point | |
165 * @return the orthoganal distance | |
166 */ | |
167 public int getDistanceOrthogonal(Point pt) { | |
168 return Math.abs(y - pt.y) + Math.abs(x - pt.x); | |
169 } | |
170 | |
171 /** | |
172 * Creates a Point with negated x and y values. | |
173 * @return A new Point | |
174 * @since 2.0 | |
175 */ | |
176 public Point getNegated() { | |
177 return getCopy().negate(); | |
178 } | |
179 | |
180 /** | |
181 * Calculates the relative position of the specified Point to this Point. | |
182 * @param p The reference Point | |
183 * @return NORTH, SOUTH, EAST, or WEST, as defined in {@link PositionConstants} | |
184 */ | |
185 public int getPosition(Point p) { | |
186 int dx = p.x - x; | |
187 int dy = p.y - y; | |
188 if (Math.abs(dx) > Math.abs(dy)) { | |
189 if (dx < 0) | |
190 return PositionConstants.WEST; | |
191 return PositionConstants.EAST; | |
192 } | |
193 if (dy < 0) | |
194 return PositionConstants.NORTH; | |
195 return PositionConstants.SOUTH; | |
196 } | |
197 | |
198 /** | |
199 * Creates a new Point from this Point by scaling by the specified amount. | |
200 * @param amount scale factor | |
201 * @return A new Point | |
202 * @since 2.0 | |
203 */ | |
204 public Point getScaled(double amount) { | |
205 return getCopy().scale(amount); | |
206 } | |
207 | |
208 /** | |
209 * Creates a new DWT {@link dwt.graphics.Point Point} from this Point. | |
210 * @return A new DWT Point | |
211 * @since 2.0 | |
212 */ | |
213 public dwt.graphics.Point.Point getSWTPoint() { | |
214 return new dwt.graphics.Point.Point(x, y); | |
215 } | |
216 | |
217 /** | |
218 * Creates a new Point which is translated by the values of the input Dimension. | |
219 * @param delta Dimension which provides the translation amounts. | |
220 * @return A new Point | |
221 * @since 2.0 | |
222 */ | |
223 public Point getTranslated(Dimension delta) { | |
224 return getCopy().translate(delta); | |
225 } | |
226 | |
227 /** | |
228 * Creates a new Point which is translated by the specified x and y values | |
229 * @param x horizontal component | |
230 * @param y vertical component | |
231 * @return A new Point | |
232 * @since 2.0 | |
233 */ | |
234 public Point getTranslated(int x, int y) { | |
235 return getCopy().translate(x, y); | |
236 } | |
237 | |
238 /** | |
239 * Creates a new Point which is translated by the values of the provided Point. | |
240 * @param pt Point which provides the translation amounts. | |
241 * @return A new Point | |
242 * @since 2.0 | |
243 */ | |
244 public Point getTranslated(Point pt) { | |
245 return getCopy().translate(pt); | |
246 } | |
247 | |
248 /** | |
249 * Creates a new Point with the transposed values of this Point. | |
250 * Can be useful in orientation change calculations. | |
251 * @return A new Point | |
252 * @since 2.0 | |
253 */ | |
254 public Point getTransposed() { | |
255 return getCopy().transpose(); | |
256 } | |
257 | |
258 /** | |
259 * @see java.lang.Object#toHash() | |
260 */ | |
261 public override hash_t toHash() { | |
262 return (x * y) ^ (x + y); | |
263 } | |
264 | |
265 /** | |
266 * Creates a new Point representing the MAX of two provided Points. | |
267 * @param p1 first point | |
268 * @param p2 second point | |
269 * @return A new Point representing the Max() | |
270 */ | |
271 public static Point max(Point p1, Point p2) { | |
272 return (new Rectangle(p1, p2)) | |
273 .getBottomRight() | |
274 .translate(-1, -1); | |
275 } | |
276 | |
277 /** | |
278 * Creates a new Point representing the MIN of two provided Points. | |
279 * @param p1 first point | |
280 * @param p2 second point | |
281 * @return A new Point representing the Min() | |
282 */ | |
283 public static Point min(Point p1, Point p2) { | |
284 return (new Rectangle(p1, p2)).getTopLeft(); | |
285 } | |
286 | |
287 /** | |
288 * Negates the x and y values of this Point. | |
289 * @return <code>this</code> for convenience | |
290 * @since 2.0 | |
291 */ | |
292 public Point negate() { | |
293 x = -x; | |
294 y = -y; | |
295 return this; | |
296 } | |
297 | |
298 /** @see Translatable#performScale(double) */ | |
299 public void performScale(double factor) { | |
300 scale(factor); | |
301 } | |
302 | |
303 /** @see Translatable#performTranslate(int, int) */ | |
304 public void performTranslate(int dx, int dy) { | |
305 translate(dx, dy); | |
306 } | |
307 | |
308 /** | |
309 * Scales this Point by the specified amount. | |
310 * @return <code>this</code> for convenience | |
311 * @param amount scale factor | |
312 * @since 2.0 | |
313 */ | |
314 public Point scale(double amount) { | |
315 x = cast(int)Math.floor(x * amount); | |
316 y = cast(int)Math.floor(y * amount); | |
317 return this; | |
318 } | |
319 | |
320 /** | |
321 * Scales this Point by the specified values. | |
322 * @param xAmount horizontal scale factor | |
323 * @param yAmount vertical scale factor | |
324 * @return <code>this</code> for convenience | |
325 * @since 2.0 | |
326 */ | |
327 public Point scale(double xAmount, double yAmount) { | |
328 x = cast(int)Math.floor(x * xAmount); | |
329 y = cast(int)Math.floor(y * yAmount); | |
330 return this; | |
331 } | |
332 | |
333 /** | |
334 * Sets the location of this Point to the provided x and y locations. | |
335 * @return <code>this</code> for convenience | |
336 * @param x the x location | |
337 * @param y the y location | |
338 * @since 2.0 | |
339 */ | |
340 public Point setLocation(int x, int y) { | |
341 this.x = x; | |
342 this.y = y; | |
343 return this; | |
344 } | |
345 | |
346 /** | |
347 * Sets the location of this Point to the specified Point. | |
348 * @return <code>this</code> for convenience | |
349 * @param pt the Location | |
350 * @since 2.0 | |
351 */ | |
352 public Point setLocation(Point pt) { | |
353 x = pt.x; | |
354 y = pt.y; | |
355 return this; | |
356 } | |
357 | |
358 /** | |
359 * @return String representation. | |
360 * @since 2.0 | |
361 */ | |
362 public override String toString() { | |
363 return Format("Point({}, {})", x, y );//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$ | |
364 } | |
365 | |
366 /** | |
367 * Shifts the location of this Point by the location of the | |
368 * input Point along each of the axes, and returns this for | |
369 * convenience. | |
370 * | |
371 * @param p Point to which the origin is being shifted. | |
372 * @return <code>this</code> for convenience | |
373 * @since 2.0 | |
374 */ | |
375 public Point translate(Point p) { | |
376 return translate(p.x, p.y); | |
377 } | |
378 | |
379 /** | |
380 * Shifts this Point by the values of the Dimension along | |
381 * each axis, and returns this for convenience. | |
382 * | |
383 * @param d Dimension by which the origin is being shifted. | |
384 * @return <code>this</code> for convenience | |
385 * @since 2.0 | |
386 */ | |
387 public Point translate(Dimension d) { | |
388 return translate(d.width, d.height); | |
389 } | |
390 | |
391 /** | |
392 * Shifts this Point by the values supplied along each axes, and | |
393 * returns this for convenience. | |
394 * | |
395 * @param dx Amount by which point is shifted along X axis. | |
396 * @param dy Amount by which point is shifted along Y axis. | |
397 * @return <code>this</code> for convenience | |
398 * @since 2.0 | |
399 */ | |
400 public Point translate(int dx, int dy) { | |
401 x += dx; | |
402 y += dy; | |
403 return this; | |
404 } | |
405 | |
406 /** | |
407 * Transposes this object. X and Y values are exchanged. | |
408 * @return <code>this</code> for convenience | |
409 * @since 2.0 | |
410 */ | |
411 public Point transpose() { | |
412 int temp = x; | |
413 x = y; | |
414 y = temp; | |
415 return this; | |
416 } | |
417 | |
418 /** | |
419 * Returns <code>double</code> x coordinate | |
420 * | |
421 * @return <code>double</code> x coordinate | |
422 * @since 3.4 | |
423 */ | |
424 public double preciseX() { | |
425 return x; | |
426 } | |
427 | |
428 /** | |
429 * Returns <code>double</code> y coordinate | |
430 * | |
431 * @return <code>double</code> y coordinate | |
432 * @since 3.4 | |
433 */ | |
434 public double preciseY() { | |
435 return y; | |
436 } | |
437 | |
438 } |