gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.util / org.gvsig.tools.util.impl / src / main / java / org / gvsig / euclidean / EuclideanLine2DImpl.java @ 2306
History | View | Annotate | Download (7.23 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.euclidean; |
7 |
|
8 |
import java.awt.geom.Point2D; |
9 |
|
10 |
/**
|
11 |
*
|
12 |
* @author fdiaz
|
13 |
*/
|
14 |
public class EuclideanLine2DImpl implements EuclideanLine2D { |
15 |
|
16 |
double coefA;
|
17 |
double coefB;
|
18 |
double coefC;
|
19 |
double m;
|
20 |
double b;
|
21 |
|
22 |
public EuclideanLine2DImpl(double coefA, double coefB, double coefC) { |
23 |
|
24 |
this.coefA = coefA;
|
25 |
this.coefB = coefB;
|
26 |
this.coefC = coefC;
|
27 |
|
28 |
reduceCoefs(); |
29 |
|
30 |
this.m = -coefA/coefB;
|
31 |
if(this.m == -0.0){ |
32 |
this.m = 0.0; |
33 |
} |
34 |
this.b = -coefC / coefB;
|
35 |
|
36 |
} |
37 |
|
38 |
public EuclideanLine2DImpl(double m, double b) { |
39 |
|
40 |
this.m = m;
|
41 |
this.b = b;
|
42 |
|
43 |
this.coefA = m;
|
44 |
this.coefB = -1.0; |
45 |
this.coefC = b;
|
46 |
|
47 |
reduceCoefs(); |
48 |
|
49 |
} |
50 |
|
51 |
public EuclideanLine2DImpl(double x0, double y0, double x1, double y1) { |
52 |
this(y1 - y0, -(x1 - x0), -(y1 - y0) * x0 + (x1 - x0) * y0);
|
53 |
} |
54 |
|
55 |
public EuclideanLine2DImpl(Point2D p0, Point2D p1) { |
56 |
this(p0.getX(), p0.getY(), p1.getX(), p1.getY());
|
57 |
} |
58 |
|
59 |
|
60 |
private void reduceCoefs() { |
61 |
if(coefA != 0 && Double.isFinite(coefA)){ |
62 |
coefC = coefC/coefA; |
63 |
coefB = coefB/coefA; |
64 |
coefA = 1.0;
|
65 |
} else if(coefB != 0 && Double.isFinite(coefB)){ |
66 |
coefA = coefA/coefB; // for change the sign of coefA when coefA is infinite && coefB = -1;
|
67 |
coefC = coefC/coefB; |
68 |
coefB = 1.0;
|
69 |
} |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public double getA() { |
74 |
return coefA;
|
75 |
} |
76 |
|
77 |
@Override
|
78 |
public double getB() { |
79 |
return coefB;
|
80 |
} |
81 |
|
82 |
@Override
|
83 |
public double getC() { |
84 |
return coefC;
|
85 |
} |
86 |
|
87 |
@Override
|
88 |
public double getSlope() { |
89 |
return m;
|
90 |
} |
91 |
|
92 |
@Override
|
93 |
public double getYIntercept() { |
94 |
return b;
|
95 |
} |
96 |
|
97 |
@Override
|
98 |
public double getY(double x) { |
99 |
return (-coefA*x-coefC)/coefB;
|
100 |
} |
101 |
|
102 |
@Override
|
103 |
public double getX(double y) { |
104 |
return (-coefB*y-coefC)/coefA;
|
105 |
} |
106 |
|
107 |
@Override
|
108 |
public double getAngle(EuclideanLine2D line) { |
109 |
double m1 = line.getSlope();
|
110 |
return Math.atan(Math.abs((m1-m)/(1+m1*m))); |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public double getAngle(EuclideanLine2D line, Point2D quadrant) { |
115 |
//FIXME: not implemented yet
|
116 |
double m1 = line.getSlope();
|
117 |
return Math.atan(Math.abs((m1-m)/(1+m1*m))); |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public double getDegreesAngle(EuclideanLine2D line) { |
122 |
return Math.toDegrees(getAngle(line)); |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public double getDegreesAngle(EuclideanLine2D line, Point2D quadrant) { |
127 |
return Math.toDegrees(getAngle(line, quadrant)); |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public double getDistance(double pointX, double pointY) { |
132 |
// d(P,r)=|A*px+B*py+C|/SQRT(A?+B?)
|
133 |
|
134 |
double den = Math.abs(Math.sqrt(Math.pow(coefA, 2) + Math.pow(coefB, 2))); |
135 |
return Math.abs((coefA * pointX + coefB * pointY + coefC)) / den; |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public double getDistance(Point2D point) { |
140 |
return getDistance(point.getX(), point.getY());
|
141 |
} |
142 |
|
143 |
@Override
|
144 |
public double getDistance(EuclideanLine2D line) { |
145 |
|
146 |
if (isParallel(line)) { //Parallel lines |
147 |
Double num = Math.abs(line.getC() - coefC); |
148 |
if (Double.isInfinite(m)) { //Vertical lines |
149 |
return num;
|
150 |
} |
151 |
Double den = Math.sqrt(Math.pow(coefA, 2) + Math.pow(coefB, 2)); |
152 |
return num / den;
|
153 |
} |
154 |
return 0d; |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public boolean isParallel(EuclideanLine2D line) { |
159 |
|
160 |
double m1 = line.getSlope();
|
161 |
return (m == m1 || (Double.isInfinite(m) && Double.isInfinite(m1))); |
162 |
} |
163 |
|
164 |
@Override
|
165 |
public Point2D getIntersection(EuclideanLine2D line) { |
166 |
//Using Cramer's rule
|
167 |
double a1 = coefA;
|
168 |
double b1 = coefB;
|
169 |
double c1 = coefC;
|
170 |
|
171 |
Double a2 = line.getA();
|
172 |
Double b2 = line.getB();
|
173 |
Double c2 = line.getC();
|
174 |
|
175 |
Double det = a1 * b2 - a2 * b1;
|
176 |
Double detX = -c1 * b2 + c2 * b1;
|
177 |
Double detY = -a1 * c2 + a2 * c1;
|
178 |
|
179 |
Double x = detX / det;
|
180 |
Double y = detY / det;
|
181 |
|
182 |
return new Point2D.Double(x, y); |
183 |
|
184 |
//Using Apache commons math library, need import the library
|
185 |
// RealMatrix coefficients =
|
186 |
// new Array2DRowRealMatrix(new double[][] { { coefA, coefB }, { line.getA(), line.getB() } }, false);
|
187 |
// DecompositionSolver solver = new LUDecomposition(coefficients).getSolver();
|
188 |
// RealVector constants = new ArrayRealVector(new double[] { -coefC, -line.getC() }, false);
|
189 |
// RealVector solution = solver.solve(constants);
|
190 |
// return new Point2D.Double(solution.getEntry(0), solution.getEntry(1));
|
191 |
|
192 |
} |
193 |
|
194 |
@Override
|
195 |
public EuclideanLine2D getPerpendicular(double pointX, double pointY) { |
196 |
if(Math.abs(m)==0.0){ |
197 |
return new EuclideanLine2DImpl(1, 0, -pointX); |
198 |
} |
199 |
if(Double.isInfinite(m)){ |
200 |
return new EuclideanLine2DImpl(0, 1, -pointY); |
201 |
} |
202 |
// perpendicular slope (m)
|
203 |
Double m1 = -1 / m; |
204 |
|
205 |
// perpendicular y-intercept (b)
|
206 |
Double b1 = pointY - (m1 * pointX);
|
207 |
|
208 |
return new EuclideanLine2DImpl(m1, b1); |
209 |
} |
210 |
|
211 |
@Override
|
212 |
public EuclideanLine2D getPerpendicular(Point2D point) { |
213 |
return getPerpendicular(point.getX(), point.getY());
|
214 |
} |
215 |
|
216 |
@Override
|
217 |
public Point2D getNearestPoint(double pointX, double pointY) { |
218 |
double x;
|
219 |
double y;
|
220 |
|
221 |
if (Double.isInfinite(m)) { |
222 |
y = pointY; |
223 |
x = (-coefB * y - coefC) / coefA; |
224 |
} else if (Math.abs(m) == 0) { |
225 |
x = pointX; |
226 |
y = b; |
227 |
} else {
|
228 |
|
229 |
EuclideanLine2D perp = getPerpendicular(pointX, pointY); |
230 |
Double m1 = perp.getSlope();
|
231 |
Double b1 = perp.getYIntercept();
|
232 |
|
233 |
x = (b1 - b) / (m - m1); |
234 |
y = m * x + b; |
235 |
} |
236 |
|
237 |
return new Point2D.Double(x, y); |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public Point2D getNearestPoint(Point2D point) { |
242 |
return getNearestPoint(point.getX(), point.getY());
|
243 |
} |
244 |
|
245 |
@Override
|
246 |
public EuclideanLine2D[] getBisectors(EuclideanLine2D line) { |
247 |
/*
|
248 |
r) A1x + B1y + C1 = 0
|
249 |
s) A2x + B2y + C2 = 0
|
250 |
|
251 |
|A1x+B1y+C1|/SQRT(A1?+B1?)= |A2x+B2y+C2|/SQRT(A2?+B2?)
|
252 |
*/
|
253 |
|
254 |
Double a1 = coefA;
|
255 |
Double b1 = coefB;
|
256 |
Double c1 = coefC;
|
257 |
|
258 |
Double a2 = line.getA();
|
259 |
Double b2 = line.getB();
|
260 |
Double c2 = line.getC();
|
261 |
|
262 |
Double den1 = Math.sqrt(Math.pow(a1,2) + Math.pow(b1,2)); |
263 |
Double den2 = Math.sqrt(Math.pow(a2,2) + Math.pow(b2,2)); |
264 |
|
265 |
EuclideanLine2D[] result = new EuclideanLine2DImpl[2]; |
266 |
result[0] = new EuclideanLine2DImpl( |
267 |
den2 * a1 - den1 * a2, |
268 |
den2 * b1 - den1 * b2, |
269 |
den2 * c1 - den1 * c2 |
270 |
); |
271 |
result[1] = new EuclideanLine2DImpl( |
272 |
den2 * a1 + den1 * a2, |
273 |
den2 * b1 + den1 * b2, |
274 |
den2 * c1 + den1 * c2 |
275 |
); |
276 |
|
277 |
return result;
|
278 |
|
279 |
} |
280 |
|
281 |
} |