예제 #1
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);
                }
            }
        }
예제 #2
0
        public void TestComplexPolygonAreaComputation2()
        {
            // 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 = 5 * side * side;

            List <PointF> data = new List <PointF>();

            data.Add(new PointF(0, 0));
            data.Add(new PointF(side, 0));
            data.Add(new PointF(side, 3 * side));
            data.Add(new PointF(0, 3 * side));
            data.Add(new PointF(0, 2 * side));
            data.Add(new PointF(3 * side, 2 * side));
            data.Add(new PointF(3 * side, 3 * side));
            data.Add(new PointF(2 * side, 3 * side));
            data.Add(new PointF(2 * side, 0));
            data.Add(new PointF(3 * side, 0));
            data.Add(new PointF(3 * side, side));
            data.Add(new PointF(0, side));

            double result = new PolygonF(data).ComputeArea();

            Trace.WriteLine(string.Format("Area: Expected {0:f5} Got {1:f5}", expected, result), "UNIT_TESTS");
            Assert.IsTrue(Math.Abs(expected - result) < expected / 100f);
        }
예제 #3
0
        /// <summary>
        /// Constructs a new polygonal region of interest, specifying an <see cref="IPointsGraphic"/> as the source of the definition and pixel data.
        /// </summary>
        /// <param name="polygon">The polygonal graphic that represents the region of interest.</param>
        public PolygonalRoi(IPointsGraphic polygon)
            : base(polygon.ParentPresentationImage)
        {
            if (polygon.Points.Count < 4 || !polygon.Points.IsClosed)             // a valid points graphic needs 4 endpoints to define 3 sides
            {
                throw new ArgumentException("Supplied graphic must be a valid closed polygon.", "polygon");
            }

            polygon.CoordinateSystem = CoordinateSystem.Source;
            try
            {
                // this list of vertices *has* the repeated start point, so we remove it here
                // the repeated point is an artifact of the polyline graphics system, and should not be replicated in abstract polygon models.
                List <PointF> vertices = new List <PointF>(polygon.Points.Count - 1);
                for (int n = 0; n < polygon.Points.Count - 1; n++)
                {
                    vertices.Add(polygon.Points[n]);
                }
                _polygon = new PolygonF(vertices);
            }
            finally
            {
                polygon.ResetCoordinateSystem();
            }
        }
예제 #4
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));
                    }
                }
            }
        }
예제 #5
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);
                    }
                }
            }
        }
예제 #6
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));
                    }
                }
            }
        }
예제 #7
0
		/// <summary>
		/// Constructs a new polygonal region of interest.
		/// </summary>
		/// <param name="vertices">The ordered vertices defining the polygonal region of interest.</param>
		/// <param name="presentationImage">The image containing the source pixel data.</param>
		public PolygonalRoi(IEnumerable<PointF> vertices, IPresentationImage presentationImage) : base(presentationImage)
		{
			var list = new List<PointF>(vertices);
			if (list.Count < 3) // a valid list of vertices needs 3 items
				throw new ArgumentException("Three vertices are required to define a polygon.", "vertices");

			// vertices should not contain repeated start point
			_polygon = new PolygonF(vertices);
		}
예제 #8
0
        /// <summary>
        /// Constructs a new polygonal region of interest.
        /// </summary>
        /// <param name="vertices">The ordered vertices defining the polygonal region of interest.</param>
        /// <param name="presentationImage">The image containing the source pixel data.</param>
        public PolygonalRoi(IEnumerable <PointF> vertices, IPresentationImage presentationImage) : base(presentationImage)
        {
            var list = new List <PointF>(vertices);

            if (list.Count < 3)             // a valid list of vertices needs 3 items
            {
                throw new ArgumentException("Three vertices are required to define a polygon.", "vertices");
            }

            // vertices should not contain repeated start point
            _polygon = new PolygonF(vertices);
        }
예제 #9
0
		public void TestBasics()
		{
			PolygonF p1 = new PolygonF(PointF.Empty, PointF.Empty, PointF.Empty);
			Assert.AreEqual(3, p1.CountVertices);
			PolygonF p2 = new PolygonF(new PointF(0, 0), new PointF(1, 0), new PointF(1, 1));
			Assert.AreEqual(3, p2.CountVertices);
			Assert.IsFalse(p2.IsComplex);
			PolygonF p3 = new PolygonF(new PointF(0, 0), new PointF(1, 0), new PointF(1, 1), new PointF(0, 1));
			Assert.AreEqual(4, p3.CountVertices);
			Assert.IsFalse(p3.IsComplex);
			PolygonF p4 = new PolygonF(new PointF(0, 0), new PointF(1, 0), new PointF(0, 1), new PointF(1, 1));
			Assert.AreEqual(4, p4.CountVertices);
			Assert.IsTrue(p4.IsComplex);
		}
예제 #10
0
        public GameWorldArea(TiledMapObject obj)
        {
            this.details = obj;

            this.ID     = obj.Identifier;
            this.bounds = null;

            if (obj is TiledMapPolygonObject)
            {
                var polyObj = (TiledMapPolygonObject)obj;
                var polygon = new PolygonF(polyObj.Points.Select(x => new Vector2(x.X, x.Y)));
                polygon.Offset(new Vector2(polyObj.Position.X, polyObj.Position.Y));

                this.bounds = polygon;
            }
            else if (obj is TiledMapRectangleObject)
            {
                var rectObj = (TiledMapRectangleObject)obj;
                this.bounds = new RectangleF((int)rectObj.Position.X, (int)rectObj.Position.Y, (int)rectObj.Size.Width, (int)rectObj.Size.Height);
            }
            else
            {
                throw new ArgumentException("Invalid GameMapArea");
            }


            if (obj.Properties != null)
            {
                String lifeDegen, speedModifier, slippery;

                if (obj.Properties.TryGetValue("life_degeneration", out lifeDegen))
                {
                    this.degenerationRatio = float.Parse(lifeDegen, CultureInfo.InvariantCulture);
                }

                if (obj.Properties.TryGetValue("speed_modifier", out speedModifier))
                {
                    this.speedModifier = float.Parse(speedModifier, CultureInfo.InvariantCulture);
                }
                else
                {
                    this.speedModifier = 1.0f;
                }

                if (obj.Properties.TryGetValue("slippery", out slippery))
                {
                    this.slippery = Boolean.Parse(slippery);
                }
            }
        }
예제 #11
0
        /// <summary>
        /// Procesa una Vía.
        /// </summary>
        /// <param name="laVía">La Vía.</param>
        /// <returns>El número de problemas detectados al procesar el elemento.</returns>
        protected override int ProcesaElemento(Vía laVía)
        {
            int númeroDeProblemasDetectados = 0;

            // Por cada ciudad, si una coordenada de la Vía está adentro de la ciudad entonces
            // se le actualiza el Indice de Ciudad.
            bool seEncontróUnaCiudad = false;

            foreach (Ciudad ciudad in ManejadorDeMapa.Ciudades)
            {
                PolygonF polígono = new PolygonF(ciudad.CoordenadasComoPuntos);
                foreach (Coordenadas coordenadas in laVía.Coordenadas)
                {
                    if (polígono.Contains(coordenadas))
                    {
                        bool cambió = laVía.ActualizaCampoIndiceDeCiudad(
                            ciudad.Indice,
                            string.Format("M100: La Vía pertenece a la Ciudad {0}.", ciudad));
                        if (cambió)
                        {
                            ++númeroDeProblemasDetectados;
                        }

                        // La Vía solo puede pertenecer a una sola ciudad.
                        // Así que nos salimos del ciclo.
                        seEncontróUnaCiudad = true;
                        break;
                    }
                }

                // La Vía solo puede pertenecer a una sola ciudad.
                // Así que nos salimos del ciclo.
                if (seEncontróUnaCiudad)
                {
                    break;
                }
            }

            // Si no se encontró una ciudad entonces hay que quitarle el campo si lo tiene.
            if (!seEncontróUnaCiudad)
            {
                bool cambió = laVía.RemueveCampoIndiceDeCiudad("M101: La vía no pertenece a ninguna ciudad.");
                if (cambió)
                {
                    ++númeroDeProblemasDetectados;
                }
            }

            return(númeroDeProblemasDetectados);
        }
예제 #12
0
        public void TestContainsPoint2()
        {
            PolygonF polygon = new PolygonF(
                new PointF(250, 208),
                new PointF(247, 201),
                new PointF(245, 208),
                new PointF(245, 213),
                new PointF(249, 215),
                new PointF(251, 215),
                new PointF(254, 215),
                new PointF(251, 210),
                new PointF(255, 209),
                new PointF(254, 201));

            Assert.IsTrue(polygon.Contains(new PointF(249, 209)));
        }
