comparison dwtx/draw2d/graph/Obstacle.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) 2004, 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.graph.Obstacle;
14
15 import dwt.dwthelper.utils;
16 import tango.text.convert.Format;
17
18 import dwtx.draw2d.PositionConstants;
19 import dwtx.draw2d.geometry.Point;
20 import dwtx.draw2d.geometry.Rectangle;
21 import dwtx.draw2d.graph.Vertex;
22 import dwtx.draw2d.graph.ShortestPathRouter;
23
24 /**
25 * An obstacle representation for the ShortestPathRouting. This is a subclass of Rectangle.
26 *
27 * This class is for internal use only.
28 * @author Whitney Sorenson
29 * @since 3.0
30 */
31 class Obstacle
32 : Rectangle
33 {
34
35 bool exclude;
36 Vertex topLeft, topRight, bottomLeft, bottomRight, center;
37 private ShortestPathRouter router;
38
39 /**
40 * Creates a new obstacle from the given rectangle bounds.
41 * @param rect the bounds
42 */
43 this(Rectangle rect, ShortestPathRouter router) {
44 init(rect);
45 this.router = router;
46 }
47
48 /**
49 * Returns <code>true</code> if the given point is contained but not on the boundary of
50 * this obstacle.
51 * @param p a point
52 * @return <code>true</code> if properly contained
53 */
54 public bool containsProper(Point p) {
55 return p.x > this.x
56 && p.x < this.x + this.width - 1
57 && p.y > this.y
58 && p.y < this.y + this.height - 1;
59 }
60
61 public int getSpacing() {
62 return router.getSpacing();
63 }
64
65 private void growVertex(Vertex vertex) {
66 if (vertex.totalCount > 0)
67 vertex.grow();
68 }
69
70 /**
71 * Grows all vertices on this obstacle.
72 */
73 void growVertices() {
74 growVertex(topLeft);
75 growVertex(topRight);
76 growVertex(bottomLeft);
77 growVertex(bottomRight);
78 }
79
80 /**
81 * Initializes this obstacle to the values of the given rectangle
82 *
83 * @param rect bounds of this obstacle
84 */
85 void init(Rectangle rect) {
86 this.x = rect.x;
87 this.y = rect.y;
88 this.width = rect.width;
89 this.height = rect.height;
90
91 exclude = false;
92
93 topLeft = new Vertex(x, y, this);
94 topLeft.positionOnObstacle = PositionConstants.NORTH_WEST;
95 topRight = new Vertex(x + width - 1, y, this);
96 topRight.positionOnObstacle = PositionConstants.NORTH_EAST;
97 bottomLeft = new Vertex(x, y + height - 1, this);
98 bottomLeft.positionOnObstacle = PositionConstants.SOUTH_WEST;
99 bottomRight = new Vertex(x + width - 1, y + height - 1, this);
100 bottomRight.positionOnObstacle = PositionConstants.SOUTH_EAST;
101 center = new Vertex(getCenter(), this);
102 }
103
104 /**
105 * Requests a full reset on all four vertices of this obstacle.
106 */
107 void reset() {
108 topLeft.fullReset();
109 bottomLeft.fullReset();
110 bottomRight.fullReset();
111 topRight.fullReset();
112 }
113
114 private void shrinkVertex(Vertex vertex) {
115 if (vertex.totalCount > 0)
116 vertex.shrink();
117 }
118
119 /**
120 * Shrinks all four vertices of this obstacle.
121 */
122 void shrinkVertices() {
123 shrinkVertex(topLeft);
124 shrinkVertex(topRight);
125 shrinkVertex(bottomLeft);
126 shrinkVertex(bottomRight);
127 }
128
129 /**
130 * @see dwtx.draw2d.geometry.Rectangle#toString()
131 */
132 public String toString() {
133 return Format("Obstacle({}, {}, {}, {})", x, y, //$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
134 width, height );//$NON-NLS-2$//$NON-NLS-1$
135 }
136
137 }