00001 namespace Tests
00002 {
00003 using Microsoft.VisualStudio.TestTools.UnitTesting;
00004 using Microsoft.Xna.Framework;
00005 using NewGamePhysics.Utilities;
00006
00010 [TestClass]
00011 public class UnitTestIntersection
00012 {
00013 public UnitTestIntersection()
00014 {
00015
00016
00017
00018 }
00019
00020 private TestContext testContextInstance;
00021
00026 public TestContext TestContext
00027 {
00028 get
00029 {
00030 return testContextInstance;
00031 }
00032 set
00033 {
00034 testContextInstance = value;
00035 }
00036 }
00037
00038 #region Additional test attributes
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #endregion
00059
00060 [TestMethod]
00061 public void TestCircleInCircle2D()
00062 {
00063
00064 float[,] Tp = {
00065 {0.0f, 0.0f, 1.0f},
00066 {0.5f, 0.5f, 1.0f},
00067
00068 {0.0f, 0.0f, 1.0f},
00069 {1.0f, 1.0f, 1.0f},
00070
00071 {0.0f, 0.0f, 1.0f},
00072 {0.0f, 0.0f, 0.1f},
00073
00074 {0.0f, 0.0f, 1.0f},
00075 {2.0f, 0.0f, 1.0f},
00076
00077 {0.0f, 0.0f, 1.0f},
00078 {0.0f, 1.0f, 0.0f},
00079
00080 {0.0f, 0.0f, 1.0f},
00081 {1.0f, 0.0f, 0.0f},
00082
00083 {0.0f, 0.0f, 1.0f},
00084 {0.0f, 0.0f, 0.0f},
00085
00086 {0.0f, 0.0f, 0.0f},
00087 {0.0f, 0.0f, 0.0f},
00088 };
00089
00090
00091 float[,] Tn = {
00092 {0.0f, 0.0f, 1.0f},
00093 {2.0f, 2.0f, 1.0f},
00094
00095 {0.0f, 0.0f, 1.0f},
00096 {2.0f, 2.0f, 0.0f},
00097
00098 {0.0f, 0.0f, -1.0f},
00099 {1.0f, 1.0f, 1.0f},
00100
00101 {0.0f, 0.0f, 1.0f},
00102 {1.0f, 1.0f, -1.0f},
00103
00104 {0.0f, 0.0f, -1.0f},
00105 {1.0f, 1.0f, -1.0f},
00106
00107 };
00108
00109 Vector2 center1;
00110 float radius1;
00111 Vector2 center2;
00112 float radius2;
00113 bool result;
00114 for (int i = 0; i < Tp.Length / 3; i += 2)
00115 {
00116 center1 = new Vector2(Tp[i + 0, 0], Tp[i + 0, 1]);
00117 radius1 = Tp[i + 0, 2];
00118 center2 = new Vector2(Tp[i + 1, 0], Tp[i + 1, 1]);
00119 radius2 = Tp[i + 1, 2];
00120 result = IntersectionTest.CircleInCircle2D(center1, radius1, center2, radius2);
00121 Assert.IsTrue(result);
00122 result = IntersectionTest.CircleInCircle2D(center2, radius2, center1, radius1);
00123 Assert.IsTrue(result);
00124 }
00125
00126 for (int i = 0; i < Tn.Length / 3; i += 2)
00127 {
00128 center1 = new Vector2(Tn[i + 0, 0], Tn[i + 0, 1]);
00129 radius1 = Tn[i + 0, 2];
00130 center2 = new Vector2(Tn[i + 1, 0], Tn[i + 1, 1]);
00131 radius2 = Tn[i + 1, 2];
00132 result = IntersectionTest.CircleInCircle2D(center1, radius1, center2, radius2);
00133 Assert.IsFalse(result);
00134 result = IntersectionTest.CircleInCircle2D(center2, radius2, center1, radius1);
00135 Assert.IsFalse(result);
00136 }
00137 }
00138
00139 [TestMethod]
00140 public void TestPointInPolygon2D()
00141 {
00142
00143 float[,] Tp = {
00144 {0.0f, 0.0f},
00145 {1.0f, 0.0f},
00146 {0.0f, 1.0f},
00147 {0.1f, 0.1f},
00148
00149 {0.0f, 0.0f},
00150 {1.0f, 0.0f},
00151 {0.0f, 1.0f},
00152 {0.0f, 0.0f},
00153 };
00154
00155
00156 float[,] Tn = {
00157 {0.0f, 0.0f},
00158 {1.0f, 0.0f},
00159 {0.0f, 1.0f},
00160 {-0.1f, -0.1f},
00161
00162 {0.0f, 0.0f},
00163 {1.0f, 0.0f},
00164 {0.0f, 1.0f},
00165 {1.0f, 1.0f},
00166
00167 {0.0f, 0.0f},
00168 {1.0f, 0.0f},
00169 {0.0f, 1.0f},
00170 {0.0f, 2.0f},
00171 };
00172
00173 Vector2[] polygon = new Vector2[3];
00174 Vector2 test;
00175 bool result;
00176 for (int i = 0; i < Tp.Length / 2; i += 4)
00177 {
00178
00179 for (int p = 0; p < 3; p++)
00180 {
00181 for (int j = 0; j < 3; j++)
00182 {
00183 int x = (j + p) % 3;
00184 polygon[j] = new Vector2(Tp[i + x, 0], Tp[i + x, 1]);
00185 }
00186
00187 test = new Vector2(Tp[i + 3, 0], Tp[i + 3, 1]);
00188 result = IntersectionTest.PointInPolygon2D(polygon, test);
00189 Assert.IsTrue(result);
00190 }
00191 }
00192
00193 for (int i = 0; i < Tn.Length / 2; i += 4)
00194 {
00195
00196 for (int p = 0; p < 3; p++)
00197 {
00198 for (int j = 0; j < 3; j++)
00199 {
00200 int x = (j + p) % 3;
00201 polygon[j] = new Vector2(Tn[i + x, 0], Tn[i + x, 1]);
00202 }
00203
00204 test = new Vector2(Tn[i + 3, 0], Tn[i + 3, 1]);
00205 result = IntersectionTest.PointInPolygon2D(polygon, test);
00206 Assert.IsFalse(result);
00207 }
00208 }
00209 }
00210
00211 [TestMethod]
00212 public void TestTriangleTriangleIntersect2D()
00213 {
00214
00215 float[,] Tp = {
00216 {0.0f, 0.0f},
00217 {1.0f, 0.0f},
00218 {0.0f, 1.0f},
00219 {0.0f, 0.0f},
00220 {1.0f, 0.0f},
00221 {0.0f, 1.0f},
00222
00223 {0.0f, 0.0f},
00224 {1.0f, 0.0f},
00225 {0.0f, 1.0f},
00226 {0.1f, 0.1f},
00227 {1.1f, 0.1f},
00228 {0.1f, 1.1f},
00229
00230 {0.0f, 0.0f},
00231 {1.0f, 0.0f},
00232 {0.0f, 1.0f},
00233 {0.0f, 0.0f},
00234 {-1.0f, 0.0f},
00235 {0.0f, -1.0f},
00236
00237 {-0.1f, 0.0f},
00238 {0.1f, 0.0f},
00239 {0.0f, 1.0f},
00240 {-0.5f, 0.4f},
00241 {-0.5f, 0.6f},
00242 {0.5f, 0.5f},
00243 };
00244
00245
00246 float[,] Tn = {
00247 {0.0f, 0.0f},
00248 {1.0f, 0.0f},
00249 {0.0f, 1.0f},
00250 {1.0f, 1.0f},
00251 {2.0f, 1.0f},
00252 {1.0f, 2.0f},
00253
00254 {0.0f, 0.0f},
00255 {1.0f, 0.0f},
00256 {0.0f, 1.0f},
00257 {0.6f, 0.6f},
00258 {1.6f, 0.6f},
00259 {0.6f, 1.6f},
00260
00261 {0.0f, 0.0f},
00262 {1.0f, 0.0f},
00263 {0.0f, 1.0f},
00264 {0.0f, 1.1f},
00265 {1.1f, 0.0f},
00266 {3.0f, 3.0f},
00267 };
00268 for (int i = 0; i < Tp.Length / 2; i += 6)
00269 {
00270 Vector2 p1 = new Vector2(Tp[i + 0, 0], Tp[i + 0, 1]);
00271 Vector2 q1 = new Vector2(Tp[i + 1, 0], Tp[i + 1, 1]);
00272 Vector2 r1 = new Vector2(Tp[i + 2, 0], Tp[i + 2, 1]);
00273 Vector2 p2 = new Vector2(Tp[i + 3, 0], Tp[i + 3, 1]);
00274 Vector2 q2 = new Vector2(Tp[i + 4, 0], Tp[i + 4, 1]);
00275 Vector2 r2 = new Vector2(Tp[i + 5, 0], Tp[i + 5, 1]);
00276 bool result;
00277 result = IntersectionTest.TriangleTriangleIntersect2D(p1, q1, r1, p2, q2, r2);
00278 Assert.IsTrue(result);
00279 result = IntersectionTest.TriangleTriangleIntersect2D(r1, p1, q1, p2, r2, q2);
00280 Assert.IsTrue(result);
00281 result = IntersectionTest.TriangleTriangleIntersect2D(q1, p1, r1, q2, r2, p2);
00282 Assert.IsTrue(result);
00283 }
00284
00285 for (int i = 0; i < Tn.Length / 2; i += 6)
00286 {
00287 Vector2 p1 = new Vector2(Tn[i + 0, 0], Tn[i + 0, 1]);
00288 Vector2 q1 = new Vector2(Tn[i + 1, 0], Tn[i + 1, 1]);
00289 Vector2 r1 = new Vector2(Tn[i + 2, 0], Tn[i + 2, 1]);
00290 Vector2 p2 = new Vector2(Tn[i + 3, 0], Tn[i + 3, 1]);
00291 Vector2 q2 = new Vector2(Tn[i + 4, 0], Tn[i + 4, 1]);
00292 Vector2 r2 = new Vector2(Tn[i + 5, 0], Tn[i + 5, 1]);
00293 bool result;
00294 result = IntersectionTest.TriangleTriangleIntersect2D(p1, q1, r1, p2, q2, r2);
00295 Assert.IsFalse(result);
00296 result = IntersectionTest.TriangleTriangleIntersect2D(r1, p1, q1, p2, r2, q2);
00297 Assert.IsFalse(result);
00298 result = IntersectionTest.TriangleTriangleIntersect2D(q1, p1, r1, q2, r2, p2);
00299 Assert.IsFalse(result);
00300 }
00301 }
00302 }
00303 }