forked from nikropht/FreeRouting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRationalVector.java
More file actions
336 lines (298 loc) · 8.73 KB
/
Copy pathRationalVector.java
File metadata and controls
336 lines (298 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright (C) 2014 Alfons Wirtz
* website www.freerouting.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License at <http://www.gnu.org/licenses/>
* for more details.
*
* RationalVector.java
*
* Created on 1. Februar 2003, 09:16
*/
package geometry.planar;
import java.math.BigInteger;
import datastructures.BigIntAux;
import datastructures.Signum;
/**
*
* Analog RationalPoint, but implementing the functionality
* of a Vector instead of the functionality of a Point.
*
* @author Alfons Wirtz
*/
public class RationalVector extends Vector implements java.io.Serializable
{
/**
* creates a RetionalVector from 3 BigIntegers p_x, p_y and p_z.
* They represent the 2-dimensional Vector with the
* rational number Tuple ( p_x / p_z , p_y / p_z).
*/
public RationalVector(BigInteger p_x, BigInteger p_y, BigInteger p_z)
{
if (p_z.signum() >= 0)
{
x = p_x;
y = p_y;
z = p_z;
}
else
{
x = p_x.negate();
y = p_y.negate();
z = p_z.negate();
}
}
/**
* creates a RetionalVector from an IntVector
*/
RationalVector(IntVector p_vector)
{
x = BigInteger.valueOf(p_vector.x);
y = BigInteger.valueOf(p_vector.y);
z = BigInteger.ONE;
}
/**
* returns true, if the x and y coordinates of this vector are 0
*/
public final boolean is_zero()
{
return x.signum() == 0 && y.signum() == 0;
}
/**
* returns true, if this RationalVector is equal to p_ob
*/
public final boolean equals( Object p_ob )
{
if ( this == p_ob )
{
return true;
}
if ( p_ob == null )
{
return false;
}
if ( getClass() != p_ob.getClass() )
{
return false ;
}
RationalPoint other = (RationalPoint)p_ob;
BigInteger det = BigIntAux.determinant(x, other.x, z, other.z);
if (det.signum() != 0)
{
return false;
}
det = BigIntAux.determinant(y, other.y, z, other.z);
return (det.signum() == 0);
}
/**
* returns the Vector such that this plus this.minus() is zero
*/
public Vector negate()
{
return new RationalVector(x.negate(), y.negate(), z);
}
/**
* adds p_other to this vector
*/
public final Vector add( Vector p_other)
{
return p_other.add(this);
}
/**
* Let L be the line from the Zero Vector to p_other.
* The function returns
* Side.ON_THE_LEFT, if this Vector is on the left of L
* Side.ON_THE_RIGHT, if this Vector is on the right of L
* and Side.COLLINEAR, if this Vector is collinear with L.
*/
public Side side_of(Vector p_other)
{
Side tmp = p_other.side_of(this);
return tmp.negate();
}
public boolean is_orthogonal()
{
return (x.signum() == 0 || y.signum() == 0);
}
public boolean is_diagonal()
{
return x.abs().equals(y.abs());
}
/**
* The function returns
* Signum.POSITIVE, if the scalar product of this vector and p_other > 0,
* Signum.NEGATIVE, if the scalar product is < 0,
* and Signum.ZERO, if the scalar product is equal 0.
*/
public Signum projection(Vector p_other)
{
return p_other.projection(this);
}
/**
* calculates the scalar product of this vector and p_other
*/
public double scalar_product(Vector p_other)
{
return p_other.scalar_product(this);
}
/**
* approximates the coordinates of this vector by float coordinates
*/
public FloatPoint to_float()
{
double xd = x.doubleValue();
double yd = y.doubleValue();
double zd = z.doubleValue();
return new FloatPoint( xd / zd, yd / zd);
}
public Vector change_length_approx(double p_lenght)
{
System.out.println("RationalVector: change_length_approx not yet implemented");
return this;
}
public Vector turn_90_degree(int p_factor)
{
int n = p_factor;
while (n < 0)
{
n += 4;
}
while (n >= 4)
{
n -= 4;
}
BigInteger new_x ;
BigInteger new_y ;
switch (n)
{
case 0: // 0 degree
new_x = x;
new_y = y;
break;
case 1: // 90 degree
new_x = y.negate();
new_y = x ;
break;
case 2: // 180 degree
new_x = x.negate() ;
new_y = y.negate() ;
break;
case 3: // 270 degree
new_x = y ;
new_y = x.negate() ;
break;
default:
return this;
}
return new RationalVector(new_x, new_y, this.z);
}
public Vector mirror_at_y_axis()
{
return new RationalVector(this.x.negate(), this.y, this.z);
}
public Vector mirror_at_x_axis()
{
return new RationalVector(this.x, this.y.negate(), this.z);
}
Direction to_normalized_direction()
{
BigInteger dx = x;
BigInteger dy = y;
BigInteger gcd = dx.gcd(y);
dx = dx.divide(gcd);
dy = dy.divide(gcd);
if ( (dx.abs()).compareTo(Limits.CRIT_INT_BIG) <= 0 &&
(dy.abs()).compareTo(Limits.CRIT_INT_BIG) <= 0 )
{
return new IntDirection(dx.intValue(), dy.intValue());
}
return new BigIntDirection(dx, dy);
}
double scalar_product(IntVector p_other)
{
Vector other = new RationalVector(p_other);
return other.scalar_product(this);
}
double scalar_product(RationalVector p_other)
{
FloatPoint v1 = to_float();
FloatPoint v2 = p_other.to_float();
return v1.x * v2.x + v1.y * v2.y;
}
Signum projection(IntVector p_other)
{
Vector other = new RationalVector(p_other);
return other.projection(this);
}
Signum projection(RationalVector p_other)
{
BigInteger tmp1 = x.multiply(p_other.x);
BigInteger tmp2 = y.multiply(p_other.y);
BigInteger tmp3 = tmp1.add(tmp2);
int result = tmp3.signum();
return Signum.of(result);
}
final Vector add(IntVector p_other)
{
RationalVector other = new RationalVector(p_other);
return add(other);
}
final Vector add(RationalVector p_other)
{
BigInteger v1[] = new BigInteger[3];
v1[0] = x;
v1[1] = y;
v1[2] = z;
BigInteger v2[] = new BigInteger[3];
v2[0] = p_other.x;
v2[1] = p_other.y;
v2[2] = p_other.z;
BigInteger[] result = BigIntAux.add_rational_coordinates(v1, v2);
return new RationalVector(result[0], result[1], result[2]);
}
Point add_to(IntPoint p_point)
{
BigInteger new_x = z.multiply(BigInteger.valueOf(p_point.x));
new_x = new_x.add(x);
BigInteger new_y = z.multiply(BigInteger.valueOf(p_point.y));
new_y = new_y.add(y);
return new RationalPoint(new_x, new_y, z);
}
Point add_to(RationalPoint p_point)
{
BigInteger v1[] = new BigInteger[3];
v1[0] = x;
v1[1] = y;
v1[2] = z;
BigInteger v2[] = new BigInteger[3];
v2[0] = p_point.x;
v2[1] = p_point.y;
v2[2] = p_point.z;
BigInteger[] result = BigIntAux.add_rational_coordinates(v1, v2);
return new RationalPoint(result[0], result[1], result[2]);
}
Side side_of(IntVector p_other)
{
RationalVector other = new RationalVector(p_other);
return side_of(other);
}
Side side_of(RationalVector p_other)
{
BigInteger tmp_1 = y.multiply(p_other.x);
BigInteger tmp_2 = x.multiply(p_other.y);
BigInteger determinant = tmp_1.subtract(tmp_2);
int signum = determinant.signum();
return Side.of(signum);
}
public final BigInteger x;
public final BigInteger y;
public final BigInteger z;
}