Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 310397 | Differences between
and this patch

Collapse All | Expand All

(-)META-INF/MANIFEST.MF (-1 / +2 lines)
Lines 7-14 Link Here
7
Bundle-Localization: plugin
7
Bundle-Localization: plugin
8
Import-Package: com.ibm.icu.text;version="[3.8.1,5.0.0)"
8
Import-Package: com.ibm.icu.text;version="[3.8.1,5.0.0)"
9
Export-Package: org.eclipse.draw2d,
9
Export-Package: org.eclipse.draw2d,
10
 org.eclipse.draw2d.graph,
11
 org.eclipse.draw2d.geometry,
10
 org.eclipse.draw2d.geometry,
11
 org.eclipse.draw2d.geometry.euclidean,
12
 org.eclipse.draw2d.graph,
12
 org.eclipse.draw2d.images,
13
 org.eclipse.draw2d.images,
13
 org.eclipse.draw2d.internal;x-internal:=true,
14
 org.eclipse.draw2d.internal;x-internal:=true,
14
 org.eclipse.draw2d.parts,
15
 org.eclipse.draw2d.parts,
(-)src/org/eclipse/draw2d/geometry/Ray.java (-204 / +193 lines)
Lines 5-237 Link Here
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *     Research Group Software Construction,
11
 *     RWTH Aachen University, Germany - Contribution for Bugzilla 245182
12
 *******************************************************************************/
8
 *******************************************************************************/
13
package org.eclipse.draw2d.geometry;
9
package org.eclipse.draw2d.geometry;
14
10
15
/**
11
/**
16
 * Represents a 2-dimensional directional Vector, or Ray. {@link java.util.Vector} is 
12
 * Represents a 2-dimensional directional Vector, or Ray.
17
 * commonly imported, so the name Ray was chosen.
13
 * {@link java.util.Vector} is commonly imported, so the name Ray was chosen.
18
 */
