Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/VectorUtils.java @ 1801:6f83d9d434f2
Polygon2D: Generate polygons for trivial cases.
flys-artifacts/trunk@3125 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 31 Oct 2011 17:05:14 +0000 |
parents | 281b9430c720 |
children | 5642a83420f2 |
comparison
equal
deleted
inserted
replaced
1800:1402991208d5 | 1801:6f83d9d434f2 |
---|---|
76 public static boolean epsilonEquals(Point2D a, Point2D b) { | 76 public static boolean epsilonEquals(Point2D a, Point2D b) { |
77 return Math.abs(X(a)-X(b)) < EPSILON | 77 return Math.abs(X(a)-X(b)) < EPSILON |
78 && Math.abs(Y(a)-Y(b)) < EPSILON; | 78 && Math.abs(Y(a)-Y(b)) < EPSILON; |
79 } | 79 } |
80 | 80 |
81 public static final Point2D intersection( | |
82 Point2D p1, Point2D p2, | |
83 Point2D p3, Point2D p4 | |
84 ) { | |
85 double x1 = X(p1); | |
86 double y1 = Y(p1); | |
87 double x2 = X(p2); | |
88 double y2 = Y(p2); | |
89 double x3 = X(p3); | |
90 double y3 = Y(p3); | |
91 double x4 = X(p4); | |
92 double y4 = Y(p4); | |
93 | |
94 // Compute a1, b1, c1, where line joining points 1 and 2 | |
95 // is "a1 x + b1 y + c1 = 0". | |
96 double a1 = y2 - y1; | |
97 double b1 = x1 - x2; | |
98 double c1 = x2*y1 - x1*y2; | |
99 | |
100 // Compute r3 and r4. | |
101 double r3 = a1*x3 + b1*y3 + c1; | |
102 double r4 = a1*x4 + b1*y4 + c1; | |
103 | |
104 if (r3 != 0d && r4 != 0d && r3*r4 >= 0) { | |
105 return null; | |
106 } | |
107 | |
108 // Compute a2, b2, c2 | |
109 double a2 = y4 - y3; | |
110 double b2 = x3 - x4; | |
111 double c2 = (x4 * y3) - (x3 * y4); | |
112 | |
113 // Compute r1 and r2 | |
114 double r1 = a2*x1 + b2*y1 + c2; | |
115 double r2 = a2*x2 + b2*y2 + c2; | |
116 | |
117 if (r1 != 0d && r2 != 0d && r1*r2 >= 0) { | |
118 return null; | |
119 } | |
120 | |
121 // Line segments intersect: compute intersection point. | |
122 double denom = a1*b2 - a2*b1; | |
123 | |
124 if (denom == 0d) { // collinear | |
125 return null; | |
126 } | |
127 | |
128 double offset = Math.abs(denom)/2d; | |
129 | |
130 // The denom/2 is to get rounding instead of truncating. It | |
131 // is added or subtracted to the numerator, depending upon the | |
132 // sign of the numerator. | |
133 double num = b1*c2 - b2*c1; | |
134 | |
135 double x = num < 0d | |
136 ? (num - offset)/denom | |
137 : (num + offset)/denom; | |
138 | |
139 num = a2*c1 - a1*c2; | |
140 | |
141 double y = num < 0d | |
142 ? (num - offset)/denom | |
143 : (num + offset)/denom; | |
144 | |
145 return new Point2D.Double(x, y); | |
146 } | |
147 | |
81 } | 148 } |
82 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 149 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |