示例#1
0
        public void TestBarycentricCoordinates()
        {
            Vector2 a = new Vector2(0, -0.5);
            Vector2 b = new Vector2(-0.5, 0.5);
            Vector2 c = new Vector2(0.5, 0.5);

            Vector2 point = new Vector2(0, 0);

            double t, u, v;

            // Check that barycentric coordinates are all between 0..1
            GeometryMath.GetBarycentricCoordinates(point, a, b, c, out t, out u, out v);
            Assert.IsTrue(t >= 0.0 && t <= 1.0 && u >= 0.0 && u <= 1.0 && v >= 0.0 && v <= 1.0, "Expected t, u, v to be within 0..1 (point in middle of triangle)");

            point = new Vector2(0, -0.5);
            // Check that barycentric coordinates are 0, 0, 1 as point is exactly where with one of the triangle points
            GeometryMath.GetBarycentricCoordinates(point, a, b, c, out t, out u, out v);
            Assert.IsTrue(t == 1.0 && u == 0.0 && v == 0.0, "Expected t = 0, u = 0, v = 1 (point in the same place as one of the triangle points)");

            point = new Vector2(-0.25, 0);
            // Check that one of the barycentric coordinates is 0 as point is on one of the triangle edges
            GeometryMath.GetBarycentricCoordinates(point, a, b, c, out t, out u, out v);
            Assert.IsTrue(v == 0, "Expected one of the (t, u, v) be 0 (point is in one of the triangle edges)");

            point = new Vector2(0, -0.51);
            // Check that barycentric coordinates are not between 0..1
            GeometryMath.GetBarycentricCoordinates(point, a, b, c, out t, out u, out v);
            Assert.IsFalse(t >= 0.0 && t <= 1.0 && u >= 0.0 && u <= 1.0 && v >= 0.0 && v <= 1.0, "Expected one of the (t, u, v) be outside 0..1 (point is outside of the triangle)");
        }