Beispiel #1
0
        public void TestComplexAreaAlgorithmWithComplexPolygonNoBufferOverrun()
        {
            // the complex area algorithm is very low resolution compared to simple area, so the error magnitude is higher
            const int side     = 1000;
            const int expected = side * side / 2;

            PointF[] data = new PointF[5];
            data[0] = new PointF(0, 0);
            data[1] = new PointF(side, 0);
            data[2] = new PointF(0, side);
            data[3] = new PointF(side, side);
            data[4] = new PointF(side / 2f, side / 2f);

            PolygonF.TestVertexNormalization(data);

            RectangleF bounding = RectangleUtilities.ComputeBoundingRectangle(data);

            // test for a possible buffer overrun in the computation
            foreach (PointF garbagePoint in SimulatedBufferOverrunPoints)
            {
                data[data.Length - 1] = garbagePoint;
                unsafe
                {
                    fixed(PointF *points = data)
                    {
                        // inside PolygonF, the vertexCount is always exactly the expected array length
                        // which is 4 (the 5th point simulates garbage data beyond the array)
                        double result = PolygonF.TestComplexAreaComputation(bounding, points, data.Length - 1);

                        Trace.WriteLine(string.Format("Complex Area: Expected {0:f5} Got {1:f5}", expected, result), "UNIT_TESTS");
                        Assert.IsTrue(Math.Abs(expected - result) < expected / 100f);
                    }
                }
            }
        }
Beispiel #2
0
        public void TestComplexAreaAlgorithmWithComplexPolygon()
        {
            // the complex area algorithm is very low resolution compared to simple area, so the error magnitude is higher
            const int side     = 1000;
            const int expected = side * side / 2;

            PointF[] data = new PointF[4];
            data[0] = new PointF(0, 0);
            data[1] = new PointF(side, 0);
            data[2] = new PointF(0, side);
            data[3] = new PointF(side, side);

            PolygonF.TestVertexNormalization(data);

            RectangleF bounding = RectangleUtilities.ComputeBoundingRectangle(data);

            unsafe
            {
                fixed(PointF *points = data)
                {
                    // inside PolygonF, the vertexCount is always exactly the expected array length
                    double result = PolygonF.TestComplexAreaComputation(bounding, points, data.Length);

                    Trace.WriteLine(string.Format("Complex Area: Expected {0:f5} Got {1:f5}", expected, result), "UNIT_TESTS");
                    Assert.IsTrue(Math.Abs(expected - result) < expected / 100f);
                }
            }
        }
Beispiel #3
0
        public void TestSimpleAreaAlgorithmNoBufferOverrun()
        {
            PointF[] data = new PointF[5];
            data[0] = new PointF(0, 0);
            data[1] = new PointF(1, 0);
            data[2] = new PointF(1, 1);
            data[3] = new PointF(0, 1);

            PolygonF.TestVertexNormalization(data);

            // test for a possible buffer overrun in the computation
            foreach (PointF garbagePoint in SimulatedBufferOverrunPoints)
            {
                data[data.Length - 1] = garbagePoint;
                unsafe
                {
                    fixed(PointF *points = data)
                    {
                        // inside PolygonF, the vertexCount is always exactly the expected array length
                        // which is 4 (the 5th point simulates garbage data beyond the array)
                        Assert.AreEqual(1, PolygonF.TestSimpleAreaComputation(points, data.Length - 1));
                    }
                }
            }
        }
Beispiel #4
0
        public void TestContainsPointNoBufferOverrun2()
        {
            PointF[] data = new PointF[11];
            data[0] = new PointF(250, 208);
            data[1] = new PointF(247, 201);
            data[2] = new PointF(245, 208);
            data[3] = new PointF(245, 213);
            data[4] = new PointF(249, 215);
            data[5] = new PointF(251, 215);
            data[6] = new PointF(254, 215);
            data[7] = new PointF(251, 210);
            data[8] = new PointF(255, 209);
            data[9] = new PointF(254, 201);

            var offset = PolygonF.TestVertexNormalization(data);

            // test for a possible buffer overrun in the computation
            foreach (PointF garbagePoint in SimulatedBufferOverrunPoints)
            {
                data[data.Length - 1] = garbagePoint;
                unsafe
                {
                    fixed(PointF *points = data)
                    {
                        // inside PolygonF, the vertexCount is always exactly the expected array length
                        // which is 10 (the 11th point simulates garbage data beyond the array)
                        int vertexCount = data.Length - 1;

                        Assert.IsTrue(PolygonF.TestContains(new PointF(249, 209) - offset, points, vertexCount));
                    }
                }
            }
        }
Beispiel #5
0
        public void TestSimpleAreaAlgorithm()
        {
            // this test differs from TestSimplePolygonAreaComputation in that here we specifically call the simple area algorithm
            PointF[] data = new PointF[4];
            data[0] = new PointF(0, 0);
            data[1] = new PointF(1, 0);
            data[2] = new PointF(1, 1);
            data[3] = new PointF(0, 1);

            PolygonF.TestVertexNormalization(data);

            unsafe
            {
                fixed(PointF *points = data)
                {
                    // inside PolygonF, the vertexCount is always exactly the expected array length
                    Assert.AreEqual(1, PolygonF.TestSimpleAreaComputation(points, data.Length));
                }
            }
        }
Beispiel #6
0
        public void TestVertexNormalization()
        {
            var data = new[]
            {
                new PointF(8000, 8000),
                new PointF(10000, 10000),
                new PointF(9503, 9763),
                new PointF(9273, 8000),
                new PointF(10000, 9939),
                new PointF(8799, 8103)
            };

            var offset = PolygonF.TestVertexNormalization(data);

            Assert.AreEqual(new SizeF(9000, 9000), offset);
            Assert.AreEqual(new PointF(-1000, -1000), data[0]);
            Assert.AreEqual(new PointF(1000, 1000), data[1]);
            Assert.AreEqual(new PointF(503, 763), data[2]);
            Assert.AreEqual(new PointF(273, -1000), data[3]);
            Assert.AreEqual(new PointF(1000, 939), data[4]);
            Assert.AreEqual(new PointF(-201, -897), data[5]);
        }
Beispiel #7
0
        public void TestContainsPointNoBufferOverrun()
        {
            PointF[] data = new PointF[5];
            data[0] = new PointF(0, 0);
            data[1] = new PointF(1, 0);
            data[2] = new PointF(1, 1);
            data[3] = new PointF(0, 1);

            var offset = PolygonF.TestVertexNormalization(data);

            // test for a possible buffer overrun in the computation
            foreach (PointF garbagePoint in SimulatedBufferOverrunPoints)
            {
                data[data.Length - 1] = garbagePoint;
                unsafe
                {
                    fixed(PointF *points = data)
                    {
                        // inside PolygonF, the vertexCount is always exactly the expected array length
                        // which is 4 (the 5th point simulates garbage data beyond the array)
                        int vertexCount = data.Length - 1;

                        Assert.IsTrue(PolygonF.TestContains(new PointF(0.5f, 0.5f) - offset, points, vertexCount));                     // inside
                        Assert.IsFalse(PolygonF.TestContains(new PointF(0.5f, 1.5f) - offset, points, vertexCount));                    // above
                        Assert.IsFalse(PolygonF.TestContains(new PointF(0f, 1.5f) - offset, points, vertexCount));                      // above

                        Assert.IsTrue(PolygonF.TestContains(new PointF(0f, 0.5f) - offset, points, vertexCount));                       // left edge
                        Assert.IsFalse(PolygonF.TestContains(new PointF(1f, 0.5f) - offset, points, vertexCount));                      // right edge
                        Assert.IsTrue(PolygonF.TestContains(new PointF(0.5f, 0f) - offset, points, vertexCount));                       // bottom edge
                        Assert.IsFalse(PolygonF.TestContains(new PointF(0.5f, 1f) - offset, points, vertexCount));                      // top edge

                        Assert.IsTrue(PolygonF.TestContains(new PointF(0f, 0f) - offset, points, vertexCount));                         // bottom left corner
                        Assert.IsFalse(PolygonF.TestContains(new PointF(0f, 1f) - offset, points, vertexCount));                        // top left corner
                        Assert.IsFalse(PolygonF.TestContains(new PointF(1f, 0f) - offset, points, vertexCount));                        // bottom right corner
                        Assert.IsFalse(PolygonF.TestContains(new PointF(1f, 1f) - offset, points, vertexCount));                        // top right corner
                    }
                }
            }
        }