예제 #13
0
        public void Polygon_Transform_Rotation_Test()
        {
            var vertices = new[]
            {
                new Vector2(-5, -5),
                new Vector2(5, 10),
                new Vector2(-5, 10)
            };

            var polygon = new PolygonF(vertices);
            polygon.Rotate(MathHelper.ToRadians(90));

            const float tolerance = 0.01f;
            Assert.IsTrue(new Vector2(5, -5).EqualsWithTolerance(polygon.Vertices[0], tolerance));
            Assert.IsTrue(new Vector2(-10, 5).EqualsWithTolerance(polygon.Vertices[1], tolerance));
            Assert.IsTrue(new Vector2(-10, -5).EqualsWithTolerance(polygon.Vertices[2], tolerance));
        }
예제 #14
0
        public void Polygon_Transform_Scale_Test()
        {
            var vertices = new[]
            {
                new Vector2(0, -1),
                new Vector2(1, 1),
                new Vector2(-1, 1)
            };

            var polygon = new PolygonF(vertices);
            polygon.Scale(new Vector2(1, -0.5f));

            const float tolerance = 0.01f;
            Assert.IsTrue(new Vector2(0, -0.5f).EqualsWithTolerance(polygon.Vertices[0], tolerance), "0");
            Assert.IsTrue(new Vector2(2f, 0.5f).EqualsWithTolerance(polygon.Vertices[1], tolerance), "1");
            Assert.IsTrue(new Vector2(-2f, 0.5f).EqualsWithTolerance(polygon.Vertices[2], tolerance), "2");
        }
예제 #15
0
		public void TestContainsPoint()
		{
			PolygonF polygon = new PolygonF(new PointF[] {new PointF(0, 0), new PointF(1, 0), new PointF(1, 1), new PointF(0, 1)});
			Assert.IsTrue(polygon.Contains(new PointF(0.5f, 0.5f))); // inside
			Assert.IsFalse(polygon.Contains(new PointF(0.5f, 1.5f))); // above
			Assert.IsFalse(polygon.Contains(new PointF(0f, 1.5f))); // above

			Assert.IsTrue(polygon.Contains(new PointF(0f, 0.5f))); // left edge
			Assert.IsFalse(polygon.Contains(new PointF(1f, 0.5f))); // right edge
			Assert.IsTrue(polygon.Contains(new PointF(0.5f, 0f))); // bottom edge
			Assert.IsFalse(polygon.Contains(new PointF(0.5f, 1f))); // top edge

			Assert.IsTrue(polygon.Contains(new PointF(0f, 0f))); // bottom left corner
			Assert.IsFalse(polygon.Contains(new PointF(0f, 1f))); // top left corner
			Assert.IsFalse(polygon.Contains(new PointF(1f, 0f))); // bottom right corner
			Assert.IsFalse(polygon.Contains(new PointF(1f, 1f))); // top right corner
		}
예제 #16
0
        /// <summary>
        /// Este método se llama antes de comenzar a procesar los elementos.
        /// </summary>
        protected override bool ComenzóAProcesar()
        {
            misAlertas.Clear();
            misPdisYaProcesadas.Clear();

            // Obtiene los límites del mapa.
            if (ManejadorDeMapa.LímitesDelMapa != null)
            {
                misLímitesDelMapa = new PolygonF(ManejadorDeMapa.LímitesDelMapa);
            }
            else
            {
                misLímitesDelMapa = null;
            }

            return(base.ComenzóAProcesar());
        }
예제 #17
0
        public void Polygon_Contains_Point_Test()
        {
            var vertices = new[]
            {
                new Vector2(0, 0),
                new Vector2(10, 0),
                new Vector2(10, 10),
                new Vector2(0, 10)
            };

            var polygon = new PolygonF(vertices);

            Assert.IsTrue(polygon.Contains(new Vector2(5, 5)));
            Assert.IsTrue(polygon.Contains(new Vector2(0.01f, 0.01f)));
            Assert.IsTrue(polygon.Contains(new Vector2(9.99f, 9.99f)));
            Assert.IsFalse(polygon.Contains(new Vector2(-1f, -1f)));
            Assert.IsFalse(polygon.Contains(new Vector2(-11f, -11f)));
        }
예제 #18
0
        public void TestContainsPoint()
        {
            PolygonF polygon = new PolygonF(new PointF[] { new PointF(0, 0), new PointF(1, 0), new PointF(1, 1), new PointF(0, 1) });

            Assert.IsTrue(polygon.Contains(new PointF(0.5f, 0.5f)));         // inside
            Assert.IsFalse(polygon.Contains(new PointF(0.5f, 1.5f)));        // above
            Assert.IsFalse(polygon.Contains(new PointF(0f, 1.5f)));          // above

            Assert.IsTrue(polygon.Contains(new PointF(0f, 0.5f)));           // left edge
            Assert.IsFalse(polygon.Contains(new PointF(1f, 0.5f)));          // right edge
            Assert.IsTrue(polygon.Contains(new PointF(0.5f, 0f)));           // bottom edge
            Assert.IsFalse(polygon.Contains(new PointF(0.5f, 1f)));          // top edge

            Assert.IsTrue(polygon.Contains(new PointF(0f, 0f)));             // bottom left corner
            Assert.IsFalse(polygon.Contains(new PointF(0f, 1f)));            // top left corner
            Assert.IsFalse(polygon.Contains(new PointF(1f, 0f)));            // bottom right corner
            Assert.IsFalse(polygon.Contains(new PointF(1f, 1f)));            // top right corner
        }
예제 #19
0
        public void Polygon_Transform_Translation_Test()
        {
            var vertices = new[] 
            {
                new Vector2(0, 0),
                new Vector2(10, 0),
                new Vector2(10, 10),
                new Vector2(0, 10)
            };

            var polygon = new PolygonF(vertices);
            polygon.Offset(new Vector2(2, 3));

            Assert.AreEqual(new Vector2(2, 3), polygon.Vertices[0]);
            Assert.AreEqual(new Vector2(12, 3), polygon.Vertices[1]);
            Assert.AreEqual(new Vector2(12, 13), polygon.Vertices[2]);
            Assert.AreEqual(new Vector2(2, 13), polygon.Vertices[3]);
        }
예제 #20
0
        public void Polygon_Contains_Point_Test()
        {
            var vertices = new[]
            {
                new Vector2(0, 0),
                new Vector2(10, 0),
                new Vector2(10, 10),
                new Vector2(0, 10)
            };

            var polygon = new PolygonF(vertices);

            Assert.IsTrue(polygon.Contains(new Vector2(5, 5)));
            Assert.IsTrue(polygon.Contains(new Vector2(0.01f, 0.01f)));
            Assert.IsTrue(polygon.Contains(new Vector2(9.99f, 9.99f)));
            Assert.IsFalse(polygon.Contains(new Vector2(-1f, -1f)));
            Assert.IsFalse(polygon.Contains(new Vector2(-11f, -11f)));
        }
예제 #21
0
        public void TestBasics()
        {
            PolygonF p1 = new PolygonF(PointF.Empty, PointF.Empty, PointF.Empty);

            Assert.AreEqual(3, p1.CountVertices);
            PolygonF p2 = new PolygonF(new PointF(0, 0), new PointF(1, 0), new PointF(1, 1));

            Assert.AreEqual(3, p2.CountVertices);
            Assert.IsFalse(p2.IsComplex);
            PolygonF p3 = new PolygonF(new PointF(0, 0), new PointF(1, 0), new PointF(1, 1), new PointF(0, 1));

            Assert.AreEqual(4, p3.CountVertices);
            Assert.IsFalse(p3.IsComplex);
            PolygonF p4 = new PolygonF(new PointF(0, 0), new PointF(1, 0), new PointF(0, 1), new PointF(1, 1));

            Assert.AreEqual(4, p4.CountVertices);
            Assert.IsTrue(p4.IsComplex);
        }
예제 #22
0
        public void Polygon_Transform_Rotation_Test()
        {
            var vertices = new[]
            {
                new Vector2(-5, -5),
                new Vector2(5, 10),
                new Vector2(-5, 10)
            };

            var polygon = new PolygonF(vertices);

            polygon.Rotate(MathHelper.ToRadians(90));

            const float tolerance = 0.01f;

            Assert.IsTrue(new Vector2(5, -5).EqualsWithTolerance(polygon.Vertices[0], tolerance));
            Assert.IsTrue(new Vector2(-10, 5).EqualsWithTolerance(polygon.Vertices[1], tolerance));
            Assert.IsTrue(new Vector2(-10, -5).EqualsWithTolerance(polygon.Vertices[2], tolerance));
        }
예제 #23
0
        public void Polygon_Transform_Translation_Test()
        {
            var vertices = new[]
            {
                new Vector2(0, 0),
                new Vector2(10, 0),
                new Vector2(10, 10),
                new Vector2(0, 10)
            };

            var polygon = new PolygonF(vertices);

            polygon.Offset(new Vector2(2, 3));

            Assert.AreEqual(new Vector2(2, 3), polygon.Vertices[0]);
            Assert.AreEqual(new Vector2(12, 3), polygon.Vertices[1]);
            Assert.AreEqual(new Vector2(12, 13), polygon.Vertices[2]);
            Assert.AreEqual(new Vector2(2, 13), polygon.Vertices[3]);
        }
예제 #24
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="elPolígono">El polígono.</param>
        /// <param name="elIndice">El índice de la ciudad.</param>
        public Ciudad(
            Polígono elPolígono,
            CampoIndiceDeCiudad elIndice)
        {
            Nombre                = elPolígono.Nombre;
            Tipo                  = elPolígono.Tipo;
            Indice                = elIndice;
            Coordenadas           = elPolígono.Coordenadas;
            CoordenadasComoPuntos = new PointF[Coordenadas.Length];
            for (int i = 0; i < Coordenadas.Length; ++i)
            {
                CoordenadasComoPuntos[i] = Coordenadas[i];
            }

            // Calcula el centro de la ciudad.
            PolygonF poligonoDeLaCiudad = new PolygonF(CoordenadasComoPuntos);

            Centro = poligonoDeLaCiudad.CenterPointOfBounds;
        }
예제 #25
0
        public void Polygon_Transform_Scale_Test()
        {
            var vertices = new[]
            {
                new Vector2(0, -1),
                new Vector2(1, 1),
                new Vector2(-1, 1)
            };

            var polygon = new PolygonF(vertices);

            polygon.Scale(new Vector2(1, -0.5f));

            const float tolerance = 0.01f;

            Assert.IsTrue(new Vector2(0, -0.5f).EqualsWithTolerance(polygon.Vertices[0], tolerance), "0");
            Assert.IsTrue(new Vector2(2f, 0.5f).EqualsWithTolerance(polygon.Vertices[1], tolerance), "1");
            Assert.IsTrue(new Vector2(-2f, 0.5f).EqualsWithTolerance(polygon.Vertices[2], tolerance), "2");
        }
예제 #26
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));
                }
            }
        }
예제 #27
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]);
        }
예제 #28
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
                    }
                }
            }
        }
예제 #29
0
		/// <summary>
		/// Constructs a new polygonal region of interest, specifying an <see cref="IPointsGraphic"/> as the source of the definition and pixel data.
		/// </summary>
		/// <param name="polygon">The polygonal graphic that represents the region of interest.</param>
		public PolygonalRoi(IPointsGraphic polygon)
			: base(polygon.ParentPresentationImage)
		{
			if (polygon.Points.Count < 4 || !polygon.Points.IsClosed) // a valid points graphic needs 4 endpoints to define 3 sides
				throw new ArgumentException("Supplied graphic must be a valid closed polygon.", "polygon");

			polygon.CoordinateSystem = CoordinateSystem.Source;
			try
			{
				// this list of vertices *has* the repeated start point, so we remove it here
				// the repeated point is an artifact of the polyline graphics system, and should not be replicated in abstract polygon models.
				List<PointF> vertices = new List<PointF>(polygon.Points.Count - 1);
				for (int n = 0; n < polygon.Points.Count - 1; n++)
				{
					vertices.Add(polygon.Points[n]);
				}
				_polygon = new PolygonF(vertices);
			}
			finally
			{
				polygon.ResetCoordinateSystem();
			}
		}
예제 #30
0
		public void TestComplexPolygonAreaComputation2()
		{
			// 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 = 5*side*side;

			List<PointF> data = new List<PointF>();
			data.Add(new PointF(0, 0));
			data.Add(new PointF(side, 0));
			data.Add(new PointF(side, 3*side));
			data.Add(new PointF(0, 3*side));
			data.Add(new PointF(0, 2*side));
			data.Add(new PointF(3*side, 2*side));
			data.Add(new PointF(3*side, 3*side));
			data.Add(new PointF(2*side, 3*side));
			data.Add(new PointF(2*side, 0));
			data.Add(new PointF(3*side, 0));
			data.Add(new PointF(3*side, side));
			data.Add(new PointF(0, side));

			double result = new PolygonF(data).ComputeArea();
			Trace.WriteLine(string.Format("Area: Expected {0:f5} Got {1:f5}", expected, result), "UNIT_TESTS");
			Assert.IsTrue(Math.Abs(expected - result) < expected/100f);
		}
예제 #31
0
		public void TestContainsPoint2()
		{
			PolygonF polygon = new PolygonF(
				new PointF(250, 208),
				new PointF(247, 201),
				new PointF(245, 208),
				new PointF(245, 213),
				new PointF(249, 215),
				new PointF(251, 215),
				new PointF(254, 215),
				new PointF(251, 210),
				new PointF(255, 209),
				new PointF(254, 201));
			Assert.IsTrue(polygon.Contains(new PointF(249, 209)));
		}
예제 #32
0
		public abstract void DrawPolygon(Vector2 position, PolygonF polygon, Color color, float thickness = 1);
예제 #33
0
        /// <summary>
        /// Procesa un PDI.
        /// </summary>
        /// <param name="elPdi">El PDI.</param>
        /// <returns>El número de problemas detectados al procesar el elemento.</returns>
        protected override int ProcesaElemento(Pdi elPdi)
        {
            // Retorna si el PDI es una ciudad.
            if (elPdi.EsCiudad)
            {
                return(0);
            }

            int númeroDeProblemasDetectados = 0;

            // Por cada ciudad, si el PDI está adentro de la ciudad entonces
            // se le actualiza el Indice de Ciudad.
            Ciudad ciudadDelPdi = null;
            Ciudad estadoDelPdi = null;

            foreach (Ciudad ciudad in ManejadorDeMapa.Ciudades)
            {
                PolygonF polígono = new PolygonF(ciudad.CoordenadasComoPuntos);
                if (polígono.Contains(elPdi.Coordenadas))
                {
                    // Tipo 0x4a representa un Estado.
                    if (ciudad.Tipo.Value.TipoPrincipal == 0x4a)
                    {
                        estadoDelPdi = ciudad;
                    }
                    else
                    {
                        // El PDI solo puede pertenecer a una sola ciudad.
                        ciudadDelPdi = ciudad;
                        break;
                    }
                }
            }
            if (ciudadDelPdi != null)
            {
                bool cambió = elPdi.ActualizaCampoIndiceDeCiudad(
                    ciudadDelPdi.Indice,
                    string.Format(Properties.Recursos.M000, ciudadDelPdi));
                if (cambió)
                {
                    ++númeroDeProblemasDetectados;
                }
            }
            else if (estadoDelPdi != null)
            {
                bool cambió = elPdi.ActualizaCampoIndiceDeCiudad(
                    estadoDelPdi.Indice,
                    string.Format(Properties.Recursos.M008, estadoDelPdi));
                if (cambió)
                {
                    ++númeroDeProblemasDetectados;
                }
            }
            else
            {
                bool cambió = elPdi.RemueveCampoIndiceDeCiudad(Properties.Recursos.M001);
                if (cambió)
                {
                    ++númeroDeProblemasDetectados;
                }
            }

            return(númeroDeProblemasDetectados);
        }
 /// <summary>
 /// Draws a polygon from a <see cref="PolygonF"/> shape
 /// </summary>
 /// <param name="spriteBatch">The destination drawing surface</param>
 /// /// <param name="position">Where to position the polygon</param>
 /// <param name="polygon">The polygon to draw</param>
 /// <param name="color">The color to use</param>
 /// <param name="thickness">The thickness of the lines</param>
 public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 position, PolygonF polygon, Color color, float thickness = 1f)
 {
     DrawPolygon(spriteBatch, position, polygon.Vertices.ToList(), color, thickness);
 }
		public override void DrawPolygon(Vector2 position, PolygonF polygon, Color color, float thickness = 1f)
		{
			DrawPolygon(position, polygon.Vertices, color, true, thickness);
		}