Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2003, 2005 IBM Corporation and others. |
2 |
* Copyright (c) 2003, 2007 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 10-15
Link Here
|
10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.draw2d.geometry; |
11 |
package org.eclipse.draw2d.geometry; |
12 |
|
12 |
|
|
|
13 |
|
13 |
/** |
14 |
/** |
14 |
* A Rectangle implementation using floating point values which are truncated into the inherited |
15 |
* A Rectangle implementation using floating point values which are truncated into the inherited |
15 |
* integer fields. The use of floating point prevents rounding errors from accumulating. |
16 |
* integer fields. The use of floating point prevents rounding errors from accumulating. |
Lines 40-57
Link Here
|
40 |
* @param rect the base rectangle |
41 |
* @param rect the base rectangle |
41 |
*/ |
42 |
*/ |
42 |
public PrecisionRectangle(Rectangle rect) { |
43 |
public PrecisionRectangle(Rectangle rect) { |
43 |
if (rect instanceof PrecisionRectangle) { |
44 |
preciseX = rect.preciseX(); |
44 |
PrecisionRectangle rectangle = (PrecisionRectangle)rect; |
45 |
preciseY = rect.preciseY(); |
45 |
preciseX = rectangle.preciseX; |
46 |
preciseWidth = rect.preciseWidth(); |
46 |
preciseY = rectangle.preciseY; |
47 |
preciseHeight = rect.preciseHeight(); |
47 |
preciseWidth = rectangle.preciseWidth; |
|
|
48 |
preciseHeight = rectangle.preciseHeight; |
49 |
} else { |
50 |
preciseX = rect.x; |
51 |
preciseY = rect.y; |
52 |
preciseWidth = rect.width; |
53 |
preciseHeight = rect.height; |
54 |
} |
55 |
updateInts(); |
48 |
updateInts(); |
56 |
} |
49 |
} |
57 |
|
50 |
|
Lines 148-161
Link Here
|
148 |
* @see org.eclipse.draw2d.geometry.Rectangle#resize(org.eclipse.draw2d.geometry.Dimension) |
141 |
* @see org.eclipse.draw2d.geometry.Rectangle#resize(org.eclipse.draw2d.geometry.Dimension) |
149 |
*/ |
142 |
*/ |
150 |
public Rectangle resize(Dimension sizeDelta) { |
143 |
public Rectangle resize(Dimension sizeDelta) { |
151 |
if (sizeDelta instanceof PrecisionDimension) { |
144 |
preciseWidth += sizeDelta.preciseWidth(); |
152 |
PrecisionDimension pd = (PrecisionDimension)sizeDelta; |
145 |
preciseHeight += sizeDelta.preciseHeight(); |
153 |
preciseWidth += pd.preciseWidth; |
|
|
154 |
preciseHeight += pd.preciseHeight; |
155 |
} else { |
156 |
preciseWidth += sizeDelta.width; |
157 |
preciseHeight += sizeDelta.height; |
158 |
} |
159 |
updateInts(); |
146 |
updateInts(); |
160 |
return this; |
147 |
return this; |
161 |
} |
148 |
} |
Lines 200-213
Link Here
|
200 |
* @see org.eclipse.draw2d.geometry.Rectangle#translate(org.eclipse.draw2d.geometry.Point) |
187 |
* @see org.eclipse.draw2d.geometry.Rectangle#translate(org.eclipse.draw2d.geometry.Point) |
201 |
*/ |
188 |
*/ |
202 |
public Rectangle translate(Point p) { |
189 |
public Rectangle translate(Point p) { |
203 |
if (p instanceof PrecisionPoint) { |
190 |
preciseX += p.preciseX(); |
204 |
PrecisionPoint pp = (PrecisionPoint)p; |
191 |
preciseY += p.preciseY(); |
205 |
preciseX += pp.preciseX; |
|
|
206 |
preciseY += pp.preciseY; |
207 |
} else { |
208 |
preciseX += p.x; |
209 |
preciseY += p.y; |
210 |
} |
211 |
updateInts(); |
192 |
updateInts(); |
212 |
return this; |
193 |
return this; |
213 |
} |
194 |
} |
Lines 218-223
Link Here
|
218 |
* @since 3.0 |
199 |
* @since 3.0 |
219 |
* @param other the rectangle being unioned |
200 |
* @param other the rectangle being unioned |
220 |
* @return <code>this</code> for convenience |
201 |
* @return <code>this</code> for convenience |
|
|
202 |
* @deprecated |
203 |
* Use {@link #union(Rectangle)} instead |
221 |
*/ |
204 |
*/ |
222 |
public PrecisionRectangle union(PrecisionRectangle other) { |
205 |
public PrecisionRectangle union(PrecisionRectangle other) { |
223 |
double newright = Math.max(preciseRight(), other.preciseRight()); |
206 |
double newright = Math.max(preciseRight(), other.preciseRight()); |
Lines 232-237
Link Here
|
232 |
} |
215 |
} |
233 |
|
216 |
|
234 |
/** |
217 |
/** |
|
|
218 |
* @see org.eclipse.draw2d.geometry.Rectangle#union(org.eclipse.draw2d.geometry.Rectangle) |
219 |
*/ |
220 |
public Rectangle union(Rectangle other) { |
221 |
double newright = Math.max(preciseRight(), other.preciseX() + other.preciseWidth()); |
222 |
double newbottom = Math.max(preciseBottom(), other.preciseY() + other.preciseHeight()); |
223 |
preciseX = Math.min(preciseX, other.preciseX()); |
224 |
preciseY = Math.min(preciseY, other.preciseY()); |
225 |
preciseWidth = newright - preciseX; |
226 |
preciseHeight = newbottom - preciseY; |
227 |
updateInts(); |
228 |
|
229 |
return this; |
230 |
} |
231 |
|
232 |
/** |
235 |
* Updates the integer values based on the current precise values. The integer values ar |
233 |
* Updates the integer values based on the current precise values. The integer values ar |
236 |
* the floor of the double values. This is called automatically when calling api which is |
234 |
* the floor of the double values. This is called automatically when calling api which is |
237 |
* overridden in this class. |
235 |
* overridden in this class. |
Lines 244-247
Link Here
|
244 |
height = (int)Math.floor(preciseHeight + preciseY + 0.000000001) - y; |
242 |
height = (int)Math.floor(preciseHeight + preciseY + 0.000000001) - y; |
245 |
} |
243 |
} |
246 |
|
244 |
|
|
|
245 |
/** |
246 |
* @see org.eclipse.draw2d.geometry.Rectangle#union(org.eclipse.draw2d.geometry.Point) |
247 |
*/ |
248 |
public void union(Point p) { |
249 |
if (p.preciseX() < preciseX) { |
250 |
preciseWidth += (preciseX - p.preciseX()); |
251 |
preciseX = p.preciseX(); |
252 |
} else { |
253 |
double right = preciseX + preciseWidth; |
254 |
if (p.preciseX() > right) { |
255 |
preciseWidth = p.preciseX() - preciseX; |
256 |
} |
257 |
} |
258 |
if (p.preciseY() < preciseY) { |
259 |
preciseHeight += (preciseY - p.preciseY()); |
260 |
preciseY = p.preciseY(); |
261 |
} else { |
262 |
double bottom = preciseY + preciseHeight; |
263 |
if (p.preciseY() > bottom) { |
264 |
preciseHeight = p.preciseY() - preciseY; |
265 |
} |
266 |
} |
267 |
updateInts(); |
268 |
} |
269 |
|
270 |
/** |
271 |
* @see org.eclipse.draw2d.geometry.Rectangle#transpose() |
272 |
*/ |
273 |
public Rectangle transpose() { |
274 |
double temp = preciseX; |
275 |
preciseX = preciseY; |
276 |
preciseY = temp; |
277 |
temp = preciseWidth; |
278 |
preciseWidth = preciseHeight; |
279 |
preciseHeight = temp; |
280 |
super.transpose(); |
281 |
return this; |
282 |
} |
283 |
|
284 |
/** |
285 |
* @see org.eclipse.draw2d.geometry.Rectangle#setLocation(org.eclipse.draw2d.geometry.Point) |
286 |
*/ |
287 |
public Rectangle setLocation(Point loc) { |
288 |
preciseX = loc.preciseX(); |
289 |
preciseY = loc.preciseY(); |
290 |
updateInts(); |
291 |
return this; |
292 |
} |
293 |
|
294 |
/** |
295 |
* Returns the precise geometric centre of the rectangle |
296 |
* |
297 |
* @return <code>PrecisionPoint</code> geometric center of the rectangle |
298 |
* @since 3.4 |
299 |
*/ |
300 |
public PrecisionPoint getPreciseCenter() { |
301 |
return new PrecisionPoint(preciseX + preciseWidth / 2.0, preciseY + preciseHeight / 2.0); |
302 |
} |
303 |
|
304 |
/** |
305 |
* Shrinks the sides of this Rectangle by the horizontal and vertical values |
306 |
* provided as input, and returns this Rectangle for convenience. The center of |
307 |
* this Rectangle is kept constant. |
308 |
* |
309 |
* @param h Horizontal reduction amount |
310 |
* @param v Vertical reduction amount |
311 |
* @return <code>this</code> for convenience |
312 |
* @since 3.4 |
313 |
*/ |
314 |
public Rectangle shrink(double h, double v) { |
315 |
preciseX += h; |
316 |
preciseWidth -= (h + h); |
317 |
preciseY += v; |
318 |
preciseHeight -= (v + v); |
319 |
updateInts(); |
320 |
return this; |
321 |
} |
322 |
|
323 |
/** |
324 |
* Expands the horizontal and vertical sides of this Rectangle with the values |
325 |
* provided as input, and returns this for convenience. The location of its |
326 |
* center is kept constant. |
327 |
* |
328 |
* @param h Horizontal increment |
329 |
* @param v Vertical increment |
330 |
* @return <code>this</code> for convenience |
331 |
* @since 3.4 |
332 |
*/ |
333 |
public Rectangle expand(double h, double v) { |
334 |
return shrink(-h, -v); |
335 |
} |
336 |
|
337 |
/** |
338 |
* @see org.eclipse.draw2d.geometry.Rectangle#shrink(int, int) |
339 |
*/ |
340 |
public Rectangle shrink(int h, int v) { |
341 |
return shrink((double)h, (double)v); |
342 |
} |
343 |
|
344 |
/** |
345 |
* @see org.eclipse.draw2d.geometry.Rectangle#contains(org.eclipse.draw2d.geometry.Point) |
346 |
*/ |
347 |
public boolean contains(Point p) { |
348 |
return preciseX <= p.preciseX() && p.preciseX() <= preciseX + preciseWidth |
349 |
&& preciseY <= p.preciseY() && p.preciseY() <= preciseY + preciseHeight; |
350 |
} |
351 |
|
352 |
double preciseX() { |
353 |
return preciseX; |
354 |
} |
355 |
|
356 |
double preciseY() { |
357 |
return preciseY; |
358 |
} |
359 |
|
360 |
double preciseWidth() { |
361 |
return preciseWidth; |
362 |
} |
363 |
|
364 |
double preciseHeight() { |
365 |
return preciseHeight; |
366 |
} |
367 |
|
247 |
} |
368 |
} |