• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Main repository of MikuMikuStudio


Commit MetaInfo

Revision83be495582060c369e8e0cf516da19c603f13176 (tree)
Time2003-09-17 07:22:32
Authormojomonkey <mojomonkey@75d0...>
Commitermojomonkey

Log Message

Finished Distance class.

git-svn-id: http://jmonkeyengine.googlecode.com/svn/trunk@91 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

Change Summary

Incremental Difference

--- a/src/jme/math/Distance.java
+++ b/src/jme/math/Distance.java
@@ -33,7 +33,10 @@ package jme.math;
3333
3434 /**
3535 * <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.
3740 *
3841 * <br><br>
3942 * <b>NOTE:</b> See 3D Game Engine Design. David H. Eberly.
@@ -75,22 +78,64 @@ public class Distance {
7578 }
7679
7780 /**
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+ /**
78103 * <code>distancePointLineSquared</code> calculates the distance squared
79104 * between a point and a line.
80105 * @param point the point to check.
81106 * @param line the line to check.
107+ * @param lineParam container for t where L(t) = B + tM.
82108 * @return the distance squared between a point and line.
83109 */
84- public static float distancePointLineSquared(Vector point, Line line) {
110+ public static float distancePointLineSquared(
111+ Vector point,
112+ Line line,
113+ float[] lineParam) {
114+
85115 Vector diff = point.subtract(line.getOrigin());
86116 float squareLen = line.getDirection().lengthSquared();
87117 float t = diff.dot(line.getDirection()) / squareLen;
88118 diff = diff.subtract(line.getDirection().mult(t));
89119
120+ if (lineParam != null) {
121+ lineParam[0] = t;
122+ }
123+
90124 return diff.lengthSquared();
91125 }
92126
93127 /**
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+ /**
94139 * <code>distancePointRaySquared</code> calculates the distance
95140 * squared between a point and a ray.
96141 * @param point the point to check.
@@ -98,6 +143,22 @@ public class Distance {
98143 * @return the distance between a point and ray.
99144 */
100145 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+
101162 Vector diff = point.subtract(ray.getOrigin());
102163 float t = diff.dot(ray.getDirection());
103164
@@ -108,10 +169,25 @@ public class Distance {
108169 diff = diff.subtract(ray.getDirection().mult(t));
109170 }
110171
172+ if (rayParam != null) {
173+ rayParam[0] = t;
174+ }
175+
111176 return diff.lengthSquared();
112177 }
113178
114179 /**
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+ /**
115191 * <code>distancePointSegmentSquared</code> calculates the distance
116192 * squared between a point and a line segment.
117193 * @param point the point to check.
@@ -119,6 +195,22 @@ public class Distance {
119195 * @return the distance squared between a point and line segment.
120196 */
121197 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+
122214 Vector diff = point.subtract(seg.getOrigin());
123215 float t = diff.dot(seg.getDirection());
124216
@@ -139,6 +231,17 @@ public class Distance {
139231 }
140232
141233 /**
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+ /**
142245 * <code>distancePointRectangleSquared</code> calculates the distance squared
143246 * between a point and a rectangle.
144247 * @param point the point to check.
@@ -148,6 +251,7 @@ public class Distance {
148251 public static float distancePointRectangleSquared(
149252 Vector point,
150253 Rectangle rect) {
254+
151255 return distancePointRectangleSquared(point, rect, null, null);
152256 }
153257
@@ -156,13 +260,15 @@ public class Distance {
156260 * between a point and a rectangle.
157261 * @param point the point to check.
158262 * @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|.
159265 * @return the distance between the point and the rectangle.
160266 */
161267 public static float distancePointRectangleSquared(
162268 Vector point,
163269 Rectangle rect,
164- float[] pointParam,
165- float[] rectParam) {
270+ float[] sParam,
271+ float[] tParam) {
166272 Vector diff = rect.getOrigin().subtract(point);
167273 float a00 = rect.getFirstEdge().lengthSquared();
168274 float a11 = rect.getSecondEdge().lengthSquared();
@@ -192,15 +298,24 @@ public class Distance {
192298 t = 1.0f;
193299 distanceSquared += a11 + 2.0 * b1;
194300 }
195- if (pointParam != null)
196- pointParam[0] = s;
301+ if (sParam != null)
302+ sParam[0] = s;
197303
198- if (rectParam != null)
199- rectParam[0] = t;
304+ if (tParam != null)
305+ tParam[0] = t;
200306 return Math.abs(distanceSquared);
201307 }
202308
203309 /**
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+ /**
204319 * <code>distanceLineLineSquared</code> calculates the distance squared
205320 * between two lines.
206321 * @param line1 the first line to check.
@@ -208,6 +323,22 @@ public class Distance {
208323 * @return the distance squared between two lines.
209324 */
210325 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) {
211342 Vector diff = line1.getOrigin().subtract(line2.getOrigin());
212343 float a = line1.getDirection().lengthSquared();
213344 float b = -line1.getDirection().dot(line2.getDirection());
@@ -237,10 +368,29 @@ public class Distance {
237368 distanceSquared = d * s + f;
238369 }
239370
371+ if (tParam != null) {
372+ tParam[0] = t;
373+ }
374+
375+ if (sParam != null) {
376+ sParam[0] = s;
377+ }
378+
240379 return Math.abs(distanceSquared);
241380 }
242381
243382 /**
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+ /**
244394 * <code>distanceLineRaySquared</code> calculates the squared distance
245395 * between a line and a ray.
246396 * @param line the line to check.
@@ -248,6 +398,23 @@ public class Distance {
248398 * @return the distance between the line and the ray.
249399 */
250400 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) {
251418 Vector diff = line.getOrigin().subtract(ray.getOrigin());
252419 float a = line.getDirection().lengthSquared();
253420 float b = -line.getDirection().dot(ray.getDirection());
@@ -286,10 +453,29 @@ public class Distance {
286453 distanceSquared = d * s + f;
287454 }
288455
456+ if (tParam != null) {
457+ tParam[0] = t;
458+ }
459+
460+ if (sParam != null) {
461+ sParam[0] = s;
462+ }
463+
289464 return Math.abs(distanceSquared);
290465 }
291466
292467 /**
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+ /**
293479 * <code>distanceLineSegementSquared</code> calculates the distance
294480 * squared between a line and a line segment.
295481 * @param line the line to check.
@@ -305,15 +491,15 @@ public class Distance {
305491 * squared between a line and a line segment.
306492 * @param line the line to check.
307493 * @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.
310496 * @return the distance squared between a line and a line segment.
311497 */
312498 public static float distanceLineSegmentSquared(
313499 Line line,
314500 Line seg,
315- float[] lineParam,
316- float[] segParam) {
501+ float[] sParam,
502+ float[] tParam) {
317503
318504 Vector diff = line.getOrigin().subtract(seg.getOrigin());
319505 float a = line.getDirection().lengthSquared();
@@ -362,17 +548,28 @@ public class Distance {
362548 t = 0.0f;
363549 squareDistance = d * s + f;
364550 }
365- if (null != lineParam) {
366- lineParam[0] = s;
551+ if (null != sParam) {
552+ sParam[0] = s;
367553 }
368- if (null != segParam) {
369- segParam[0] = t;
554+ if (null != tParam) {
555+ tParam[0] = t;
370556 }
371557
372558 return Math.abs(squareDistance);
373559 }
374560
375561 /**
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+ /**
376573 * <code>distanceLineRectangleSquared</code> calculates the distance squared
377574 * between a line and a rectangle.
378575 * @param line the line to check.
@@ -382,6 +579,27 @@ public class Distance {
382579 public static float distanceLineRectangleSquared(
383580 Line line,
384581 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) {
385603
386604 float[] r = new float[1];
387605 float[] s = new float[1];
@@ -582,17 +800,56 @@ public class Distance {
582800 }
583801 }
584802
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+
585815 return Math.abs(distanceSquared);
586816 }
587817
588818 /**
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+ /**
589840 * <code>distanceRayRaySquared</code> calculates the distance squared
590841 * between two rays.
591842 * @param ray1 the first ray to check.
592843 * @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.
593846 * @return the distance squared between the two rays.
594847 */
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) {
596853 Vector diff = ray1.getOrigin().subtract(ray2.getOrigin());
597854 float a = ray1.getDirection().lengthSquared();
598855 float b = -ray1.getDirection().dot(ray2.getDirection());
@@ -612,8 +869,7 @@ public class Distance {
612869 t = b * d - a * e;
613870
614871 if (s >= 0.0) {
615- if (t >= 0.0) // region 0 (interior)
616- {
872+ if (t >= 0.0) {
617873 // minimum at two interior points of rays
618874 float inverseDeterminate = 1.0f / determinate;
619875 s *= inverseDeterminate;
@@ -622,8 +878,7 @@ public class Distance {
622878 s * (a * s + b * t + 2.0f * d)
623879 + t * (b * s + c * t + 2.0f * e)
624880 + f;
625- } else // region 3 (side)
626- {
881+ } else {
627882 t = 0.0f;
628883 if (d >= 0.0) {
629884 s = 0.0f;
@@ -634,8 +889,7 @@ public class Distance {
634889 }
635890 }
636891 } else {
637- if (t >= 0.0) // region 1 (side)
638- {
892+ if (t >= 0.0) {
639893 s = 0.0f;
640894 if (e >= 0.0) {
641895 t = 0.0f;
@@ -644,8 +898,7 @@ public class Distance {
644898 t = -e / c;
645899 distanceSquared = e * t + f;
646900 }
647- } else // region 2 (corner)
648- {
901+ } else {
649902 if (d < 0.0) {
650903 s = -d / a;
651904 t = 0.0f;
@@ -689,10 +942,29 @@ public class Distance {
689942 }
690943 }
691944
945+ if (sParam != null) {
946+ sParam[0] = s;
947+ }
948+
949+ if (tParam != null) {
950+ tParam[0] = t;
951+ }
952+
692953 return Math.abs(distanceSquared);
693954 }
694955
695956 /**
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+ /**
696968 * <code>distanceRaySegmentSquared</code> calculates the distance
697969 * squared between a ray and a line segment.
698970 * @param ray the ray to check.
@@ -708,13 +980,15 @@ public class Distance {
708980 * squared between a ray and a line segment.
709981 * @param ray the ray to check.
710982 * @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.
711985 * @return the distance between the ray and the line segment.
712986 */
713987 public static float distanceRaySegmentSquared(
714988 Line ray,
715989 Line seg,
716- float[] rayParam,
717- float[] segParam) {
990+ float[] sParam,
991+ float[] tParam) {
718992
719993 Vector diff = ray.getOrigin().subtract(seg.getOrigin());
720994 float a = ray.getDirection().lengthSquared();
@@ -852,16 +1126,29 @@ public class Distance {
8521126 }
8531127 }
8541128
855- if (rayParam != null)
856- rayParam[0] = s;
1129+ if (sParam != null) {
1130+ sParam[0] = s;
1131+ }
8571132
858- if (segParam != null)
859- segParam[0] = t;
1133+ if (tParam != null) {
1134+ tParam[0] = t;
1135+ }
8601136
8611137 return Math.abs(distanceSquared);
8621138 }
8631139
8641140 /**
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+ /**
8651152 * <code>distanceRayRectangleSquared</code> calculates the distance
8661153 * squared between a ray and a rectangle.
8671154 * @param ray the ray to check.
@@ -869,6 +1156,28 @@ public class Distance {
8691156 * @return the distance squared between a ray and a rectangle.
8701157 */
8711158 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) {
8721181 Vector diff = rect.getOrigin().subtract(ray.getOrigin());
8731182 float a00 = ray.getDirection().lengthSquared();
8741183 float a01 = -ray.getDirection().dot(rect.getFirstEdge());
@@ -1349,15 +1658,38 @@ public class Distance {
13491658 }
13501659 }
13511660
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+
13521673 return Math.abs(squaredDistance);
13531674 }
1675+
13541676 /**
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+ */
13611693 public static float distanceSegmentSegmentSquared(Line seg1, Line seg2) {
13621694 return distanceSegmentSegmentSquared(seg1, seg2, null, null);
13631695 }
@@ -1367,13 +1699,15 @@ public class Distance {
13671699 * squared between two line segments.
13681700 * @param seg1 the first line segment to check.
13691701 * @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.
13711705 */
13721706 public static float distanceSegmentSegmentSquared(
13731707 Line seg1,
13741708 Line seg2,
1375- float[] seg1Param,
1376- float[] seg2Param) {
1709+ float[] sParam,
1710+ float[] tParam) {
13771711 Vector diff = seg1.getOrigin().subtract(seg2.getOrigin());
13781712 float a = seg1.getDirection().lengthSquared();
13791713 float b = -seg1.getDirection().dot(seg2.getDirection());
@@ -1396,8 +1730,7 @@ public class Distance {
13961730 if (s >= 0.0) {
13971731 if (s <= determinate) {
13981732 if (t >= 0.0) {
1399- if (t <= determinate) // region 0 (interior)
1400- {
1733+ if (t <= determinate) {
14011734 // minimum at two interior points of 3D lines
14021735 float inverseDeterminate = 1.0f / determinate;
14031736 s *= inverseDeterminate;
@@ -1406,8 +1739,7 @@ public class Distance {
14061739 s * (a * s + b * t + 2.0f * d)
14071740 + t * (b * s + c * t + 2.0f * e)
14081741 + f;
1409- } else // region 3 (side)
1410- {
1742+ } else {
14111743 t = 1.0f;
14121744 temp = b + d;
14131745 if (temp >= 0.0f) {
@@ -1421,8 +1753,7 @@ public class Distance {
14211753 distanceSquared = temp * s + c + 2.0f * e + f;
14221754 }
14231755 }
1424- } else // region 7 (side)
1425- {
1756+ } else {
14261757 t = 0.0f;
14271758 if (d >= 0.0f) {
14281759 s = 0.0f;
@@ -1437,8 +1768,7 @@ public class Distance {
14371768 }
14381769 } else {
14391770 if (t >= 0.0f) {
1440- if (t <= determinate) // region 1 (side)
1441- {
1771+ if (t <= determinate) {
14421772 s = 1.0f;
14431773 temp = b + e;
14441774 if (temp >= 0.0f) {
@@ -1451,8 +1781,7 @@ public class Distance {
14511781 t = -temp / c;
14521782 distanceSquared = temp * t + a + 2.0f * d + f;
14531783 }
1454- } else // region 2 (corner)
1455- {
1784+ } else {
14561785 temp = b + d;
14571786 if (-temp <= a) {
14581787 t = 1.0f;
@@ -1481,8 +1810,7 @@ public class Distance {
14811810 }
14821811 }
14831812 }
1484- } else // region 8 (corner)
1485- {
1813+ } else {
14861814 if (-d < a) {
14871815 t = 0.0f;
14881816 if (d >= 0.0f) {
@@ -1510,8 +1838,7 @@ public class Distance {
15101838 }
15111839 } else {
15121840 if (t >= 0.0f) {
1513- if (t <= determinate) // region 5 (side)
1514- {
1841+ if (t <= determinate) {
15151842 s = 0.0f;
15161843 if (e >= 0.0f) {
15171844 t = 0.0f;
@@ -1523,8 +1850,7 @@ public class Distance {
15231850 t = -e / c;
15241851 distanceSquared = e * t + f;
15251852 }
1526- } else // region 4 (corner)
1527- {
1853+ } else {
15281854 temp = b + d;
15291855 if (temp < 0.0f) {
15301856 t = 1.0f;
@@ -1549,8 +1875,7 @@ public class Distance {
15491875 }
15501876 }
15511877 }
1552- } else // region 6 (corner)
1553- {
1878+ } else {
15541879 if (d < 0.0f) {
15551880 t = 0.0f;
15561881 if (-d >= a) {
@@ -1624,16 +1949,29 @@ public class Distance {
16241949 }
16251950 }
16261951
1627- if (seg1Param != null)
1628- seg1Param[0] = s;
1952+ if (sParam != null) {
1953+ sParam[0] = s;
1954+ }
16291955
1630- if (seg2Param != null)
1631- seg2Param[0] = t;
1956+ if (tParam != null) {
1957+ tParam[0] = t;
1958+ }
16321959
16331960 return Math.abs(distanceSquared);
16341961 }
16351962
16361963 /**
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+ /**
16371975 * <code>distanceSegmentRectangleSquared</code> calculates the distance
16381976 * squared between a line segment and a rectangle.
16391977 * @param seg the line segment to check.
@@ -1643,6 +1981,28 @@ public class Distance {
16431981 public static float distanceSegmentRectangleSquared(
16441982 Line seg,
16451983 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) {
16462006
16472007 Vector diff = rect.getOrigin().subtract(seg.getOrigin());
16482008 float a00 = seg.getDirection().lengthSquared();
@@ -1722,8 +2082,7 @@ public class Distance {
17222082 s[0] = s0[0];
17232083 t[0] = t0[0];
17242084 }
1725- } else if (t[0] <= 1.0) // region 5m
1726- {
2085+ } else if (t[0] <= 1.0) {
17272086 // min on face s=0 or r=0
17282087 tempSegment.setOrigin(rect.getOrigin());
17292088 tempSegment.setDirection(rect.getSecondEdge());
@@ -1914,8 +2273,7 @@ public class Distance {
19142273 s[0] = s0[0];
19152274 t[0] = t0[0];
19162275 }
1917- } else // region 2m
1918- {
2276+ } else {
19192277 // min on face s=1 or t=1 or r=0
19202278 tempSegment.setOrigin(
19212279 rect.getOrigin().add(rect.getFirstEdge()));
@@ -2235,8 +2593,7 @@ public class Distance {
22352593 }
22362594 }
22372595 } else if (s[0] <= 1.0) {
2238- if (t[0] < 0.0) // region 7p
2239- {
2596+ if (t[0] < 0.0) {
22402597 // min on face t=0 or r=1
22412598 tempSegment.setOrigin(rect.getOrigin());
22422599 tempSegment.setOrigin(rect.getFirstEdge());
@@ -2257,15 +2614,13 @@ public class Distance {
22572614 s[0] = s0[0];
22582615 t[0] = t0[0];
22592616 }
2260- } else if (t[0] <= 1.0) // region 0p
2261- {
2617+ } else if (t[0] <= 1.0) {
22622618 // min on face r=1
22632619 point = seg.getOrigin().add(seg.getDirection());
22642620 squaredDistance =
22652621 distancePointRectangleSquared(point, rect, s, t);
22662622 r[0] = 1.0f;
2267- } else // region 3p
2268- {
2623+ } else {
22692624 // min on face t=1 or r=1
22702625 tempSegment.setOrigin(
22712626 rect.getOrigin().add(rect.getSecondEdge()));
@@ -2289,8 +2644,7 @@ public class Distance {
22892644 }
22902645 }
22912646 } else {
2292- if (t[0] < 0.0) // region 8p
2293- {
2647+ if (t[0] < 0.0) {
22942648 // min on face s=1 or t=0 or r=1
22952649 tempSegment.setOrigin(
22962650 rect.getOrigin().add(rect.getFirstEdge()));
@@ -2327,8 +2681,7 @@ public class Distance {
23272681 s[0] = s0[0];
23282682 t[0] = t0[0];
23292683 }
2330- } else if (t[0] <= 1.0) // region 1p
2331- {
2684+ } else if (t[0] <= 1.0) {
23322685 // min on face s=1 or r=1
23332686 tempSegment.setOrigin(
23342687 rect.getOrigin().add(rect.getFirstEdge()));
@@ -2350,8 +2703,7 @@ public class Distance {
23502703 s[0] = s0[0];
23512704 t[0] = t0[0];
23522705 }
2353- } else // region 2p
2354- {
2706+ } else {
23552707 // min on face s=1 or t=1 or r=1
23562708 tempSegment.setOrigin(
23572709 rect.getOrigin().add(rect.getFirstEdge()));
@@ -2457,6 +2809,196 @@ public class Distance {
24572809 }
24582810 }
24592811
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+
24602824 return Math.abs(squaredDistance);
24612825 }
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+ }
24623004 }