19
public final class Ray {
20
21
/** the X value */
22
public int x;
23
/** the Y value*/
24
public int y;
25
26
/**
27
 * Constructs a Ray <0, 0> with no direction and magnitude.
28
 * @since 2.0
29
 */
30
public Ray() { }
31
32
/**
33
 * Constructs a Ray pointed in the specified direction.
34
 * 
14
 * 
35
 * @param x  X value.
15
 * @deprecated Use {@link Vector} instead, which offers double precision instead
36
 * @param y  Y value.
16
 *             of integer precision.
37
 * @since 2.0
38
 */
39
public Ray(int x, int y) {
40
	this.x = x;
41
	this.y = y;
42
}
43
44
/**
45
 * Constructs a Ray pointed in the direction specified by a Point.
46
 * @param p the Point
47
 * @since 2.0
48
 */
49
public Ray(Point p) {
50
	x = p.x; y = p.y;
51
}
52
53
/**
54
 * Constructs a Ray representing the direction and magnitude between to provided Points.
55
 * @param start Strarting Point
56
 * @param end End Point
57
 * @since 2.0
58
 */
17
 */
59
public Ray(Point start, Point end) {
18
public final class Ray {
60
	x = end.x - start.x;
61
	y = end.y - start.y;
62
}
63
19
64
/**
20
	/** the X value */
65
 * Constructs a Ray representing the difference between two provided Rays.
21
	public int x;
66
 * @param start  The start Ray
22
	/** the Y value */
67
 * @param end   The end Ray
23
	public int y;
68
 * @since 2.0
24
69
 */
25
	/**
70
public Ray(Ray start, Ray end) {
26
	 * Constructs a Ray <0, 0> with no direction and magnitude.
71
	x = end.x - start.x;
27
	 * 
72
	y = end.y - start.y;
28
	 * @since 2.0
73
}
29
	 */
30
	public Ray() {
31
	}
74
32
75
/**
33
	/**
76
 * Calculates the magnitude of the cross product of this Ray with another.
34
	 * Constructs a Ray pointed in the specified direction.
77
 * Represents the amount by which two Rays are directionally different.
35
	 * 
78
 * Parallel Rays return a value of 0.
36
	 * @param x
79
 * @param r  Ray being compared
37
	 *            X value.
80
 * @return  The assimilarity
38
	 * @param y
81
 * @see #similarity(Ray)
39
	 *            Y value.
82
 * @since 2.0
40
	 * @since 2.0
83
 */
41
	 */
84
public int assimilarity(Ray r) {
42
	public Ray(int x, int y) {
85
	return Math.abs(x * r.y - y * r.x);
43
		this.x = x;
86
}
44
		this.y = y;
45
	}
87
46
88
/**
47
	/**
89
 * Calculates the dot product of this Ray with another.
48
	 * Constructs a Ray pointed in the direction specified by a Point.
90
 * @param r the Ray used to perform the dot product
49
	 * 
91
 * @return The dot product
50
	 * @param p
92
 * @since 2.0
51
	 *            the Point
93
 */
52
	 * @since 2.0
94
public int dotProduct(Ray r) {
53
	 */
95
	return x * r.x + y * r.y;
54
	public Ray(Point p) {
96
}
55
		x = p.x;
56
		y = p.y;
57
	}
97
58
98
/**
59
	/**
99
 * Calculates the dot product of this Ray with another.
60
	 * Constructs a Ray representing the direction and magnitude between to
100
 * @param r the Ray used to perform the dot product
61
	 * provided Points.
101
 * @return The dot product as <code>long</code> to avoid possible integer overflow
62
	 * 
102
 * @since 3.4.1
63
	 * @param start
103
 */
64
	 *            Strarting Point
104
long dotProductL(Ray r) {
65
	 * @param end
105
	return (long) x * r.x + (long) y * r.y;
66
	 *            End Point
106
}
67
	 * @since 2.0
68
	 */
69
	public Ray(Point start, Point end) {
70
		x = end.x - start.x;
71
		y = end.y - start.y;
72
	}
107
73
108
/**
74
	/**
109
 * @see java.lang.Object#equals(Object)
75
	 * Constructs a Ray representing the difference between two provided Rays.
110
 */
76
	 * 
111
public boolean equals(Object obj) {
77
	 * @param start
112
	if (obj == this)
78
	 *            The start Ray
113
		return true;
79
	 * @param end
114
	if (obj instanceof Ray) {
80
	 *            The end Ray
115
		Ray r = (Ray)obj;
81
	 * @since 2.0
116
		return x == r.x && y == r.y;
82
	 */
83
	public Ray(Ray start, Ray end) {
84
		x = end.x - start.x;
85
		y = end.y - start.y;
117
	}
86
	}
118
	return false;
119
}
120
87
121
/**
88
	/**
122
 * Creates a new Ray which is the sum of this Ray with another.
89
	 * Calculates the magnitude of the cross product of this Ray with another.
123
 * @param r  Ray to be added with this Ray
90
	 * Represents the amount by which two Rays are directionally different.
124
 * @return  a new Ray
91
	 * Parallel Rays return a value of 0.
125
 * @since 2.0
92
	 * 
126
 */
93
	 * @param r
127
public Ray getAdded(Ray r) {
94
	 *            Ray being compared
128
	return new Ray(r.x + x, r.y + y);
95
	 * @return The assimilarity
129
}
96
	 * @see #similarity(Ray)
97
	 * @since 2.0
98
	 */
99
	public int assimilarity(Ray r) {
100
		return Math.abs(x * r.y - y * r.x);
101
	}
130
102
131
/**
103
	/**
132
 * Creates a new Ray which is the difference of this Ray with the provided Ray.
104
	 * Calculates the dot product of this Ray with another.
133
 * 
105
	 * 
134
 * @param q
106
	 * @param r
135
 *            Ray to be subtracted from this Ray
107
	 *            the Ray used to perform the dot product
136
 * @return a new Ray
108
	 * @return The dot product
137
 * @since 3.6
109
	 * @since 2.0
138
 */
110
	 */
139
public Ray getSubtracted(Ray q) {
111
	public int dotProduct(Ray r) {
140
	return new Ray(x - q.x, y - q.y);
112
		return x * r.x + y * r.y;
141
}
113
	}
142
114
143
/**
115
	/**
144
 * Returns the angle between this Ray and the provided Ray.
116
	 * Calculates the dot product of this Ray with another.
145
 * @param q Ray to calculate the angle. 
117
	 * 
146
 * @return the angle between the two Rays.
118
	 * @param r
147
 * @since 3.6
119
	 *            the Ray used to perform the dot product
148
 */
120
	 * @return The dot product as <code>long</code> to avoid possible integer
149
public double getAngle(Ray q) {
121
	 *         overflow
150
	double cosAlpha = dotProduct(q) / (length() * q.length());
122
	 * @since 3.4.1
151
	return Math.acos(cosAlpha);
123
	 */
152
}
124
	long dotProductL(Ray r) {
125
		return (long) x * r.x + (long) y * r.y;
126
	}
153
127
154
/**
128
	/**
155
 * Creates a new Ray which represents the average of this Ray with another.
129
	 * @see java.lang.Object#equals(Object)
156
 * @param r  Ray to calculate the average.
130
	 */
157
 * @return  a new Ray
131
	public boolean equals(Object obj) {
158
 * @since 2.0
132
		if (obj == this)
159
 */
133
			return true;
160
public Ray getAveraged(Ray r) {
134
		if (obj instanceof Ray) {
161
	return new Ray ((x + r.x) / 2, (y + r.y) / 2);
135
			Ray r = (Ray) obj;
162
}
136
			return x == r.x && y == r.y;
137
		}
138
		return false;
139
	}
163
140
164
/**
141
	/**
165
 * Creates a new Ray which represents this Ray scaled by the amount provided.
142
	 * Creates a new Ray which is the sum of this Ray with another.
166
 * @param s  Value providing the amount to scale.
143
	 * 
167
 * @return  a new Ray
144
	 * @param r
168
 * @since 2.0
145
	 *            Ray to be added with this Ray
169
 */
146
	 * @return a new Ray
170
public Ray getScaled(int s) {
147
	 * @since 2.0
171
	return new Ray(x * s, y * s);
148
	 */
172
}
149
	public Ray getAdded(Ray r) {
150
		return new Ray(r.x + x, r.y + y);
151
	}
173
152
174
/**
153
	/**
175
 * Returns the orthogonal complement of this Ray, which is defined to be
154
	 * Creates a new Ray which represents the average of this Ray with another.
176
 * (-y, x).
155
	 * 
177
 * 
156
	 * @param r
178
 * @return the orthogonal complement of this Ray
157
	 *            Ray to calculate the average.
179
 * @since 3.6
158
	 * @return a new Ray
180
 */
159
	 * @since 2.0
181
public Ray getOrthogonalComplement() {
160
	 */
182
	return new Ray(-y, x);
161
	public Ray getAveraged(Ray r) {
183
}
162
		return new Ray((x + r.x) / 2, (y + r.y) / 2);
163
	}
184
164
185
/**
165
	/**
186
 * @see java.lang.Object#hashCode()
166
	 * Creates a new Ray which represents this Ray scaled by the amount
187
 */
167
	 * provided.
188
public int hashCode() {
168
	 * 
189
	return (x * y) ^ (x + y);
169
	 * @param s
190
}
170
	 *            Value providing the amount to scale.
171
	 * @return a new Ray
172
	 * @since 2.0
173
	 */
174
	public Ray getScaled(int s) {
175
		return new Ray(x * s, y * s);
176
	}
191
177
192
/**
178
	/**
193
 * Returns true if this Ray has a non-zero horizontal comonent.
179
	 * @see java.lang.Object#hashCode()
194
 * @return  true if this Ray has a non-zero horizontal comonent
180
	 */
195
 * @since 2.0
181
	public int hashCode() {
196
 */
182
		return (x * y) ^ (x + y);
197
public boolean isHorizontal() {
183
	}
198
	return x != 0;
199
}
200
184
201
/**
185
	/**
202
 * Returns the length of this Ray.
186
	 * Returns true if this Ray has a non-zero horizontal comonent.
203
 * @return  Length of this Ray
187
	 * 
204
 * @since 2.0
188
	 * @return true if this Ray has a non-zero horizontal comonent
205
 */
189
	 * @since 2.0
206
public double length() {
190
	 */
207
	return Math.sqrt(dotProductL(this));
191
	public boolean isHorizontal() {
208
}
192
		return x != 0;
193
	}
209
194
210
/**
195
	/**
211
 * Calculates the similarity of this Ray with another.
196
	 * Returns the length of this Ray.
212
 * Similarity is defined as the absolute value of the dotProduct()
197
	 * 
213
 * @param r  Ray being tested for similarity
198
	 * @return Length of this Ray
214
 * @return  the Similarity
199
	 * @since 2.0
215
 * @see #assimilarity(Ray)
200
	 */
216
 * @since 2.0
201
	public double length() {
217
 */
202
		return Math.sqrt(dotProductL(this));
218
public int similarity(Ray r) {
203
	}
219
	return Math.abs(dotProduct(r));
220
}
221
204
222
/**
205
	/**
223
 * @return a String representation
206
	 * Calculates the similarity of this Ray with another. Similarity is defined
224
 */
207
	 * as the absolute value of the dotProduct()
225
public String toString() {
208
	 * 
226
	return "(" + x + "," + y + ")";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
209
	 * @param r
227
}
210
	 *            Ray being tested for similarity
211
	 * @return the Similarity
212
	 * @see #assimilarity(Ray)
213
	 * @since 2.0
214
	 */
215
	public int similarity(Ray r) {
216
		return Math.abs(dotProduct(r));
217
	}
228
218
229
/**
219
	/**
230
 * @return A Point representation
220
	 * @return a String representation
231
 * @since 3.6
221
	 */
232
 */
222
	public String toString() {
233
public Point toPoint(){
223
		return "(" + x + "," + y + ")";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
234
	return new Point(x,y);
224
	}
235
}
236
225
237
}
226
}
(-)src/org/eclipse/draw2d/geometry/Straight.java (-180 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
 *     Research Group Software Construction,
10
 *     RWTH Aachen University, Germany - initial API and implementation
11
 */
12
package org.eclipse.draw2d.geometry;
13
14
/**
15
 * Represents a straight line within a 2-dimensional space. Together with Ray,
16
 * this allows computations defined within Euclidean geometry.
17
 * 
18
 * @author Alexander Nyssen
19
 * @since 3.6
20
 */
21
public class Straight {
22
23
	private Ray p; // Ray indicating the position vector of the straight
24
	private Ray a; // Ray representing the direction vector of this straight
25
26
	/**
27
	 * Constructs a new Straight with the given position and direction.
28
	 * 
29
	 * @param position
30
	 * @param direction
31
	 */
32
	public Straight(Ray position, Ray direction) {
33
		this.p = position;
34
		this.a = direction;
35
	}
36
37
	/**
38
	 * Constructs a new Straight between the two given Points.
39
	 * 
40
	 * @param point1
41
	 * @param point2
42
	 */
43
	public Straight(Point point1, Point point2) {
44
		this(new Ray(point1), new Ray(point1, point2));
45
	}
46
47
	/**
48
	 * Returns the position of this Straight.
49
	 * 
50
	 * @return A Ray indicating the position of this Straight.
51
	 */
52
	public Ray getPosition() {
53
		return p;
54
	}
55
56
	/**
57
	 * Returns the direction of this Straight.
58
	 * 
59
	 * @return A Ray indicating the position of this Straight.
60
	 */
61
	public Ray getDirection() {
62
		return a;
63
	}
64
65
	/**
66
	 * Checks whether this Straight and the provided one have an intersection
67
	 * point.
68
	 * 
69
	 * @param g2
70
	 *            The Straight to use for calculations. It may not be equal to
71
	 *            this Straight.
72
	 * @return true if the two Straights intersect, false otherwise.
73
	 */
74
	public boolean intersects(Straight g2) {
75
		// check if there is an intersection point
76
		return a.dotProduct(g2.getDirection().getOrthogonalComplement()) != 0;
77
	}
78
79
	/**
80
	 * Computes the intersection point of this Straight and the provided one, if it exists.
81
	 * 
82
	 * @param g2
83
	 *            The Straight to use for calculations. It may not be equal to
84
	 *            this Straight.
85
	 * @return A Ray pointing to the intersection point, if it exists, null
86
	 *         if no intersection point exists (or the Straights are equal).
87
	 */
88
	public Ray getIntersection(Straight g2) {
89
		if (!intersects(g2)) {
90
			return null;
91
		}
92
93
		// retrieve position and direction Rays of other straight
94
		Ray q = g2.getPosition();
95
		Ray b = g2.getDirection();
96
97
		// retrieve orthogonal complements needed during computation
98
		Ray aOC = a.getOrthogonalComplement(); // orthogonal complement of a
99
		Ray bOC = b.getOrthogonalComplement(); // orthogonal complement of b
100
101
		// compute intersection point
102
		int[] intersection = new int[2];
103
		intersection[0] = (q.dotProduct(bOC) * a.x - p.dotProduct(aOC) * b.x)
104
				/ a.dotProduct(bOC);
105
		intersection[1] = (q.dotProduct(bOC) * a.y - p.dotProduct(aOC) * b.y)
106
				/ a.dotProduct(bOC);
107
		return new Ray(intersection[0], intersection[1]);
108
	}
109
110
	/**
111
	 * Returns the (smallest) angle between this Ray and the provided one.
112
	 * 
113
	 * @param g2
114
	 *            The Ray to be used for calculations.
115
	 * @return The angle spanned between the two Rays.
116
	 */
117
	public double getAngle(Straight g2) {
118
		return a.getAngle(g2.getDirection());
119
	}
120
121
	/**
122
	 * Returns the projection of the given Ray onto this Straight, which is the
123
	 * point on this Straight with the minimal distance to the point, denoted by
124
	 * the provided Ray.
125
	 * 
126
	 * @param q
127
	 *            The Ray whose projection should be determined.
128
	 * @return A new Ray representing the projection of the provided Ray onto
129
	 *         this Straight.
130
	 */
131
	public Ray getProjection(Ray q) {
132
		Ray s = getIntersection(new Straight(q, a.getOrthogonalComplement()));
133
		return s;
134
	}
135
136
	/**
137
	 * Returns the distance of the provided Ray to this Straight, which is the
138
	 * distance between the provided Ray and its projection onto this Straight.
139
	 * 
140
	 * @param q
141
	 *            The Ray whose distance is to be calculated.
142
	 * @return the distance between this Straight and the provided Ray.
143
	 */
144
	public double getDistance(Ray q) {
145
		Ray s = getProjection(q);
146
		return s.getSubtracted(q).length();
147
	}
148
149
	/**
150
	 * Calculates wether the point indicated by the provided Ray is a point on
151
	 * this Straight.
152
	 * 
153
	 * @param q
154
	 *            the Ray who has to be checked.
155
	 * @return true if the position indicated by the given Ray is a point of
156
	 *         this Straight, false otherwise.
157
	 */
158
	public boolean contains(Ray q) {
159
		return getDistance(q) == 0.0;
160
	}
161
162
	/**
163
	 * Checks whether this Straight is equal to the provided Straight. Two
164
	 * Straights s1 and s2 are equal, if the position vector of s2 is a point on
165
	 * s1 and the direction vectors of s1 and s2 are parallel.
166
	 * 
167
	 * @see java.lang.Object#equals(java.lang.Object)
168
	 */
169
	public boolean equals(Object other) {
170
		if (!(other instanceof Straight)) {
171
			return false;
172
		} else {
173
			Straight otherLine = (Straight) other;
174
			return contains(otherLine.getPosition())
175
					&& a.getOrthogonalComplement().dotProduct(
176
							otherLine.getDirection()) == 0;
177
		}
178
	}
179
180
}
(-)src/org/eclipse/draw2d/geometry/euclidean/Straight.java (+187 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
 *     Research Group Software Construction,
10
 *     RWTH Aachen University, Germany - initial API and implementation
11
 */
12
package org.eclipse.draw2d.geometry.euclidean;
13
14
import org.eclipse.draw2d.geometry.PrecisionPoint;
15
16
/**
17
 * Represents a straight line within a 2-dimensional space. Together with
18
 * Vector, this allows computations defined within Euclidean geometry.
19
 * 
20
 * @author Alexander Nyssen
21
 * @since 3.6
22
 */
23
public class Straight {
24
25
	private Vector p; // Vector indicating the position vector of the straight
26
	private Vector a; // Vector representing the direction vector of this
27
						// straight
28
29
	/**
30
	 * Constructs a new Straight with the given position and direction.
31
	 * 
32
	 * @param position
33
	 * @param direction
34
	 */
35
	public Straight(Vector position, Vector direction) {
36
		this.p = position;
37
		this.a = direction;
38
	}
39
40
	/**
41
	 * Constructs a new Straight between the two given Points.
42
	 * 
43
	 * @param point1
44
	 *            the first waypoint
45
	 * @param point2
46
	 *            the second waypoint
47
	 */
48
	public Straight(PrecisionPoint point1, PrecisionPoint point2) {
49
		this(new Vector(point1), new Vector(point1, point2));
50
	}
51
52
	/**
53
	 * Returns the position of this Straight.
54
	 * 
55
	 * @return A Vector indicating the position of this Straight.
56
	 */
57
	public Vector getPosition() {
58
		return p;
59
	}
60
61
	/**
62
	 * Returns the direction of this Straight.
63
	 * 
64
	 * @return A Vector indicating the position of this Straight.
65
	 */
66
	public Vector getDirection() {
67
		return a;
68
	}
69
70
	/**
71
	 * Checks whether this Straight and the provided one have an intersection
72
	 * point.
73
	 * 
74
	 * @param s2
75
	 *            The Straight to use for calculations. It may not be equal to
76
	 *            this Straight.
77
	 * @return true if the two Straights intersect, false otherwise.
78
	 */
79
	public boolean intersects(Straight s2) {
80
		// check if there is an intersection point
81
		return a.dotProduct(s2.getDirection().getOrthogonalComplement()) != 0;
82
	}
83
84
	/**
85
	 * Computes the intersection point of this Straight and the provided one, if
86
	 * it exists.
87
	 * 
88
	 * @param s2
89
	 *            The Straight to use for calculations. It may not be equal to
90
	 *            this Straight.
91
	 * @return A Vector pointing to the intersection point, if it exists, null
92
	 *         if no intersection point exists (or the Straights are equal).
93
	 */
94
	public Vector getIntersection(Straight s2) {
95
		if (!intersects(s2)) {
96
			return null;
97
		}
98
99
		// retrieve position and direction Vectors of other straight
100
		Vector q = s2.getPosition();
101
		Vector b = s2.getDirection();
102
103
		// retrieve orthogonal complements needed during computation
104
		Vector aOC = a.getOrthogonalComplement(); // orthogonal complement of a
105
		Vector bOC = b.getOrthogonalComplement(); // orthogonal complement of b
106
107
		// compute intersection point
108
		double[] intersection = new double[2];
109
		intersection[0] = (q.dotProduct(bOC) * a.x - p.dotProduct(aOC) * b.x)
110
				/ a.dotProduct(bOC);
111
		intersection[1] = (q.dotProduct(bOC) * a.y - p.dotProduct(aOC) * b.y)
112
				/ a.dotProduct(bOC);
113
		return new Vector(intersection[0], intersection[1]);
114
	}
115
116
	/**
117
	 * Returns the (smallest) angle between this Straight and the provided one.
118
	 * 
119
	 * @param s2
120
	 *            The Straight to be used for calculations.
121
	 * @return The angle spanned between the two Straights.
122
	 */
123
	public double getAngle(Straight s2) {
124
		return a.getAngle(s2.getDirection());
125
	}
126
127
	/**
128
	 * Returns the projection of the given Vector onto this Straight, which is
129
	 * the point on this Straight with the minimal distance to the point,
130
	 * denoted by the provided Vector.
131
	 * 
132
	 * @param q
133
	 *            The Vector whose projection should be determined.
134
	 * @return A new Vector representing the projection of the provided Vector
135
	 *         onto this Straight.
136
	 */
137
	public Vector getProjection(Vector q) {
138
		Vector s = getIntersection(new Straight(q, a.getOrthogonalComplement()));
139
		return s;
140
	}
141
142
	/**
143
	 * Returns the distance of the provided Vector to this Straight, which is
144
	 * the distance between the provided Vector and its projection onto this
145
	 * Straight.
146
	 * 
147
	 * @param q
148
	 *            The Vector whose distance is to be calculated.
149
	 * @return the distance between this Straight and the provided Vector.
150
	 */
151
	public double getDistance(Vector q) {
152
		Vector s = getProjection(q);
153
		return s.getSubtracted(q).length();
154
	}
155
156
	/**
157
	 * Calculates whether the point indicated by the provided Vector is a point
158
	 * on this Straight.
159
	 * 
160
	 * @param q
161
	 *            the Vector who has to be checked.
162
	 * @return true if the position indicated by the given Vector is a point of
163
	 *         this Straight, false otherwise.
164
	 */
165
	public boolean contains(Vector q) {
166
		return getDistance(q) == 0.0;
167
	}
168
169
	/**
170
	 * Checks whether this Straight is equal to the provided Straight. Two
171
	 * straights s1 and s2 are equal, if the position vector of s2 is a point on
172
	 * s1 and the direction vectors of s1 and s2 are parallel.
173
	 * 
174
	 * @see java.lang.Object#equals(java.lang.Object)
175
	 */
176
	public boolean equals(Object other) {
177
		if (!(other instanceof Straight)) {
178
			return false;
179
		} else {
180
			Straight otherLine = (Straight) other;
181
			return contains(otherLine.getPosition())
182
					&& a.getOrthogonalComplement().dotProduct(
183
							otherLine.getDirection()) == 0;
184
		}
185
	}
186
187
}
(-)src/org/eclipse/draw2d/geometry/euclidean/Vector.java (+249 lines)
Added Link Here
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
 *     Research Group Software Construction,
11
 *     RWTH Aachen University, Germany - Contribution for Bugzilla 245182
12
 *
13
 *******************************************************************************/
14
package org.eclipse.draw2d.geometry.euclidean;
15
16
import org.eclipse.draw2d.geometry.PrecisionPoint;
17
import org.eclipse.draw2d.geometry.Ray;
18
19
/**
20
 * Represents a 2-dimensional vector as of Euclidean geometry. This class was created based
21
 * on {@link Ray}, and is meant as a replacement, using double precision.
22
 * 
23
 * @author Alexander Nyssen
24
 * @since 3.6
25
 */
26
public final class Vector {
27
28
	/** the X value */
29
	public double x;
30
	/** the Y value */
31
	public double y;
32
33
	/**
34
	 * Constructs a Vector with no direction and magnitude.
35
	 * 
36
	 */
37
	public Vector() {
38
	}
39
40
	/**
41
	 * Constructs a Ray pointed in the specified direction.
42
	 * 
43
	 * @param x
44
	 *            X value.
45
	 * @param y
46
	 *            Y value.
47
	 */
48
	public Vector(double x, double y) {
49
		this.x = x;
50
		this.y = y;
51
	}
52
53
	/**
54
	 * Constructs a Ray pointed in the direction specified by a Point.
55
	 * 
56
	 * @param p
57
	 *            the point
58
	 */
59
	public Vector(PrecisionPoint p) {
60
		x = p.preciseX;
61
		y = p.preciseY;
62
	}
63
64
	/**
65
	 * Constructs a Ray representing the direction and magnitude between to
66
	 * provided Points.
67
	 * 
68
	 * @param start
69
	 *            starting point
70
	 * @param end
71
	 *            End Point
72
	 */
73
	public Vector(PrecisionPoint start, PrecisionPoint end) {
74
		x = end.preciseX - start.preciseX;
75
		y = end.preciseY - start.preciseY;
76
	}
77
78
	/**
79
	 * Constructs a Ray representing the difference between two provided Rays.
80
	 * 
81
	 * @param start
82
	 *            The start Ray
83
	 * @param end
84
	 *            The end Ray
85
	 */
86
	public Vector(Vector start, Vector end) {
87
		x = end.x - start.x;
88
		y = end.y - start.y;
89
	}
90
91
	/**
92
	 * Calculates the magnitude of the cross product of this Ray with another.
93
	 * Represents the amount by which two Rays are directionally different.
94
	 * Parallel Rays return a value of 0.
95
	 * 
96
	 * @param r
97
	 *            Ray being compared
98
	 * @return The assimilarity
99
	 * @see #similarity(Vector)
100
	 */
101
	public double assimilarity(Vector r) {
102
		return Math.abs(x * r.y - y * r.x);
103
	}
104
105
	/**
106
	 * Calculates the dot product of this Ray with another.
107
	 * 
108
	 * @param r
109
	 *            the Ray used to perform the dot product
110
	 * @return The dot product
111
	 */
112
	public double dotProduct(Vector r) {
113
		return x * r.x + y * r.y;
114
	}
115
116
	/**
117
	 * @see java.lang.Object#equals(Object)
118
	 */
119
	public boolean equals(Object obj) {
120
		if (obj == this)
121
			return true;
122
		if (obj instanceof Vector) {
123
			Vector r = (Vector) obj;
124
			return x == r.x && y == r.y;
125
		}
126
		return false;
127
	}
128
129
	/**
130
	 * Creates a new Ray which is the sum of this Ray with another.
131
	 * 
132
	 * @param r
133
	 *            Ray to be added with this Ray
134
	 * @return a new Ray
135
	 */
136
	public Vector getAdded(Vector r) {
137
		return new Vector(r.x + x, r.y + y);
138
	}
139
140
	/**
141
	 * Creates a new Ray which is the difference of this Ray with the provided
142
	 * Ray.
143
	 * 
144
	 * @param q
145
	 *            Ray to be subtracted from this Ray
146
	 * @return a new Ray
147
	 */
148
	public Vector getSubtracted(Vector q) {
149
		return new Vector(x - q.x, y - q.y);
150
	}
151
152
	/**
153
	 * Returns the angle between this Ray and the provided Ray.
154
	 * 
155
	 * @param q
156
	 *            Ray to calculate the angle.
157
	 * @return the angle between the two Rays.
158
	 */
159
	public double getAngle(Vector q) {
160
		double cosAlpha = dotProduct(q) / (length() * q.length());
161
		return Math.acos(cosAlpha);
162
	}
163
164
	/**
165
	 * Creates a new Ray which represents the average of this Ray with another.
166
	 * 
167
	 * @param r
168
	 *            Ray to calculate the average.
169
	 * @return a new Ray
170
	 */
171
	public Vector getAveraged(Vector r) {
172
		return new Vector((x + r.x) / 2, (y + r.y) / 2);
173
	}
174
175
	/**
176
	 * Creates a new Ray which represents this Ray scaled by the amount
177
	 * provided.
178
	 * 
179
	 * @param s
180
	 *            Value providing the amount to scale.
181
	 * @return a new Ray
182
	 */
183
	public Vector getScaled(int s) {
184
		return new Vector(x * s, y * s);
185
	}
186
187
	/**
188
	 * Returns the orthogonal complement of this Ray, which is defined to be
189
	 * (-y, x).
190
	 * 
191
	 * @return the orthogonal complement of this Ray
192
	 */
193
	public Vector getOrthogonalComplement() {
194
		return new Vector(-y, x);
195
	}
196
197
	/**
198
	 * @see java.lang.Object#hashCode()
199
	 */
200
	public int hashCode() {
201
		return (int) (x * y) ^ (int) (x + y);
202
	}
203
204
	/**
205
	 * Returns true if this Ray has a non-zero horizontal comonent.
206
	 * 
207
	 * @return true if this Ray has a non-zero horizontal comonent
208
	 */
209
	public boolean isHorizontal() {
210
		return x != 0;
211
	}
212
213
	/**
214
	 * Returns the length of this Ray.
215
	 * 
216
	 * @return Length of this Ray
217
	 */
218
	public double length() {
219
		return Math.sqrt(dotProduct(this));
220
	}
221
222
	/**
223
	 * Calculates the similarity of this Ray with another. Similarity is defined
224
	 * as the absolute value of the dotProduct()
225
	 * 
226
	 * @param r
227
	 *            Ray being tested for similarity
228
	 * @return the Similarity
229
	 * @see #assimilarity(Vector)
230
	 */
231
	public double similarity(Vector r) {
232
		return Math.abs(dotProduct(r));
233
	}
234
235
	/**
236
	 * @return a String representation
237
	 */
238
	public String toString() {
239
		return "(" + x + "," + y + ")";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
240
	}
241
242
	/**
243
	 * @return a PrecisionPoint representation
244
	 */
245
	public PrecisionPoint toPoint() {
246
		return new PrecisionPoint(x, y);
247
	}
248
249
}
(-)src/org/eclipse/draw2d/test/Draw2dTestSuite.java (+1 lines)
Lines 51-56 Link Here
51
		addTest(new TestSuite(RectangleTest.class));
51
		addTest(new TestSuite(RectangleTest.class));
52
		// addTest(new TestSuite(ColorConstantTest.class));
52
		// addTest(new TestSuite(ColorConstantTest.class));
53
		addTest(new TestSuite(RayTest.class));
53
		addTest(new TestSuite(RayTest.class));
54
		addTest(new TestSuite(VectorTest.class));
54
		addTest(new TestSuite(StraightTest.class));
55
		addTest(new TestSuite(StraightTest.class));
55
		addTest(new TestSuite(RelativeBendpointTest.class));
56
		addTest(new TestSuite(RelativeBendpointTest.class));
56
		addTest(new TestSuite(GeometryTest.class));
57
		addTest(new TestSuite(GeometryTest.class));
(-)src/org/eclipse/draw2d/test/RayTest.java (-5 lines)
Lines 40-50 Link Here
40
		testLengthValues(3, 4, 5);
40
		testLengthValues(3, 4, 5);
41
		testLengthValues(0, Integer.MAX_VALUE, Integer.MAX_VALUE);
41
		testLengthValues(0, Integer.MAX_VALUE, Integer.MAX_VALUE);
42
	}
42
	}
