source: trunk/src/org/expeditee/core/bounds/CombinationBounds.java@ 1097

Last change on this file since 1097 was 1097, checked in by davidb, 6 years ago

Newly structured files from Corey's work on logic/graphics separation

File size: 5.4 KB
Line 
1package org.expeditee.core.bounds;
2
3import java.util.LinkedList;
4import java.util.List;
5
6import org.expeditee.core.Point;
7
8/**
9 * The CombinationBounds class represents a collection of bounds, which
10 * may be disjoint. A point is considered inside the CombinationBounds if
11 * it is inside any of the individual bounds in the collection (i.e. if it
12 * is inside the conjunction of the individual bounds). If the combination
13 * bounds has no component bounds, then no points are inside it.
14 *
15 * @author cts16
16 */
17public class CombinationBounds extends Bounds {
18
19 /** The collection of bounds. */
20 protected List<Bounds> _bounds;
21
22 /** Default constructor. */
23 public CombinationBounds()
24 {
25 this(null);
26 }
27
28 /** Standard constructor. */
29 public CombinationBounds(Bounds b)
30 {
31 _bounds = new LinkedList<Bounds>();
32 add(b);
33 }
34
35 /** Copy constructor. */
36 public CombinationBounds(CombinationBounds other)
37 {
38 _bounds = new LinkedList<Bounds>();
39 for (Bounds b : other._bounds) add(b);
40 }
41
42 /** Adds a bounds to the combination. */
43 public CombinationBounds add(Bounds b)
44 {
45 // Only valid Bounds are allowed in the collection
46 if (b == null) return this;
47
48 _bounds.add(b);
49
50 return this;
51 }
52
53 /** Gets the bounds at the given index in this combination. */
54 public Bounds get(int index)
55 {
56 if (0 <= index && index < _bounds.size()) return _bounds.get(index);
57
58 return null;
59 }
60
61 /** Removes all component bounds. */
62 public CombinationBounds clear()
63 {
64 _bounds.clear();
65
66 return this;
67 }
68
69 /** Whether this combination bounds has no component bounds. */
70 public boolean isEmpty()
71 {
72 return _bounds.isEmpty();
73 }
74
75 @Override
76 public boolean contains(int x, int y)
77 {
78 // If any component bounds contains the point, then the combination as
79 // a whole does as well
80 for (Bounds b : _bounds) {
81 if (b.contains(x, y)) return true;
82 }
83
84 // No component contained the point
85 return false;
86 }
87
88 @Override
89 public int getMaxX()
90 {
91 int maxX = Integer.MIN_VALUE;
92
93 for (Bounds b : _bounds) {
94 int thisX = b.getMaxX();
95 if (thisX > maxX) maxX = thisX;
96 }
97
98 return maxX;
99 }
100
101 @Override
102 public int getMinX()
103 {
104 int minX = Integer.MAX_VALUE;
105
106 for (Bounds b : _bounds) {
107 int thisX = b.getMinX();
108 if (thisX < minX) minX = thisX;
109 }
110
111 return minX;
112 }
113
114 @Override
115 public int getMaxY()
116 {
117 int maxY = Integer.MIN_VALUE;
118
119 for (Bounds b : _bounds) {
120 int thisY = b.getMaxY();
121 if (thisY > maxY) maxY = thisY;
122 }
123
124 return maxY;
125 }
126
127 @Override
128 public int getMinY()
129 {
130 int minY = Integer.MAX_VALUE;
131
132 for (Bounds b : _bounds) {
133 int thisY = b.getMinY();
134 if (thisY < minY) minY = thisY;
135 }
136
137 return minY;
138 }
139
140 @Override
141 public CombinationBounds translate(int x, int y)
142 {
143 for (Bounds b : _bounds) {
144 b.translate(x, y);
145 }
146
147 return this;
148 }
149
150 @Override
151 public boolean equals(Bounds other)
152 {
153 if (other instanceof CombinationBounds) {
154 CombinationBounds c = (CombinationBounds) other;
155 if (_bounds.size() == c._bounds.size()) {
156 for (int i = 0; i < _bounds.size(); i++) {
157 if (!(_bounds.get(i).equals(c._bounds.get(i)))) return false;
158 }
159 return true;
160 }
161 }
162 return false;
163 }
164
165 @Override
166 public boolean intersects(Bounds other)
167 {
168 for (Bounds b : _bounds) {
169 if (b.intersects(other)) return true;
170 }
171
172 return false;
173 }
174
175 @Override
176 public boolean intersects(AxisAlignedBoxBounds other)
177 {
178 return intersects((Bounds) other);
179 }
180
181 @Override
182 public boolean intersects(CombinationBounds other)
183 {
184 return intersects((Bounds) other);
185 }
186
187 @Override
188 public boolean intersects(EllipticalBounds other)
189 {
190 return intersects((Bounds) other);
191 }
192
193 @Override
194 public boolean intersects(PolygonBounds other)
195 {
196 return intersects((Bounds) other);
197 }
198
199 @Override
200 public double getArea()
201 {
202 // TODO Complete. Difficult as have to take into account overlapping areas. cts16
203 System.err.println("Unimplemented method: CombinationBounds.getArea()");
204 return 0;
205 }
206
207 @Override
208 public boolean perimeterContains(Point p, double error)
209 {
210 for (Bounds b : _bounds) {
211 if (b.perimeterContains(p, error)) {
212 for (Bounds b2 : _bounds) {
213 if (b2 != b && b2.contains(p) && !b2.perimeterContains(p, error)) return false;
214 }
215 return true;
216 }
217 }
218
219 return false;
220 }
221
222 @Override
223 public boolean completelyContains(AxisAlignedBoxBounds other)
224 {
225 // TODO Complete. Difficult as we have to take into account conjoined areas. cts16
226 System.err.println("Unimplemented method: CombinationBounds.completelyContains(AxisAlignedBoxBounds)");
227 return false;
228 }
229
230 @Override
231 public boolean completelyContains(CombinationBounds other)
232 {
233 // TODO Complete. Difficult as we have to take into account conjoined areas. cts16
234 System.err.println("Unimplemented method: CombinationBounds.completelyContains(CombinationBounds)");
235 return false;
236 }
237
238 @Override
239 public boolean completelyContains(EllipticalBounds other)
240 {
241 // TODO Complete. Difficult as we have to take into account conjoined areas. cts16
242 System.err.println("Unimplemented method: CombinationBounds.completelyContains(EllipticalBounds)");
243 return false;
244 }
245
246 @Override
247 public boolean completelyContains(PolygonBounds other)
248 {
249 // TODO Complete. Difficult as we have to take into account conjoined areas. cts16
250 System.err.println("Unimplemented method: CombinationBounds.completelyContains(PolygonBounds)");
251 return false;
252 }
253
254 @Override
255 public CombinationBounds clone()
256 {
257 return new CombinationBounds(this);
258 }
259}
Note: See TracBrowser for help on using the repository browser.