|
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 |
} |