Main repository of MikuMikuStudio
Revision | 83be495582060c369e8e0cf516da19c603f13176 (tree) |
---|---|
Time | 2003-09-17 07:22:32 |
Author | mojomonkey <mojomonkey@75d0...> |
Commiter | mojomonkey |
Finished Distance class.
git-svn-id: http://jmonkeyengine.googlecode.com/svn/trunk@91 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
@@ -33,7 +33,10 @@ package jme.math; | ||
33 | 33 | |
34 | 34 | /** |
35 | 35 | * <code>Distance</code> is a static class that provides commonly used math |
36 | - * functions. | |
36 | + * functions. All tests contain a set of three methods. First is a method to | |
37 | + * calculate the distance, second is to calculate the distance square and third | |
38 | + * is to calculate the distance squared and keep a copy of the parameters | |
39 | + * defined for the distance algorithms. | |
37 | 40 | * |
38 | 41 | * <br><br> |
39 | 42 | * <b>NOTE:</b> See 3D Game Engine Design. David H. Eberly. |
@@ -75,22 +78,64 @@ public class Distance { | ||
75 | 78 | } |
76 | 79 | |
77 | 80 | /** |
81 | + * <code>distancePointLine</code> calculates teh distance between a | |
82 | + * point and a line. | |
83 | + * @param point the point to check. | |
84 | + * @param line the line to check. | |
85 | + * @return the distance between the point and the line. | |
86 | + */ | |
87 | + public static float distancePointLine(Vector point, Line line) { | |
88 | + return (float) Math.sqrt(distancePointLineSquared(point, line)); | |
89 | + } | |
90 | + | |
91 | + /** | |
92 | + * <code>distancePointLineSquared</code> calculates the distance squared | |
93 | + * between a point and a line. | |
94 | + * @param point the point to check. | |
95 | + * @param line the line to check. | |
96 | + * @return the distance squared between a point and a line. | |
97 | + */ | |
98 | + public static float distancePointLineSquared(Vector point, Line line) { | |
99 | + return distancePointLineSquared(point, line, null); | |
100 | + } | |
101 | + | |
102 | + /** | |
78 | 103 | * <code>distancePointLineSquared</code> calculates the distance squared |
79 | 104 | * between a point and a line. |
80 | 105 | * @param point the point to check. |
81 | 106 | * @param line the line to check. |
107 | + * @param lineParam container for t where L(t) = B + tM. | |
82 | 108 | * @return the distance squared between a point and line. |
83 | 109 | */ |
84 | - public static float distancePointLineSquared(Vector point, Line line) { | |
110 | + public static float distancePointLineSquared( | |
111 | + Vector point, | |
112 | + Line line, | |
113 | + float[] lineParam) { | |
114 | + | |
85 | 115 | Vector diff = point.subtract(line.getOrigin()); |
86 | 116 | float squareLen = line.getDirection().lengthSquared(); |
87 | 117 | float t = diff.dot(line.getDirection()) / squareLen; |
88 | 118 | diff = diff.subtract(line.getDirection().mult(t)); |
89 | 119 | |
120 | + if (lineParam != null) { | |
121 | + lineParam[0] = t; | |
122 | + } | |
123 | + | |
90 | 124 | return diff.lengthSquared(); |
91 | 125 | } |
92 | 126 | |
93 | 127 | /** |
128 | + * <code>distancePointRay</code> calculates the distance between a point | |
129 | + * and a line. | |
130 | + * @param point the point to check. | |
131 | + * @param ray the ray to check. | |
132 | + * @return the distance between a point and a ray. | |
133 | + */ | |
134 | + public static float distancePointRay(Vector point, Line ray) { | |
135 | + return (float) Math.sqrt(distancePointRaySquared(point, ray)); | |
136 | + } | |
137 | + | |
138 | + /** | |
94 | 139 | * <code>distancePointRaySquared</code> calculates the distance |
95 | 140 | * squared between a point and a ray. |
96 | 141 | * @param point the point to check. |
@@ -98,6 +143,22 @@ public class Distance { | ||
98 | 143 | * @return the distance between a point and ray. |
99 | 144 | */ |
100 | 145 | public static float distancePointRaySquared(Vector point, Line ray) { |
146 | + return distancePointRaySquared(point, ray, null); | |
147 | + } | |
148 | + | |
149 | + /** | |
150 | + * <code>distancePointRaySquared</code> calculates the distance | |
151 | + * squared between a point and a ray. | |
152 | + * @param point the point to check. | |
153 | + * @param ray the ray to check. | |
154 | + * @param rayParam container for t where L(t) = B + tM and t >= 0. | |
155 | + * @return the distance between a point and ray. | |
156 | + */ | |
157 | + public static float distancePointRaySquared( | |
158 | + Vector point, | |
159 | + Line ray, | |
160 | + float[] rayParam) { | |
161 | + | |
101 | 162 | Vector diff = point.subtract(ray.getOrigin()); |
102 | 163 | float t = diff.dot(ray.getDirection()); |
103 | 164 |
@@ -108,10 +169,25 @@ public class Distance { | ||
108 | 169 | diff = diff.subtract(ray.getDirection().mult(t)); |
109 | 170 | } |
110 | 171 | |
172 | + if (rayParam != null) { | |
173 | + rayParam[0] = t; | |
174 | + } | |
175 | + | |
111 | 176 | return diff.lengthSquared(); |
112 | 177 | } |
113 | 178 | |
114 | 179 | /** |
180 | + * <code>distancePointSegment</code> calculates the distance between a | |
181 | + * point and a line segment. | |
182 | + * @param point the point to check. | |
183 | + * @param seg the line segment to check. | |
184 | + * @return the distance between a point and a line segment. | |
185 | + */ | |
186 | + public static float distancePointSegment(Vector point, Line seg) { | |
187 | + return (float) Math.sqrt(distancePointSegmentSquared(point, seg)); | |
188 | + } | |
189 | + | |
190 | + /** | |
115 | 191 | * <code>distancePointSegmentSquared</code> calculates the distance |
116 | 192 | * squared between a point and a line segment. |
117 | 193 | * @param point the point to check. |
@@ -119,6 +195,22 @@ public class Distance { | ||
119 | 195 | * @return the distance squared between a point and line segment. |
120 | 196 | */ |
121 | 197 | public static float distancePointSegmentSquared(Vector point, Line seg) { |
198 | + return distancePointSegmentSquared(point, seg, null); | |
199 | + } | |
200 | + | |
201 | + /** | |
202 | + * <code>distancePointSegmentSquared</code> calculates the distance | |
203 | + * squared between a point and a line segment. | |
204 | + * @param point the point to check. | |
205 | + * @param seg the line segment to check. | |
206 | + * @param lineParam storage for t where L(t) = B + tM and t is between (0,1). | |
207 | + * @return the distance squared between a point and line segment. | |
208 | + */ | |
209 | + public static float distancePointSegmentSquared( | |
210 | + Vector point, | |
211 | + Line seg, | |
212 | + float[] lineParam) { | |
213 | + | |
122 | 214 | Vector diff = point.subtract(seg.getOrigin()); |
123 | 215 | float t = diff.dot(seg.getDirection()); |
124 | 216 |
@@ -139,6 +231,17 @@ public class Distance { | ||
139 | 231 | } |
140 | 232 | |
141 | 233 | /** |
234 | + * <code>distancePointRectangle</code> calculates the distance between a | |
235 | + * pointa and a rectangle. | |
236 | + * @param point the point to check. | |
237 | + * @param rect the rectangle to check. | |
238 | + * @return the distance between the point and the rectangle. | |
239 | + */ | |
240 | + public static float distancePointRectangle(Vector point, Rectangle rect) { | |
241 | + return (float) Math.sqrt(distancePointRectangleSquared(point, rect)); | |
242 | + } | |
243 | + | |
244 | + /** | |
142 | 245 | * <code>distancePointRectangleSquared</code> calculates the distance squared |
143 | 246 | * between a point and a rectangle. |
144 | 247 | * @param point the point to check. |
@@ -148,6 +251,7 @@ public class Distance { | ||
148 | 251 | public static float distancePointRectangleSquared( |
149 | 252 | Vector point, |
150 | 253 | Rectangle rect) { |
254 | + | |
151 | 255 | return distancePointRectangleSquared(point, rect, null, null); |
152 | 256 | } |
153 | 257 |
@@ -156,13 +260,15 @@ public class Distance { | ||
156 | 260 | * between a point and a rectangle. |
157 | 261 | * @param point the point to check. |
158 | 262 | * @param rect the rectangle to check. |
263 | + * @param sParam storage for the s value in the equation: Q(s,t) = |T(s,t) - P|. | |
264 | + * @param tParam storage for the t value in the equation: Q(s,t) = |T(s,t) - P|. | |
159 | 265 | * @return the distance between the point and the rectangle. |
160 | 266 | */ |
161 | 267 | public static float distancePointRectangleSquared( |
162 | 268 | Vector point, |
163 | 269 | Rectangle rect, |
164 | - float[] pointParam, | |
165 | - float[] rectParam) { | |
270 | + float[] sParam, | |
271 | + float[] tParam) { | |
166 | 272 | Vector diff = rect.getOrigin().subtract(point); |
167 | 273 | float a00 = rect.getFirstEdge().lengthSquared(); |
168 | 274 | float a11 = rect.getSecondEdge().lengthSquared(); |
@@ -192,15 +298,24 @@ public class Distance { | ||
192 | 298 | t = 1.0f; |
193 | 299 | distanceSquared += a11 + 2.0 * b1; |
194 | 300 | } |
195 | - if (pointParam != null) | |
196 | - pointParam[0] = s; | |
301 | + if (sParam != null) | |
302 | + sParam[0] = s; | |
197 | 303 | |
198 | - if (rectParam != null) | |
199 | - rectParam[0] = t; | |
304 | + if (tParam != null) | |
305 | + tParam[0] = t; | |
200 | 306 | return Math.abs(distanceSquared); |
201 | 307 | } |
202 | 308 | |
203 | 309 | /** |
310 | + * <code>distanceLineLine</code> calculates the distance between two lines. | |
311 | + * @param line1 the first line to check. | |
312 | + * @param line2 the second line to check. | |
313 | + * @return the distance between two lines. | |
314 | + */ | |
315 | + public static float distanceLineLine(Line line1, Line line2) { | |
316 | + return (float) Math.sqrt(distanceLineLineSquared(line1, line2)); | |
317 | + } | |
318 | + /** | |
204 | 319 | * <code>distanceLineLineSquared</code> calculates the distance squared |
205 | 320 | * between two lines. |
206 | 321 | * @param line1 the first line to check. |
@@ -208,6 +323,22 @@ public class Distance { | ||
208 | 323 | * @return the distance squared between two lines. |
209 | 324 | */ |
210 | 325 | public static float distanceLineLineSquared(Line line1, Line line2) { |
326 | + return distanceLineLineSquared(line1, line2, null, null); | |
327 | + } | |
328 | + /** | |
329 | + * <code>distanceLineLineSquared</code> calculates the distance squared | |
330 | + * between two lines. | |
331 | + * @param line1 the first line to check. | |
332 | + * @param line2 the second line to check. | |
333 | + * @param sParam container for s in the first line where L0(s) = B0 + sM0. | |
334 | + * @param tParam container for t in the second line where L1(t) = B1 + tM1. | |
335 | + * @return the distance squared between two lines. | |
336 | + */ | |
337 | + public static float distanceLineLineSquared( | |
338 | + Line line1, | |
339 | + Line line2, | |
340 | + float[] sParam, | |
341 | + float[] tParam) { | |
211 | 342 | Vector diff = line1.getOrigin().subtract(line2.getOrigin()); |
212 | 343 | float a = line1.getDirection().lengthSquared(); |
213 | 344 | float b = -line1.getDirection().dot(line2.getDirection()); |
@@ -237,10 +368,29 @@ public class Distance { | ||
237 | 368 | distanceSquared = d * s + f; |
238 | 369 | } |
239 | 370 | |
371 | + if (tParam != null) { | |
372 | + tParam[0] = t; | |
373 | + } | |
374 | + | |
375 | + if (sParam != null) { | |
376 | + sParam[0] = s; | |
377 | + } | |
378 | + | |
240 | 379 | return Math.abs(distanceSquared); |
241 | 380 | } |
242 | 381 | |
243 | 382 | /** |
383 | + * <code>distanceLineRay</code> calculates the distance between a line and | |
384 | + * a ray. | |
385 | + * @param line the line to check. | |
386 | + * @param ray the ray to check. | |
387 | + * @return the distance between a line and a ray. | |
388 | + */ | |
389 | + public static float distanceLineRay(Line line, Line ray) { | |
390 | + return (float) Math.sqrt(distanceLineRaySquared(line, ray)); | |
391 | + } | |
392 | + | |
393 | + /** | |
244 | 394 | * <code>distanceLineRaySquared</code> calculates the squared distance |
245 | 395 | * between a line and a ray. |
246 | 396 | * @param line the line to check. |
@@ -248,6 +398,23 @@ public class Distance { | ||
248 | 398 | * @return the distance between the line and the ray. |
249 | 399 | */ |
250 | 400 | public static float distanceLineRaySquared(Line line, Line ray) { |
401 | + return distanceLineRaySquared(line, ray, null, null); | |
402 | + } | |
403 | + | |
404 | + /** | |
405 | + * <code>distanceLineRaySquared</code> calculates the squared distance | |
406 | + * between a line and a ray. | |
407 | + * @param line the line to check. | |
408 | + * @param ray the ray to check. | |
409 | + * @param sParam container for s where s is L0(s) = B0 + sM0. | |
410 | + * @param tParam container for t where t is L1(t) = B1 + tM1. | |
411 | + * @return the distance between the line and the ray. | |
412 | + */ | |
413 | + public static float distanceLineRaySquared( | |
414 | + Line line, | |
415 | + Line ray, | |
416 | + float[] sParam, | |
417 | + float[] tParam) { | |
251 | 418 | Vector diff = line.getOrigin().subtract(ray.getOrigin()); |
252 | 419 | float a = line.getDirection().lengthSquared(); |
253 | 420 | float b = -line.getDirection().dot(ray.getDirection()); |
@@ -286,10 +453,29 @@ public class Distance { | ||
286 | 453 | distanceSquared = d * s + f; |
287 | 454 | } |
288 | 455 | |
456 | + if (tParam != null) { | |
457 | + tParam[0] = t; | |
458 | + } | |
459 | + | |
460 | + if (sParam != null) { | |
461 | + sParam[0] = s; | |
462 | + } | |
463 | + | |
289 | 464 | return Math.abs(distanceSquared); |
290 | 465 | } |
291 | 466 | |
292 | 467 | /** |
468 | + * <code>distanceLineSegment</code> calculates the distance between a | |
469 | + * line and a line segment. | |
470 | + * @param line the line to check. | |
471 | + * @param seg the line segment to check. | |
472 | + * @return the distance between a line and a line segment. | |
473 | + */ | |
474 | + public static float distanceLineSegment(Line line, Line seg) { | |
475 | + return (float) Math.sqrt(distanceLineSegmentSquared(line, seg)); | |
476 | + } | |
477 | + | |
478 | + /** | |
293 | 479 | * <code>distanceLineSegementSquared</code> calculates the distance |
294 | 480 | * squared between a line and a line segment. |
295 | 481 | * @param line the line to check. |
@@ -305,15 +491,15 @@ public class Distance { | ||
305 | 491 | * squared between a line and a line segment. |
306 | 492 | * @param line the line to check. |
307 | 493 | * @param seg the line segment to check. |
308 | - * @param lineParam storage for the line parameter. | |
309 | - * @param segParam storage for the segment parameter. | |
494 | + * @param sParam container for s where s is L0(s) = B0 + sM0. | |
495 | + * @param tParam container for t where t is L1(t) = B1 + tM1. | |
310 | 496 | * @return the distance squared between a line and a line segment. |
311 | 497 | */ |
312 | 498 | public static float distanceLineSegmentSquared( |
313 | 499 | Line line, |
314 | 500 | Line seg, |
315 | - float[] lineParam, | |
316 | - float[] segParam) { | |
501 | + float[] sParam, | |
502 | + float[] tParam) { | |
317 | 503 | |
318 | 504 | Vector diff = line.getOrigin().subtract(seg.getOrigin()); |
319 | 505 | float a = line.getDirection().lengthSquared(); |
@@ -362,17 +548,28 @@ public class Distance { | ||
362 | 548 | t = 0.0f; |
363 | 549 | squareDistance = d * s + f; |
364 | 550 | } |
365 | - if (null != lineParam) { | |
366 | - lineParam[0] = s; | |
551 | + if (null != sParam) { | |
552 | + sParam[0] = s; | |
367 | 553 | } |
368 | - if (null != segParam) { | |
369 | - segParam[0] = t; | |
554 | + if (null != tParam) { | |
555 | + tParam[0] = t; | |
370 | 556 | } |
371 | 557 | |
372 | 558 | return Math.abs(squareDistance); |
373 | 559 | } |
374 | 560 | |
375 | 561 | /** |
562 | + * <code>distanceLineRectangle</code> calculates the distance between a | |
563 | + * line and a rectangle. | |
564 | + * @param line the line to check. | |
565 | + * @param rect the rectangle to check. | |
566 | + * @return the distance between a line and a rectangle. | |
567 | + */ | |
568 | + public static float distanceLineRectangle(Line line, Rectangle rect) { | |
569 | + return (float) Math.sqrt(distanceLineRectangleSquared(line, rect)); | |
570 | + } | |
571 | + | |
572 | + /** | |
376 | 573 | * <code>distanceLineRectangleSquared</code> calculates the distance squared |
377 | 574 | * between a line and a rectangle. |
378 | 575 | * @param line the line to check. |
@@ -382,6 +579,27 @@ public class Distance { | ||
382 | 579 | public static float distanceLineRectangleSquared( |
383 | 580 | Line line, |
384 | 581 | Rectangle rect) { |
582 | + return distanceLineRectangleSquared(line, rect, null, null, null); | |
583 | + } | |
584 | + | |
585 | + /** | |
586 | + * <code>distanceLineRectangleSquared</code> calculates the distance squared | |
587 | + * between a line and a rectangle. | |
588 | + * @param line the line to check. | |
589 | + * @param rect the rectangle to check. | |
590 | + * @param rParam container for the Line's r parameter in L(r) = B + rM. | |
591 | + * @param sParam container for the rectangle's s parameter in | |
592 | + * R(s,t) = A + sE0 + tE1. | |
593 | + * @param tParam container for the rectangle's t parameter in | |
594 | + * R(s,t) = A + sE0 + tE1. | |
595 | + * @return the distance squared. | |
596 | + */ | |
597 | + public static float distanceLineRectangleSquared( | |
598 | + Line line, | |
599 | + Rectangle rect, | |
600 | + float[] rParam, | |
601 | + float[] sParam, | |
602 | + float[] tParam) { | |
385 | 603 | |
386 | 604 | float[] r = new float[1]; |
387 | 605 | float[] s = new float[1]; |
@@ -582,17 +800,56 @@ public class Distance { | ||
582 | 800 | } |
583 | 801 | } |
584 | 802 | |
803 | + if (rParam != null) { | |
804 | + rParam[0] = r[0]; | |
805 | + } | |
806 | + | |
807 | + if (sParam != null) { | |
808 | + sParam[0] = s[0]; | |
809 | + } | |
810 | + | |
811 | + if (tParam != null) { | |
812 | + tParam[0] = t[0]; | |
813 | + } | |
814 | + | |
585 | 815 | return Math.abs(distanceSquared); |
586 | 816 | } |
587 | 817 | |
588 | 818 | /** |
819 | + * <code>distanceRayRay</code> returns the distance between two rays. | |
820 | + * @param ray1 the first ray to check. | |
821 | + * @param ray2 the second ray to check. | |
822 | + * @return the distance between two rays. | |
823 | + */ | |
824 | + public static float distanceRayRay(Line ray1, Line ray2) { | |
825 | + return (float) Math.sqrt(distanceRayRaySquared(ray1, ray2)); | |
826 | + } | |
827 | + | |
828 | + /** | |
829 | + * <code>distanceRayRaySquared</code> returns the distance squared between | |
830 | + * two rays. | |
831 | + * @param ray1 the first ray to check. | |
832 | + * @param ray2 the second ray to check. | |
833 | + * @return the distance squared between the two rays. | |
834 | + */ | |
835 | + public static float distanceRayRaySquared(Line ray1, Line ray2) { | |
836 | + return distanceRayRaySquared(ray1, ray2, null, null); | |
837 | + } | |
838 | + | |
839 | + /** | |
589 | 840 | * <code>distanceRayRaySquared</code> calculates the distance squared |
590 | 841 | * between two rays. |
591 | 842 | * @param ray1 the first ray to check. |
592 | 843 | * @param ray2 the second ray to check. |
844 | + * @param sParam container for s in L1(s) = B1 + sM1. | |
845 | + * @param tParam container for t in L2(t) = B2 + tM2. | |
593 | 846 | * @return the distance squared between the two rays. |
594 | 847 | */ |
595 | - public static float distanceRayRaySquared(Line ray1, Line ray2) { | |
848 | + public static float distanceRayRaySquared( | |
849 | + Line ray1, | |
850 | + Line ray2, | |
851 | + float[] sParam, | |
852 | + float[] tParam) { | |
596 | 853 | Vector diff = ray1.getOrigin().subtract(ray2.getOrigin()); |
597 | 854 | float a = ray1.getDirection().lengthSquared(); |
598 | 855 | float b = -ray1.getDirection().dot(ray2.getDirection()); |
@@ -612,8 +869,7 @@ public class Distance { | ||
612 | 869 | t = b * d - a * e; |
613 | 870 | |
614 | 871 | if (s >= 0.0) { |
615 | - if (t >= 0.0) // region 0 (interior) | |
616 | - { | |
872 | + if (t >= 0.0) { | |
617 | 873 | // minimum at two interior points of rays |
618 | 874 | float inverseDeterminate = 1.0f / determinate; |
619 | 875 | s *= inverseDeterminate; |
@@ -622,8 +878,7 @@ public class Distance { | ||
622 | 878 | s * (a * s + b * t + 2.0f * d) |
623 | 879 | + t * (b * s + c * t + 2.0f * e) |
624 | 880 | + f; |
625 | - } else // region 3 (side) | |
626 | - { | |
881 | + } else { | |
627 | 882 | t = 0.0f; |
628 | 883 | if (d >= 0.0) { |
629 | 884 | s = 0.0f; |
@@ -634,8 +889,7 @@ public class Distance { | ||
634 | 889 | } |
635 | 890 | } |
636 | 891 | } else { |
637 | - if (t >= 0.0) // region 1 (side) | |
638 | - { | |
892 | + if (t >= 0.0) { | |
639 | 893 | s = 0.0f; |
640 | 894 | if (e >= 0.0) { |
641 | 895 | t = 0.0f; |
@@ -644,8 +898,7 @@ public class Distance { | ||
644 | 898 | t = -e / c; |
645 | 899 | distanceSquared = e * t + f; |
646 | 900 | } |
647 | - } else // region 2 (corner) | |
648 | - { | |
901 | + } else { | |
649 | 902 | if (d < 0.0) { |
650 | 903 | s = -d / a; |
651 | 904 | t = 0.0f; |
@@ -689,10 +942,29 @@ public class Distance { | ||
689 | 942 | } |
690 | 943 | } |
691 | 944 | |
945 | + if (sParam != null) { | |
946 | + sParam[0] = s; | |
947 | + } | |
948 | + | |
949 | + if (tParam != null) { | |
950 | + tParam[0] = t; | |
951 | + } | |
952 | + | |
692 | 953 | return Math.abs(distanceSquared); |
693 | 954 | } |
694 | 955 | |
695 | 956 | /** |
957 | + * <code>distanceRaySegment</code> calculates the distance between a ray | |
958 | + * and a line segment. | |
959 | + * @param ray the ray to check. | |
960 | + * @param seg the line segment to check. | |
961 | + * @return | |
962 | + */ | |
963 | + public static float distanceRaySegment(Line ray, Line seg) { | |
964 | + return (float) Math.sqrt(distanceRaySegmentSquared(ray, seg)); | |
965 | + } | |
966 | + | |
967 | + /** | |
696 | 968 | * <code>distanceRaySegmentSquared</code> calculates the distance |
697 | 969 | * squared between a ray and a line segment. |
698 | 970 | * @param ray the ray to check. |
@@ -708,13 +980,15 @@ public class Distance { | ||
708 | 980 | * squared between a ray and a line segment. |
709 | 981 | * @param ray the ray to check. |
710 | 982 | * @param seg the line segment to check. |
983 | + * @param sParam container for s where L1(s) = B1 + sM1. | |
984 | + * @param tParam container for t where L2(t) = B2 + tM2. | |
711 | 985 | * @return the distance between the ray and the line segment. |
712 | 986 | */ |
713 | 987 | public static float distanceRaySegmentSquared( |
714 | 988 | Line ray, |
715 | 989 | Line seg, |
716 | - float[] rayParam, | |
717 | - float[] segParam) { | |
990 | + float[] sParam, | |
991 | + float[] tParam) { | |
718 | 992 | |
719 | 993 | Vector diff = ray.getOrigin().subtract(seg.getOrigin()); |
720 | 994 | float a = ray.getDirection().lengthSquared(); |
@@ -852,16 +1126,29 @@ public class Distance { | ||
852 | 1126 | } |
853 | 1127 | } |
854 | 1128 | |
855 | - if (rayParam != null) | |
856 | - rayParam[0] = s; | |
1129 | + if (sParam != null) { | |
1130 | + sParam[0] = s; | |
1131 | + } | |
857 | 1132 | |
858 | - if (segParam != null) | |
859 | - segParam[0] = t; | |
1133 | + if (tParam != null) { | |
1134 | + tParam[0] = t; | |
1135 | + } | |
860 | 1136 | |
861 | 1137 | return Math.abs(distanceSquared); |
862 | 1138 | } |
863 | 1139 | |
864 | 1140 | /** |
1141 | + * <code>distanceRayRectangle</code> calculates the distance between a | |
1142 | + * ray and a rectangle. | |
1143 | + * @param ray the ray to check. | |
1144 | + * @param rect the rectangle to check. | |
1145 | + * @return the distance between a ray and a rectangle. | |
1146 | + */ | |
1147 | + public static float distanceRayRectangle(Line ray, Rectangle rect) { | |
1148 | + return (float) Math.sqrt(distanceRayRectangleSquared(ray, rect)); | |
1149 | + } | |
1150 | + | |
1151 | + /** | |
865 | 1152 | * <code>distanceRayRectangleSquared</code> calculates the distance |
866 | 1153 | * squared between a ray and a rectangle. |
867 | 1154 | * @param ray the ray to check. |
@@ -869,6 +1156,28 @@ public class Distance { | ||
869 | 1156 | * @return the distance squared between a ray and a rectangle. |
870 | 1157 | */ |
871 | 1158 | public static float distanceRayRectangleSquared(Line ray, Rectangle rect) { |
1159 | + return distanceRayRectangleSquared(ray, rect, null, null, null); | |
1160 | + } | |
1161 | + | |
1162 | + /** | |
1163 | + * <code>distanceRayRectangleSquared</code> calculates the distance | |
1164 | + * squared between a ray and a rectangle. | |
1165 | + * @param ray the ray to check. | |
1166 | + * @param rect the rectangle to check. | |
1167 | + * @param rParam container for the Line's r parameter in L(r) = B + rM. | |
1168 | + * @param sParam container for the rectangle's s parameter in | |
1169 | + * R(s,t) = A + sE0 + tE1. | |
1170 | + * @param tParam container for the rectangle's t parameter in | |
1171 | + * R(s,t) = A + sE0 + tE1. | |
1172 | + * | |
1173 | + * @return the distance squared between a ray and a rectangle. | |
1174 | + */ | |
1175 | + public static float distanceRayRectangleSquared( | |
1176 | + Line ray, | |
1177 | + Rectangle rect, | |
1178 | + float[] rParam, | |
1179 | + float[] sParam, | |
1180 | + float[] tParam) { | |
872 | 1181 | Vector diff = rect.getOrigin().subtract(ray.getOrigin()); |
873 | 1182 | float a00 = ray.getDirection().lengthSquared(); |
874 | 1183 | float a01 = -ray.getDirection().dot(rect.getFirstEdge()); |
@@ -1349,15 +1658,38 @@ public class Distance { | ||
1349 | 1658 | } |
1350 | 1659 | } |
1351 | 1660 | |
1661 | + if (rParam != null) { | |
1662 | + rParam[0] = r[0]; | |
1663 | + } | |
1664 | + | |
1665 | + if (sParam != null) { | |
1666 | + sParam[0] = s[0]; | |
1667 | + } | |
1668 | + | |
1669 | + if (tParam != null) { | |
1670 | + tParam[0] = t[0]; | |
1671 | + } | |
1672 | + | |
1352 | 1673 | return Math.abs(squaredDistance); |
1353 | 1674 | } |
1675 | + | |
1354 | 1676 | /** |
1355 | - * <code>distanceSegmentSegmentSquared</code> calculates the distance | |
1356 | - * squared between two line segments. | |
1357 | - * @param seg1 the first line segment to check. | |
1358 | - * @param seg2 the second line segment to check. | |
1359 | - * @return the distance between two line segments. | |
1360 | - */ | |
1677 | + * <code>distanceSegmentSegment</code> calculates the distance between | |
1678 | + * two lien segments. | |
1679 | + * @param seg1 the first line segment to check. | |
1680 | + * @param seg2 the second line segment to check. | |
1681 | + * @return the distance between the two line segments. | |
1682 | + */ | |
1683 | + public static float distanceSegmentSegment(Line seg1, Line seg2) { | |
1684 | + return (float) Math.sqrt(distanceSegmentSegment(seg1, seg2)); | |
1685 | + } | |
1686 | + /** | |
1687 | + * <code>distanceSegmentSegmentSquared</code> calculates the distance | |
1688 | + * squared between two line segments. | |
1689 | + * @param seg1 the first line segment to check. | |
1690 | + * @param seg2 the second line segment to check. | |
1691 | + * @return the distance squared between two line segments. | |
1692 | + */ | |
1361 | 1693 | public static float distanceSegmentSegmentSquared(Line seg1, Line seg2) { |
1362 | 1694 | return distanceSegmentSegmentSquared(seg1, seg2, null, null); |
1363 | 1695 | } |
@@ -1367,13 +1699,15 @@ public class Distance { | ||
1367 | 1699 | * squared between two line segments. |
1368 | 1700 | * @param seg1 the first line segment to check. |
1369 | 1701 | * @param seg2 the second line segment to check. |
1370 | - * @return the distance between two line segments. | |
1702 | + * @param sParam container for s where s is L1(s) = B1 + sM1. | |
1703 | + * @param tParam container for t where t is L2(t) = B2 + tM2. | |
1704 | + * @return the distance squared between two line segments. | |
1371 | 1705 | */ |
1372 | 1706 | public static float distanceSegmentSegmentSquared( |
1373 | 1707 | Line seg1, |
1374 | 1708 | Line seg2, |
1375 | - float[] seg1Param, | |
1376 | - float[] seg2Param) { | |
1709 | + float[] sParam, | |
1710 | + float[] tParam) { | |
1377 | 1711 | Vector diff = seg1.getOrigin().subtract(seg2.getOrigin()); |
1378 | 1712 | float a = seg1.getDirection().lengthSquared(); |
1379 | 1713 | float b = -seg1.getDirection().dot(seg2.getDirection()); |
@@ -1396,8 +1730,7 @@ public class Distance { | ||
1396 | 1730 | if (s >= 0.0) { |
1397 | 1731 | if (s <= determinate) { |
1398 | 1732 | if (t >= 0.0) { |
1399 | - if (t <= determinate) // region 0 (interior) | |
1400 | - { | |
1733 | + if (t <= determinate) { | |
1401 | 1734 | // minimum at two interior points of 3D lines |
1402 | 1735 | float inverseDeterminate = 1.0f / determinate; |
1403 | 1736 | s *= inverseDeterminate; |
@@ -1406,8 +1739,7 @@ public class Distance { | ||
1406 | 1739 | s * (a * s + b * t + 2.0f * d) |
1407 | 1740 | + t * (b * s + c * t + 2.0f * e) |
1408 | 1741 | + f; |
1409 | - } else // region 3 (side) | |
1410 | - { | |
1742 | + } else { | |
1411 | 1743 | t = 1.0f; |
1412 | 1744 | temp = b + d; |
1413 | 1745 | if (temp >= 0.0f) { |
@@ -1421,8 +1753,7 @@ public class Distance { | ||
1421 | 1753 | distanceSquared = temp * s + c + 2.0f * e + f; |
1422 | 1754 | } |
1423 | 1755 | } |
1424 | - } else // region 7 (side) | |
1425 | - { | |
1756 | + } else { | |
1426 | 1757 | t = 0.0f; |
1427 | 1758 | if (d >= 0.0f) { |
1428 | 1759 | s = 0.0f; |
@@ -1437,8 +1768,7 @@ public class Distance { | ||
1437 | 1768 | } |
1438 | 1769 | } else { |
1439 | 1770 | if (t >= 0.0f) { |
1440 | - if (t <= determinate) // region 1 (side) | |
1441 | - { | |
1771 | + if (t <= determinate) { | |
1442 | 1772 | s = 1.0f; |
1443 | 1773 | temp = b + e; |
1444 | 1774 | if (temp >= 0.0f) { |
@@ -1451,8 +1781,7 @@ public class Distance { | ||
1451 | 1781 | t = -temp / c; |
1452 | 1782 | distanceSquared = temp * t + a + 2.0f * d + f; |
1453 | 1783 | } |
1454 | - } else // region 2 (corner) | |
1455 | - { | |
1784 | + } else { | |
1456 | 1785 | temp = b + d; |
1457 | 1786 | if (-temp <= a) { |
1458 | 1787 | t = 1.0f; |
@@ -1481,8 +1810,7 @@ public class Distance { | ||
1481 | 1810 | } |
1482 | 1811 | } |
1483 | 1812 | } |
1484 | - } else // region 8 (corner) | |
1485 | - { | |
1813 | + } else { | |
1486 | 1814 | if (-d < a) { |
1487 | 1815 | t = 0.0f; |
1488 | 1816 | if (d >= 0.0f) { |
@@ -1510,8 +1838,7 @@ public class Distance { | ||
1510 | 1838 | } |
1511 | 1839 | } else { |
1512 | 1840 | if (t >= 0.0f) { |
1513 | - if (t <= determinate) // region 5 (side) | |
1514 | - { | |
1841 | + if (t <= determinate) { | |
1515 | 1842 | s = 0.0f; |
1516 | 1843 | if (e >= 0.0f) { |
1517 | 1844 | t = 0.0f; |
@@ -1523,8 +1850,7 @@ public class Distance { | ||
1523 | 1850 | t = -e / c; |
1524 | 1851 | distanceSquared = e * t + f; |
1525 | 1852 | } |
1526 | - } else // region 4 (corner) | |
1527 | - { | |
1853 | + } else { | |
1528 | 1854 | temp = b + d; |
1529 | 1855 | if (temp < 0.0f) { |
1530 | 1856 | t = 1.0f; |
@@ -1549,8 +1875,7 @@ public class Distance { | ||
1549 | 1875 | } |
1550 | 1876 | } |
1551 | 1877 | } |
1552 | - } else // region 6 (corner) | |
1553 | - { | |
1878 | + } else { | |
1554 | 1879 | if (d < 0.0f) { |
1555 | 1880 | t = 0.0f; |
1556 | 1881 | if (-d >= a) { |
@@ -1624,16 +1949,29 @@ public class Distance { | ||
1624 | 1949 | } |
1625 | 1950 | } |
1626 | 1951 | |
1627 | - if (seg1Param != null) | |
1628 | - seg1Param[0] = s; | |
1952 | + if (sParam != null) { | |
1953 | + sParam[0] = s; | |
1954 | + } | |
1629 | 1955 | |
1630 | - if (seg2Param != null) | |
1631 | - seg2Param[0] = t; | |
1956 | + if (tParam != null) { | |
1957 | + tParam[0] = t; | |
1958 | + } | |
1632 | 1959 | |
1633 | 1960 | return Math.abs(distanceSquared); |
1634 | 1961 | } |
1635 | 1962 | |
1636 | 1963 | /** |
1964 | + * <code>distanceSegmentRectangle</code> returns the distance between a | |
1965 | + * line segment and a rectangle. | |
1966 | + * @param seg the line segment to check. | |
1967 | + * @param rect the rectangle to check. | |
1968 | + * @return the distance between a line segment and a rectangle. | |
1969 | + */ | |
1970 | + public static float distanceSegmentRectangle(Line seg, Rectangle rect) { | |
1971 | + return (float) Math.sqrt(distanceSegmentRectangleSquared(seg, rect)); | |
1972 | + } | |
1973 | + | |
1974 | + /** | |
1637 | 1975 | * <code>distanceSegmentRectangleSquared</code> calculates the distance |
1638 | 1976 | * squared between a line segment and a rectangle. |
1639 | 1977 | * @param seg the line segment to check. |
@@ -1643,6 +1981,28 @@ public class Distance { | ||
1643 | 1981 | public static float distanceSegmentRectangleSquared( |
1644 | 1982 | Line seg, |
1645 | 1983 | Rectangle rect) { |
1984 | + return distanceSegmentRectangleSquared(seg, rect, null, null, null); | |
1985 | + } | |
1986 | + | |
1987 | + /** | |
1988 | + * <code>distanceSegmentRectangleSquared</code> calculates the distance | |
1989 | + * squared between a line segment and a rectangle. | |
1990 | + * @param seg the line segment to check. | |
1991 | + * @param rect the rectangle to check. | |
1992 | + * @param rParam container for the Line's r parameter in L(r) = B + rM. | |
1993 | + * @param sParam container for the rectangle's s parameter in | |
1994 | + * R(s,t) = A + sE0 + tE1. | |
1995 | + * @param tParam container for the rectangle's t parameter in | |
1996 | + * R(s,t) = A + sE0 + tE1. | |
1997 | + * | |
1998 | + * @return the distance squared between a line segment and rectangle. | |
1999 | + */ | |
2000 | + public static float distanceSegmentRectangleSquared( | |
2001 | + Line seg, | |
2002 | + Rectangle rect, | |
2003 | + float[] rParam, | |
2004 | + float[] sParam, | |
2005 | + float[] tParam) { | |
1646 | 2006 | |
1647 | 2007 | Vector diff = rect.getOrigin().subtract(seg.getOrigin()); |
1648 | 2008 | float a00 = seg.getDirection().lengthSquared(); |
@@ -1722,8 +2082,7 @@ public class Distance { | ||
1722 | 2082 | s[0] = s0[0]; |
1723 | 2083 | t[0] = t0[0]; |
1724 | 2084 | } |
1725 | - } else if (t[0] <= 1.0) // region 5m | |
1726 | - { | |
2085 | + } else if (t[0] <= 1.0) { | |
1727 | 2086 | // min on face s=0 or r=0 |
1728 | 2087 | tempSegment.setOrigin(rect.getOrigin()); |
1729 | 2088 | tempSegment.setDirection(rect.getSecondEdge()); |
@@ -1914,8 +2273,7 @@ public class Distance { | ||
1914 | 2273 | s[0] = s0[0]; |
1915 | 2274 | t[0] = t0[0]; |
1916 | 2275 | } |
1917 | - } else // region 2m | |
1918 | - { | |
2276 | + } else { | |
1919 | 2277 | // min on face s=1 or t=1 or r=0 |
1920 | 2278 | tempSegment.setOrigin( |
1921 | 2279 | rect.getOrigin().add(rect.getFirstEdge())); |
@@ -2235,8 +2593,7 @@ public class Distance { | ||
2235 | 2593 | } |
2236 | 2594 | } |
2237 | 2595 | } else if (s[0] <= 1.0) { |
2238 | - if (t[0] < 0.0) // region 7p | |
2239 | - { | |
2596 | + if (t[0] < 0.0) { | |
2240 | 2597 | // min on face t=0 or r=1 |
2241 | 2598 | tempSegment.setOrigin(rect.getOrigin()); |
2242 | 2599 | tempSegment.setOrigin(rect.getFirstEdge()); |
@@ -2257,15 +2614,13 @@ public class Distance { | ||
2257 | 2614 | s[0] = s0[0]; |
2258 | 2615 | t[0] = t0[0]; |
2259 | 2616 | } |
2260 | - } else if (t[0] <= 1.0) // region 0p | |
2261 | - { | |
2617 | + } else if (t[0] <= 1.0) { | |
2262 | 2618 | // min on face r=1 |
2263 | 2619 | point = seg.getOrigin().add(seg.getDirection()); |
2264 | 2620 | squaredDistance = |
2265 | 2621 | distancePointRectangleSquared(point, rect, s, t); |
2266 | 2622 | r[0] = 1.0f; |
2267 | - } else // region 3p | |
2268 | - { | |
2623 | + } else { | |
2269 | 2624 | // min on face t=1 or r=1 |
2270 | 2625 | tempSegment.setOrigin( |
2271 | 2626 | rect.getOrigin().add(rect.getSecondEdge())); |
@@ -2289,8 +2644,7 @@ public class Distance { | ||
2289 | 2644 | } |
2290 | 2645 | } |
2291 | 2646 | } else { |
2292 | - if (t[0] < 0.0) // region 8p | |
2293 | - { | |
2647 | + if (t[0] < 0.0) { | |
2294 | 2648 | // min on face s=1 or t=0 or r=1 |
2295 | 2649 | tempSegment.setOrigin( |
2296 | 2650 | rect.getOrigin().add(rect.getFirstEdge())); |
@@ -2327,8 +2681,7 @@ public class Distance { | ||
2327 | 2681 | s[0] = s0[0]; |
2328 | 2682 | t[0] = t0[0]; |
2329 | 2683 | } |
2330 | - } else if (t[0] <= 1.0) // region 1p | |
2331 | - { | |
2684 | + } else if (t[0] <= 1.0) { | |
2332 | 2685 | // min on face s=1 or r=1 |
2333 | 2686 | tempSegment.setOrigin( |
2334 | 2687 | rect.getOrigin().add(rect.getFirstEdge())); |
@@ -2350,8 +2703,7 @@ public class Distance { | ||
2350 | 2703 | s[0] = s0[0]; |
2351 | 2704 | t[0] = t0[0]; |
2352 | 2705 | } |
2353 | - } else // region 2p | |
2354 | - { | |
2706 | + } else { | |
2355 | 2707 | // min on face s=1 or t=1 or r=1 |
2356 | 2708 | tempSegment.setOrigin( |
2357 | 2709 | rect.getOrigin().add(rect.getFirstEdge())); |
@@ -2457,6 +2809,196 @@ public class Distance { | ||
2457 | 2809 | } |
2458 | 2810 | } |
2459 | 2811 | |
2812 | + if (rParam != null) { | |
2813 | + rParam[0] = r[0]; | |
2814 | + } | |
2815 | + | |
2816 | + if (sParam != null) { | |
2817 | + sParam[0] = s[0]; | |
2818 | + } | |
2819 | + | |
2820 | + if (tParam != null) { | |
2821 | + tParam[0] = t[0]; | |
2822 | + } | |
2823 | + | |
2460 | 2824 | return Math.abs(squaredDistance); |
2461 | 2825 | } |
2826 | + | |
2827 | + /** | |
2828 | + * <code>distanceRectangleRectangle</code> calculates the distance between | |
2829 | + * two rectangles. | |
2830 | + * @param rect1 the first rectangle to check. | |
2831 | + * @param rect2 the second rectangle to check. | |
2832 | + * @return the distance between two rectangles. | |
2833 | + */ | |
2834 | + public static float distanceRectangleRectangle( | |
2835 | + Rectangle rect1, | |
2836 | + Rectangle rect2) { | |
2837 | + return (float) Math.sqrt( | |
2838 | + distanceRectangleRectangleSquared(rect1, rect2)); | |
2839 | + } | |
2840 | + | |
2841 | + /** | |
2842 | + * <code>distanceRectangleRectangleSquared</code> calculates the distance | |
2843 | + * squared between two rectangles. | |
2844 | + * @param rect1 the first rectangle to check. | |
2845 | + * @param rect2 the second rectangle to check. | |
2846 | + * @return the distance squared between these two rectangles. | |
2847 | + */ | |
2848 | + public static float distanceRectangleRectangleSquared( | |
2849 | + Rectangle rect1, | |
2850 | + Rectangle rect2) { | |
2851 | + return distanceRectangleRectangleSquared( | |
2852 | + rect1, | |
2853 | + rect2, | |
2854 | + null, | |
2855 | + null, | |
2856 | + null, | |
2857 | + null); | |
2858 | + } | |
2859 | + | |
2860 | + /** | |
2861 | + * <code>distanceRectangleRectangleSquared</code> calculates the distance | |
2862 | + * squared between two rectangles. | |
2863 | + * @param rect1 the first rectangle to check. | |
2864 | + * @param rect2 the second rectangle to check. | |
2865 | + * @param sParam container for s where s is R1(s,t) = A + sE0 + tE1. | |
2866 | + * @param tParam container for t where t is R1(s,t) = A + sE0 + tE1. | |
2867 | + * @param uParam container for u where u is R2(u,v) = A + uE0 + vE1. | |
2868 | + * @param vParam container for v where v is R2(u,v) = A + uE0 + vE1. | |
2869 | + * @return the distance squared between these two rectangles. | |
2870 | + */ | |
2871 | + public static float distanceRectangleRectangleSquared( | |
2872 | + Rectangle rect1, | |
2873 | + Rectangle rect2, | |
2874 | + float[] sParam, | |
2875 | + float[] tParam, | |
2876 | + float[] uParam, | |
2877 | + float[] vParam) { | |
2878 | + | |
2879 | + float[] s = new float[1]; | |
2880 | + float[] t = new float[1]; | |
2881 | + float[] u = new float[1]; | |
2882 | + float[] v = new float[1]; | |
2883 | + float[] t0 = new float[1]; | |
2884 | + float[] u0 = new float[1]; | |
2885 | + float[] v0 = new float[1]; | |
2886 | + float[] s0 = new float[1]; | |
2887 | + | |
2888 | + float distanceSquared, distanceSquared0; | |
2889 | + Line segment = new Line(); | |
2890 | + | |
2891 | + // compare edges of rct0 against all of rct1 | |
2892 | + segment.setOrigin(rect1.getOrigin()); | |
2893 | + segment.setDirection(rect1.getFirstEdge()); | |
2894 | + distanceSquared = | |
2895 | + distanceSegmentRectangleSquared(segment, rect2, s, u, v); | |
2896 | + t[0] = 0.0f; | |
2897 | + | |
2898 | + segment.setDirection(rect1.getSecondEdge()); | |
2899 | + distanceSquared0 = | |
2900 | + distanceSegmentRectangleSquared(segment, rect2, t0, u0, v0); | |
2901 | + s0[0] = 0.0f; | |
2902 | + if (distanceSquared0 < distanceSquared) { | |
2903 | + distanceSquared = distanceSquared0; | |
2904 | + s[0] = s0[0]; | |
2905 | + t[0] = t0[0]; | |
2906 | + u[0] = u0[0]; | |
2907 | + v[0] = v0[0]; | |
2908 | + } | |
2909 | + | |
2910 | + segment.setOrigin(rect1.getOrigin().add(rect1.getFirstEdge())); | |
2911 | + distanceSquared0 = | |
2912 | + distanceSegmentRectangleSquared(segment, rect2, t0, u0, v0); | |
2913 | + s0[0] = 1.0f; | |
2914 | + if (distanceSquared0 < distanceSquared) { | |
2915 | + distanceSquared = distanceSquared0; | |
2916 | + s[0] = s0[0]; | |
2917 | + t[0] = t0[0]; | |
2918 | + u[0] = u0[0]; | |
2919 | + v[0] = v0[0]; | |
2920 | + } | |
2921 | + | |
2922 | + segment.setOrigin(rect1.getOrigin().add(rect1.getSecondEdge())); | |
2923 | + segment.setDirection(rect1.getFirstEdge()); | |
2924 | + distanceSquared0 = | |
2925 | + distanceSegmentRectangleSquared(segment, rect2, s0, u0, v0); | |
2926 | + t0[0] = 1.0f; | |
2927 | + if (distanceSquared0 < distanceSquared) { | |
2928 | + distanceSquared = distanceSquared0; | |
2929 | + s[0] = s0[0]; | |
2930 | + t[0] = t0[0]; | |
2931 | + u[0] = u0[0]; | |
2932 | + v[0] = v0[0]; | |
2933 | + } | |
2934 | + | |
2935 | + // compare edges of pgm1 against all of pgm0 | |
2936 | + segment.setOrigin(rect2.getOrigin()); | |
2937 | + segment.setDirection(rect2.getFirstEdge()); | |
2938 | + distanceSquared0 = | |
2939 | + distanceSegmentRectangleSquared(segment, rect1, u0, s0, t0); | |
2940 | + v0[0] = 0.0f; | |
2941 | + if (distanceSquared0 < distanceSquared) { | |
2942 | + distanceSquared = distanceSquared0; | |
2943 | + s[0] = s0[0]; | |
2944 | + t[0] = t0[0]; | |
2945 | + u[0] = u0[0]; | |
2946 | + v[0] = v0[0]; | |
2947 | + } | |
2948 | + | |
2949 | + segment.setDirection(rect2.getSecondEdge()); | |
2950 | + distanceSquared0 = | |
2951 | + distanceSegmentRectangleSquared(segment, rect1, v0, s0, t0); | |
2952 | + u0[0] = 0.0f; | |
2953 | + if (distanceSquared0 < distanceSquared) { | |
2954 | + distanceSquared = distanceSquared0; | |
2955 | + s[0] = s0[0]; | |
2956 | + t[0] = t0[0]; | |
2957 | + u[0] = u0[0]; | |
2958 | + v[0] = v0[0]; | |
2959 | + } | |
2960 | + | |
2961 | + segment.setOrigin(rect2.getOrigin().add(rect2.getFirstEdge())); | |
2962 | + distanceSquared0 = | |
2963 | + distanceSegmentRectangleSquared(segment, rect1, v0, s0, t0); | |
2964 | + u0[0] = 1.0f; | |
2965 | + if (distanceSquared0 < distanceSquared) { | |
2966 | + distanceSquared = distanceSquared0; | |
2967 | + s[0] = s0[0]; | |
2968 | + t[0] = t0[0]; | |
2969 | + u[0] = u0[0]; | |
2970 | + v[0] = v0[0]; | |
2971 | + } | |
2972 | + | |
2973 | + segment.setOrigin(rect2.getOrigin().add(rect2.getSecondEdge())); | |
2974 | + segment.setDirection(rect2.getFirstEdge()); | |
2975 | + distanceSquared0 = | |
2976 | + distanceSegmentRectangleSquared(segment, rect1, u0, s0, t0); | |
2977 | + v0[0] = 1.0f; | |
2978 | + if (distanceSquared0 < distanceSquared) { | |
2979 | + distanceSquared = distanceSquared0; | |
2980 | + s[0] = s0[0]; | |
2981 | + t[0] = t0[0]; | |
2982 | + u[0] = u0[0]; | |
2983 | + v[0] = v0[0]; | |
2984 | + } | |
2985 | + | |
2986 | + if (sParam != null) { | |
2987 | + sParam[0] = s[0]; | |
2988 | + } | |
2989 | + | |
2990 | + if (tParam != null) { | |
2991 | + tParam[0] = t[0]; | |
2992 | + } | |
2993 | + | |
2994 | + if (uParam != null) { | |
2995 | + uParam[0] = u[0]; | |
2996 | + } | |
2997 | + | |
2998 | + if (vParam != null) { | |
2999 | + vParam[0] = v[0]; | |
3000 | + } | |
3001 | + | |
3002 | + return Math.abs(distanceSquared); | |
3003 | + } | |
2462 | 3004 | } |