43
		
44
	public void test_getOrthoComplement() {
45
		Ray a = new Ray(3, -5);
46
		assertTrue(a.getOrthogonalComplement().equals(new Ray(5, 3)));
47
	}
48
43
49
	public void test_getScalarProduct() {
44
	public void test_getScalarProduct() {
50
		Ray a = new Ray(3, 2);
45
		Ray a = new Ray(3, 2);
(-)src/org/eclipse/draw2d/test/StraightTest.java (-9 / +21 lines)
Lines 13-20 Link Here
13
13
14
import junit.framework.TestCase;
14
import junit.framework.TestCase;
15
15
16
import org.eclipse.draw2d.geometry.Ray;
16
import org.eclipse.draw2d.geometry.PrecisionPoint;
17
import org.eclipse.draw2d.geometry.Straight;
17
import org.eclipse.draw2d.geometry.euclidean.Straight;
18
import org.eclipse.draw2d.geometry.euclidean.Vector;
18
19
19
/**
20
/**
20
 * @author Alexander Nyssen
21
 * @author Alexander Nyssen
Lines 23-38 Link Here
23
public class StraightTest extends TestCase {
24
public class StraightTest extends TestCase {
24
25
25
	public void test_getIntersection() {
26
	public void test_getIntersection() {
26
		Ray p = new Ray(1, 1);
27
		// test integer precision
27
		Ray a = new Ray(2, 1);
28
		Vector p = new Vector(1, 1);
28
		Ray q = new Ray(1, 4);
29
		Vector a = new Vector(2, 1);
29
		Ray b = new Ray(1, -1);
30
		Vector q = new Vector(1, 4);
31
		Vector b = new Vector(1, -1);
30
32
31
		Ray intersection = new Straight(p, a)
33
		Vector intersection = new Straight(p, a).getIntersection(new Straight(
32
				.getIntersection(new Straight(q, b));
34
				q, b));
33
35
34
		assertTrue(intersection.equals(new Ray(3, 2)));
36
		assertTrue(intersection.equals(new Vector(3, 2)));
35
37
38
		// test double precision
39
		p = new Vector(0, 0);
40
		a = new Vector(new PrecisionPoint(0, 0), new PrecisionPoint(5, 5));
41
		q = new Vector(0, 5);
42
		b = new Vector(new PrecisionPoint(0, 5), new PrecisionPoint(5, 0));
43
44
		intersection = new Straight(p, a).getIntersection(new Straight(q, b));
45
		assertTrue(intersection.equals(new Vector(2.5, 2.5)));
46
47
		// check straight does not intersect itself
36
		assertNull(new Straight(p, a).getIntersection(new Straight(p, a)));
48
		assertNull(new Straight(p, a).getIntersection(new Straight(p, a)));
37
	}
49
	}
38
}
50
}
(-)src/org/eclipse/draw2d/test/VectorTest.java (+60 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008, 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.test;
12
13
import junit.framework.TestCase;
14
15
import org.eclipse.draw2d.geometry.euclidean.Vector;
16
17
/**
18
 * Vector's tests
19
 * 
20
 * @author aboyko
21
 *
22
 */
23
public class VectorTest extends TestCase {
24
25
	/**
26
	 * @see TestCase#setUp()
27
	 */
28
	protected void setUp() throws Exception {
29
		super.setUp();
30
	}
31
32
	/**
33
	 * @see TestCase#tearDown()
34
	 */
35
	protected void tearDown() throws Exception {
36
		super.tearDown();
37
	}
38
39
	public void test_length() {
40
		testLengthValues(3, 4, 5);
41
		testLengthValues(0, Integer.MAX_VALUE, Integer.MAX_VALUE);
42
	}
43
		
44
	public void test_getOrthoComplement() {
45
		Vector a = new Vector(3, -5);
46
		assertTrue(a.getOrthogonalComplement().equals(new Vector(5, 3)));
47
	}
48
49
	public void test_getScalarProduct() {
50
		Vector a = new Vector(3, 2);
51
		Vector b = new Vector(2, -2);
52
		assertTrue(a.dotProduct(b) == 2);
53
	}
54
	
55
	private void testLengthValues(int x, int y, double expectedLength) {
56
		Vector Vector = new Vector(x, y);
57
		assertEquals(expectedLength, Vector.length(), 0);
58
	}
59
60
}

Return to bug 310397