comparison dwtx/draw2d/geometry/PointList.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
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
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 dwtx.draw2d.geometry.PointList;
14
15 import dwt.dwthelper.utils;
16 import tango.text.convert.Format;
17
18 import dwtx.draw2d.geometry.Translatable;
19 import dwtx.draw2d.geometry.Rectangle;
20 import dwtx.draw2d.geometry.Point;
21 import dwtx.draw2d.geometry.Geometry;
22
23 /**
24 * Represents a List of Points. This class is used for building an <code>int[]</code>.
25 * The array is internal, and is constructed and queried by the client using
26 * {@link Point Points}. DWT uses integer arrays when painting polylines and polygons.
27 */
28 public class PointList
29 : /+java.io.Serializable,+/ Translatable
30 {
31
32 private int[] points;
33 private Rectangle bounds;
34 private int size_ = 0;
35
36 static const long serialVersionUID = 1;
37
38 /**
39 * Constructs an empty PointList.
40 *
41 * @since 2.0
42 */
43 public this() { }
44
45 /**
46 * Constructs a PointList with the given points.
47 * @param points int array where two consecutive ints form the coordinates of a point
48 * @since 3.1
49 */
50 public this(int points[]) {
51 this.points = points;
52 this.size_ = points.length / 2;
53 }
54
55 /**
56 * Constructs a PointList with initial capacity <i>size</i>, but no points.
57 *
58 * @param size Number of points to hold.
59 * @since 2.0
60 */
61 public this(int size_) {
62 points = new int[size_ * 2];
63 }
64
65 /**
66 * Appends all of the given points to this PointList.
67 * @param source the source pointlist
68 */
69 public void addAll(PointList source) {
70 ensureCapacity(size_ + source.size_);
71 System.arraycopy(source.points, 0, points, size_ * 2, source.size_ * 2);
72 size_ += source.size_;
73 }
74
75 /**
76 * Adds Point <i>p</i> to this PointList.
77 * @param p the point to be added
78 * @see #removePoint(int)
79 * @since 2.0
80 */
81 public void addPoint(Point p) {
82 addPoint(p.x, p.y);
83 }
84
85 /**
86 * Adds the input point values to this PointList.
87 * @param x X value of a point to add
88 * @param y Y value of a point to add
89 * @since 2.0
90 */
91 public void addPoint(int x, int y) {
92 bounds = null;
93 int index = size_ * 2;
94 ensureCapacity(size_ + 1);
95 points[index] = x;
96 points[index + 1] = y;
97 size_++;
98 }
99
100 private void ensureCapacity(int newSize) {
101 newSize *= 2;
102 if (points.length < newSize) {
103 int old[] = points;
104 points = new int[Math.max(newSize, size_ * 4)];
105 System.arraycopy(old, 0, points, 0, size_ * 2);
106 }
107 }
108
109 /**
110 * Returns the smallest Rectangle which contains all Points.
111 * @return The smallest Rectangle which contains all Points.
112 * @since 2.0
113 */
114 public Rectangle getBounds() {
115 if (bounds !is null)
116 return bounds;
117 bounds = new Rectangle();
118 if (size_ > 0) {
119 bounds.setLocation(getPoint(0));
120 for (int i = 0; i < size_; i++)
121 bounds.union_(getPoint(i));
122 }
123 return bounds;
124 }
125
126 /**
127 * Creates a copy
128 * @return PointList A copy of this PointList
129 */
130 public PointList getCopy() {
131 PointList result = new PointList(size_);
132 System.arraycopy(points, 0, result.points, 0, size_ * 2);
133 result.size_ = size_;
134 result.bounds = null;
135 return result;
136 }
137
138 /**
139 * Returns the first Point in the list.
140 * @return The first point in the list.
141 * @throws IndexOutOfBoundsException if the list is empty
142 * @since 2.0
143 */
144 public Point getFirstPoint() {
145 return getPoint(0);
146 }
147
148 /**
149 * Returns the last point in the list.
150 * @throws IndexOutOfBoundsException if the list is empty
151 * @return The last Point in the list
152 * @since 2.0
153 */
154 public Point getLastPoint() {
155 return getPoint(size_ - 1);
156 }
157
158 /**
159 * Returns the midpoint of the list of Points. The midpoint is the median of the List,
160 * unless there are 2 medians (size is even), then the middle of the medians is returned.
161 * @return The midpoint
162 * @throws IndexOutOfBoundsException if the list is empty
163 */
164 public Point getMidpoint() {
165 if (size() % 2 is 0)
166 return getPoint(size() / 2 - 1).
167 getTranslated(getPoint(size() / 2)).
168 scale(0.5f);
169 return getPoint(size() / 2);
170 }
171
172 /**
173 * Returns the Point in the list at the specified index.
174 * @param index Index of the desired Point
175 * @return The requested Point
176 * @throws IndexOutOfBoundsException If the specified index is out of range
177 * @since 2.0
178 */
179 public Point getPoint(int index) {
180 if (index < 0 || index >= size_)
181 throw new IndexOutOfBoundsException( Format(
182 "Index: {}, Size: {}", index, //$NON-NLS-1$
183 size_)); //$NON-NLS-1$
184 index *= 2;
185 return new Point(points[index], points[index + 1]);
186 }
187
188 /**
189 * Copies the x and y values at given index into a specified Point.
190 * This method exists to avoid the creation of a new <code>Point</code>.
191 * @see #getPoint(int)
192 * @param p The Point which will be set with the &lt;x, y&gt; values
193 * @param index The index being requested
194 * @return The parameter <code>p</code> is returned for convenience
195 * @since 2.0
196 */
197 public Point getPoint(Point p, int index) {
198 if (index < 0 || index >= size_)
199 throw new IndexOutOfBoundsException( Format(
200 "Index: {}, Size: {}", index, //$NON-NLS-1$
201 size_)); //$NON-NLS-1$
202 index *= 2;
203 p.x = points[index];
204 p.y = points[index + 1];
205 return p;
206 }
207
208 /**
209 * Inserts a given point at a specified index.
210 * @param p Point to be inserted.
211 * @param index Position where the point is to be inserted.
212 * @exception IndexOutOfBoundsException if the index is invalid
213 * @see #setPoint(Point, int)
214 * @since 2.0
215 */
216 public void insertPoint(Point p, int index) {
217 if (bounds !is null && !bounds.contains(p))
218 bounds = null;
219 if (index > size_ || index < 0)
220 throw new IndexOutOfBoundsException( Format(
221 "Index: {}, Size: {}", index, //$NON-NLS-1$
222 size_)); //$NON-NLS-1$
223 index *= 2;
224
225 int length = points.length;
226 int old[] = points;
227 points = new int[length + 2];
228 System.arraycopy(old, 0, points, 0, index);
229 System.arraycopy(old, index, points, index + 2, length - index);
230
231 points[index] = p.x;
232 points[index + 1] = p.y;
233 size_++;
234 }
235
236 /**
237 * Determines whether any of the line segments represented by this PointList intersect
238 * the given Rectangle. If a segment touches the given rectangle, that's considered
239 * intersection.
240 *
241 * @param r the rectangle
242 * @return <code>true</code> if the given rectangle intersects any of the line segments
243 * represented by this PointList
244 * @since 3.1
245 */
246 public bool intersects(Rectangle r) {
247 if (r.isEmpty())
248 return false;
249 for (int i = 0; i < size_ * 2; i += 2) {
250 if (r.contains(points[i], points[i + 1]))
251 return true;
252 }
253 int diagonal1x1 = r.x,
254 diagonal1y1 = r.y,
255 diagonal1x2 = r.x + r.width - 1,
256 diagonal1y2 = r.y + r.height - 1,
257 diagonal2x1 = r.x + r.width - 1,
258 diagonal2y1 = r.y,
259 diagonal2x2 = r.x,
260 diagonal2y2 = r.y + r.height - 1;
261 for (int i = 0; i < (size_ - 1) * 2; i += 2) {
262 if (Geometry.linesIntersect(diagonal1x1, diagonal1y1, diagonal1x2,
263 diagonal1y2, points[i], points[i + 1], points[i + 2], points[i + 3])
264 || Geometry.linesIntersect(diagonal2x1, diagonal2y1, diagonal2x2,
265 diagonal2y2, points[i], points[i + 1], points[i + 2], points[i + 3]))
266 return true;
267 }
268 return false;
269 }
270
271 /**
272 * @see dwtx.draw2d.geometry.Translatable#performScale(double)
273 */
274 public void performScale(double factor) {
275 for (int i = 0; i < points.length; i++)
276 points[i] = cast(int)Math.floor(points[i] * factor);
277 bounds = null;
278 }
279
280 /**
281 * @see dwtx.draw2d.geometry.Translatable#performTranslate(int, int)
282 */
283 public void performTranslate(int dx, int dy) {
284 for (int i = 0; i < size_ * 2; i += 2) {
285 points[i] += dx;
286 points[i + 1] += dy;
287 }
288 if (bounds !is null)
289 bounds.translate(dx, dy);
290 }
291
292 /**
293 * Removes all the points stored by this list. Resets all
294 * the properties based on the point information.
295 *
296 * @since 2.0
297 */
298 public void removeAllPoints() {
299 bounds = null;
300 size_ = 0;
301 }
302
303 /**
304 * Removes the point at the specified index from the PointList, and
305 * returns it.
306 * @since 2.0
307 * @see #addPoint(Point)
308 * @param index Index of the point to be removed.
309 * @return The point which has been removed
310 * @throws IndexOutOfBoundsException if the removal index is beyond the list capacity
311 */
312 public Point removePoint(int index) {
313 bounds = null;
314 if (index < 0 || index >= size_)
315 throw new IndexOutOfBoundsException( Format(
316 "Index: {}, Size: {}", index, //$NON-NLS-1$
317 size_)); //$NON-NLS-1$
318
319 index *= 2;
320 Point pt = new Point(points[index], points[index + 1]);
321 if (index !is size_ * 2 - 2)
322 System.arraycopy(points, index + 2, points, index, size_ * 2 - index - 2);
323 size_--;
324 return pt;
325 }
326
327 /**
328 * Reverses the order of the points in the list.
329 * @since 3.2
330 */
331 public void reverse() {
332 int temp;
333 for (int i = 0, j = size_ * 2 - 2; i < size_; i += 2 , j -= 2) {
334 temp = points[i];
335 points[i] = points[j];
336 points[j] = temp;
337 temp = points[i + 1];
338 points[i + 1] = points[j + 1];
339 points[j + 1] = temp;
340 }
341 }
342
343 /**
344 * Overwrites a point at a given index in the list with the specified Point.
345 * @param pt Point which is to be stored at the index.
346 * @param index Index where the given point is to be stored.
347 * @since 2.0
348 */
349 public void setPoint(Point pt, int index) {
350 if (index < 0 || index >= size_)
351 throw new IndexOutOfBoundsException( Format(
352 "Index: {}, Size: {}", index, //$NON-NLS-1$
353 size_)); //$NON-NLS-1$
354 if (bounds !is null && !bounds.contains(pt))
355 bounds = null;
356 points[index * 2] = pt.x;
357 points[index * 2 + 1] = pt.y;
358 }
359
360 /**
361 * Sets the size of this PointList.
362 * @param newSize the new size
363 */
364 public void setSize(int newSize) {
365 if (points.length > newSize * 2) {
366 size_ = newSize;
367 return;
368 }
369 int[] newArray = new int[newSize * 2];
370 System.arraycopy(points, 0, newArray, 0, points.length);
371 points = newArray;
372 size_ = newSize;
373 }
374
375 /**
376 * Returns the number of points in this PointList.
377 * @return The number of points
378 * @since 2.0
379 */
380 public int size() {
381 return size_;
382 }
383
384 /**
385 * Returns the contents of this PointList as an integer array. The returned array is by
386 * reference. Any changes made to the array will also be changing the original PointList.
387 *
388 * @return the integer array of points by reference
389 * @since 2.0
390 */
391 public int[] toIntArray() {
392 if (points.length !is size_ * 2) {
393 int[] old = points;
394 points = new int[size_ * 2];
395 System.arraycopy(old, 0, points, 0, size_ * 2);
396 }
397 return points;
398 }
399
400 /**
401 * Moves the origin (0,0) of the coordinate system of all
402 * the points to the Point <i>pt</i>. This updates the position
403 * of all the points in this PointList.
404 *
405 * @param pt Position by which all the points will be shifted.
406 * @see #translate(int,int)
407 * @since 2.0
408 */
409 public final void translate(Point pt) {
410 translate(pt.x, pt.y);
411 }
412
413 /**
414 * Moves the origin (0,0) of the coordinate system of all
415 * the points to the Point (x,y). This updates the position
416 * of all the points in this PointList.
417 *
418 * @param x Amount by which all the points will be shifted on the X axis.
419 * @param y Amount by which all the points will be shifted on the Y axis.
420 * @see #translate(Point)
421 * @since 2.0
422 */
423 public void translate(int x, int y) {
424 if (x is 0 && y is 0)
425 return;
426 if (bounds !is null)
427 bounds.translate(x, y);
428 for (int i = 0; i < size_ * 2; i += 2) {
429 points[i] += x;
430 points[i + 1] += y;
431 }
432 }
433
434 /**
435 * Transposes all x and y values. Useful for orientation changes.
436 * @since 3.2
437 */
438 public void transpose() {
439 int temp;
440 if (bounds !is null)
441 bounds.transpose();
442 for (int i = 0; i < size_ * 2; i += 2) {
443 temp = points[i];
444 points[i] = points[i + 1];
445 points[i + 1] = temp;
446 }
447 }
448
449 }