Project

General

Profile

Download (7.69 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 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
 *******************************************************************************/
11
package org.eclipse.draw2d.geometry;
12

    
13
/**
14
 * @author danlee
15
 */
16
public class PrecisionPoint extends Point {
17

    
18
	/**
19
	 * Double value for X
20
	 * 
21
	 * @noreference
22
	 * @deprecated Use {@link #setPreciseX(double)} and {@link #preciseX()}
23
	 *             instead. This field will become private in future versions.
24
	 */
25
	public double preciseX;
26

    
27
	/**
28
	 * Double value for Y
29
	 * 
30
	 * @noreference
31
	 * @deprecated Use {@link #setPreciseY(double)} and {@link #preciseY()}
32
	 *             instead. This field will become private in future versions.
33
	 */
34
	public double preciseY;
35

    
36
	/**
37
	 * Constructor for PrecisionPoint.
38
	 */
39
	public PrecisionPoint() {
40
		super();
41
	}
42

    
43
	/**
44
	 * Constructor for PrecisionPoint.
45
	 * 
46
	 * @param x
47
	 *            X value
48
	 * @param y
49
	 *            Y value
50
	 */
51
	public PrecisionPoint(double x, double y) {
52
		setPreciseLocation(x, y);
53
	}
54

    
55
	/**
56
	 * Constructor for PrecisionPoint.
57
	 * 
58
	 * @param x
59
	 *            X value
60
	 * @param y
61
	 *            Y value
62
	 */
63
	public PrecisionPoint(int x, int y) {
64
		this((double) x, (double) y);
65
	}
66

    
67
	/**
68
	 * Constructor for PrecisionPoint.
69
	 * 
70
	 * @param p
71
	 *            Point from which the initial values are taken
72
	 */
73
	public PrecisionPoint(Point p) {
74
		this(p.preciseX(), p.preciseY());
75
	}
76

    
77
	/**
78
	 * @see org.eclipse.draw2d.geometry.Point#equals(java.lang.Object)
79
	 */
80
	public boolean equals(Object o) {
81
		if (o instanceof PrecisionPoint) {
82
			PrecisionPoint p = (PrecisionPoint) o;
83
			return p.preciseX() == preciseX() && p.preciseY() == preciseY();
84
		}
85
		return super.equals(o);
86
	}
87

    
88
	/**
89
	 * @see org.eclipse.draw2d.geometry.Point#getCopy()
90
	 */
91
	public Point getCopy() {
92
		return getPreciseCopy();
93
	}
94

    
95
	/**
96
	 * @see org.eclipse.draw2d.geometry.Point#getDifference(org.eclipse.draw2d.geometry.Point)
97
	 */
98
	public Dimension getDifference(Point p) {
99
		return new PrecisionDimension(this.preciseX() - p.preciseX(),
100
				this.preciseY() - p.preciseY());
101
	}
102

    
103
	/**
104
	 * Returns a precise copy of this.
105
	 * 
106
	 * @return a precise copy
107
	 * @since 3.7
108
	 */
109
	public PrecisionPoint getPreciseCopy() {
110
		return new PrecisionPoint(preciseX(), preciseY());
111
	}
112

    
113
	/**
114
	 * @see org.eclipse.draw2d.geometry.Point#performScale(double)
115
	 */
116
	public void performScale(double factor) {
117
		setPreciseX(preciseX() * factor);
118
		setPreciseY(preciseY() * factor);
119
	}
120

    
121
	/**
122
	 * @see org.eclipse.draw2d.geometry.Point#performTranslate(int, int)
123
	 */
124
	public void performTranslate(int dx, int dy) {
125
		setPreciseX(preciseX() + dx);
126
		setPreciseY(preciseY() + dy);
127
	}
128

    
129
	/**
130
	 * @see org.eclipse.draw2d.geometry.Point#preciseX()
131
	 */
132
	public double preciseX() {
133
		updatePreciseXDouble();
134
		return preciseX;
135
	}
136

    
137
	/**
138
	 * @see org.eclipse.draw2d.geometry.Point#preciseY()
139
	 */
140
	public double preciseY() {
141
		updatePreciseYDouble();
142
		return preciseY;
143
	}
144

    
145
	/**
146
	 * @see org.eclipse.draw2d.geometry.Point#scale(double, double)
147
	 */
148
	public Point scale(double xFactor, double yFactor) {
149
		setPreciseX(preciseX() * xFactor);
150
		setPreciseY(preciseY() * yFactor);
151
		return this;
152
	}
153

    
154
	/**
155
	 * @see org.eclipse.draw2d.geometry.Point#setLocation(int, int)
156
	 */
157
	public Point setLocation(int x, int y) {
158
		return setPreciseLocation(x, y);
159
	}
160

    
161
	/**
162
	 * @see org.eclipse.draw2d.geometry.Point#setLocation(Point)
163
	 */
164
	public Point setLocation(Point pt) {
165
		return setPreciseLocation(pt.preciseX(), pt.preciseY());
166
	}
167

    
168
	/**
169
	 * Sets the precise location of this PrecisionPoint to the given x and y
170
	 * values.
171
	 * 
172
	 * @param x
173
	 *            The new x value
174
	 * @param y
175
	 *            The new y value
176
	 * @return this for convenience
177
	 * @since 3.7
178
	 */
179
	public PrecisionPoint setPreciseLocation(double x, double y) {
180
		setPreciseX(x);
181
		setPreciseY(y);
182
		return this;
183
	}
184

    
185
	/**
186
	 * Sets the precise location of this PrecisionPoint to the x and y values of
187
	 * the given one.
188
	 * 
189
	 * @param p
190
	 *            The PrecisionPoint specifying the new x and y values.
191
	 * @return this for convenience
192
	 * @since 3.7
193
	 */
194
	public PrecisionPoint setPreciseLocation(PrecisionPoint p) {
195
		return setPreciseLocation(p.preciseX(), p.preciseY());
196
	}
197

    
198
	/**
199
	 * Sets the precise x value of this PrecisionPoint to the given value.
200
	 * 
201
	 * @param x
202
	 *            The new x value
203
	 * @return this for convenience
204
	 * @since 3.7
205
	 */
206
	public PrecisionPoint setPreciseX(double x) {
207
		preciseX = x;
208
		updateXInt();
209
		return this;
210
	}
211

    
212
	/**
213
	 * Sets the precise y value of this PrecisionPoint to the given value.
214
	 * 
215
	 * @param y
216
	 *            The new y value
217
	 * @return this for convenience
218
	 * @since 3.7
219
	 */
220
	public PrecisionPoint setPreciseY(double y) {
221
		preciseY = y;
222
		updateYInt();
223
		return this;
224
	}
225

    
226
	/**
227
	 * @see org.eclipse.draw2d.geometry.Point#setX(int)
228
	 */
229
	public Point setX(int x) {
230
		return setPreciseX(x);
231
	}
232

    
233
	/**
234
	 * @see org.eclipse.draw2d.geometry.Point#setY(int)
235
	 */
236
	public Point setY(int y) {
237
		return setPreciseY(y);
238
	}
239

    
240
	/**
241
	 * @see org.eclipse.draw2d.geometry.Point#translate(org.eclipse.draw2d.geometry.Dimension)
242
	 */
243
	public Point translate(Dimension d) {
244
		return translatePrecise(d.preciseWidth(), d.preciseHeight());
245
	}
246

    
247
	/**
248
	 * @see org.eclipse.draw2d.geometry.Point#translate(int, int)
249
	 */
250
	public Point translate(int dx, int dy) {
251
		return translatePrecise(dx, dy);
252
	}
253

    
254
	/**
255
	 * @see org.eclipse.draw2d.geometry.Point#translate(org.eclipse.draw2d.geometry.Point)
256
	 */
257
	public Point translate(Point p) {
258
		return translatePrecise(p.preciseX(), p.preciseY());
259
	}
260

    
261
	/**
262
	 * Shifts this Point by the values supplied along each axes, and returns
263
	 * this for convenience.
264
	 * 
265
	 * @param dx
266
	 *            Amount by which point is shifted along X axis.
267
	 * @param dy
268
	 *            Amount by which point is shifted along Y axis.
269
	 * @return <code>this</code> for convenience
270
	 * @since 3.7
271
	 */
272
	private PrecisionPoint translatePrecise(double dx, double dy) {
273
		setPreciseX(preciseX() + dx);
274
		setPreciseY(preciseY() + dy);
275
		return this;
276
	}
277

    
278
	/**
279
	 * @see org.eclipse.draw2d.geometry.Point#transpose()
280
	 */
281
	public Point transpose() {
282
		double temp = preciseX();
283
		setPreciseX(preciseY());
284
		setPreciseY(temp);
285
		return this;
286
	}
287

    
288
	/**
289
	 * Updates the integer fields using the precise versions.
290
	 * 
291
	 * @deprecated This method should not be accessed by clients any more (it
292
	 *             will be made private in future releases). The update of
293
	 *             integer and precision fields is performed automatically if
294
	 *             {@link #preciseX} and {@link #preciseY} field values are not
295
	 *             manipulated directly, but only via respective methods offered
296
	 *             by this class.
297
	 */
298
	public final void updateInts() {
299
		updateXInt();
300
		updateYInt();
301
	}
302

    
303
	/**
304
	 * Updates the x integer field using the value of preciseX.
305
	 */
306
	private final void updateXInt() {
307
		x = PrecisionGeometry.doubleToInteger(preciseX);
308
	}
309

    
310
	/**
311
	 * Updates the y integer field using the value of preciseY.
312
	 */
313
	private final void updateYInt() {
314
		y = PrecisionGeometry.doubleToInteger(preciseY);
315
	}
316

    
317
	/**
318
	 * Updates the preciseX double field using the value of x.
319
	 */
320
	private final void updatePreciseXDouble() {
321
		if (x != PrecisionGeometry.doubleToInteger(preciseX)) {
322
			preciseX = x;
323
		}
324
	}
325

    
326
	/**
327
	 * Updates the preciseY double field using the value of y.
328
	 */
329
	private final void updatePreciseYDouble() {
330
		if (y != PrecisionGeometry.doubleToInteger(preciseY)) {
331
			preciseY = y;
332
		}
333
	}
334

    
335
}
(8-8/17)