示例#1
0
        // Triangulate the polygon.
        //
        // For a nice, detailed explanation of this method,
        // see Ian Garton's Web page:
        // http://www-cgrl.cs.mcgill.ca/~godfried/teaching/cg-projects/97/Ian/cutting_ears.html
        public List <Triangle> Triangulate()
        {
            // Copy the points into a scratch array.
            PointF[] pts = new PointF[Points.Length];
            Array.Copy(Points, pts, Points.Length);

            // Make a scratch polygon.
            Polygon2 pgon = new Polygon2(pts);

            // Orient the polygon clockwise.
            pgon.OrientPolygonClockwise();

            // Make room for the triangles.
            List <Triangle> triangles = new List <Triangle>();

            // While the copy of the polygon has more than
            // three points, remove an ear.
            while (pgon.Points.Length > 3)
            {
                // Remove an ear from the polygon.
                pgon.RemoveEar(triangles);
            }

            // Copy the last three points into their own triangle.
            triangles.Add(new Triangle(pgon.Points[0], pgon.Points[1], pgon.Points[2]));

            return(triangles);
        }
        /// <summary>
        /// this method draws the map on the canvas
        /// </summary>
        /// <param name="path">The provided map</param>
        private void DrawMap(string path)
        {
            FeatureCollection geoCollection = GetData.GetFeatures(path);

            this.GetMinAndMaxValues(geoCollection);

            for (var i = 0; i < geoCollection.Features.Count(); i++)
            {
                string naam = geoCollection.Features[i].Properties["localname"].ToString();
                GeoJSON.Net.Geometry.MultiPolygon geo = (GeoJSON.Net.Geometry.MultiPolygon)geoCollection.Features[i].Geometry;
                this.colorCounter = i;
                Console.WriteLine("printing: " + naam);
                Console.WriteLine(this.colorCounter);
                for (int j = 0; j < geo.Coordinates.Count(); j++)
                {
                    for (int k = 0; k < geo.Coordinates[j].Coordinates.Count(); k++)
                    {
                        Polygon newPoly = new Polygon();
                        List <System.Windows.Point> pointList = new List <System.Windows.Point>();

                        // get all x and y points in a generic collection PointsList
                        for (int l = 0; l < geo.Coordinates[j].Coordinates[k].Coordinates.Count(); l++)
                        {
                            for (int q = 0; q < 1; q++)
                            {
                                double x = this.ConvertX(geo.Coordinates[j].Coordinates[k].Coordinates[l].Longitude);
                                double y = this.ConvertY(geo.Coordinates[j].Coordinates[k].Coordinates[l].Latitude);

                                pointList.Add(new System.Windows.Point(this.AddScalabilityX(x), this.AddScalabilityY(y)));
                            }
                        }

                        // point reduction
                        try
                        {
                            pointList = PointReduction.DouglasPeuckerReduction(pointList, double.Parse(Epsilon.Text));
                        }
                        catch
                        {
                            pointList = PointReduction.DouglasPeuckerReduction(pointList, 1.0);
                        }

                        List <PointF>   newPoints        = new List <PointF>();
                        PointCollection pointsCollection = new PointCollection();

                        for (int z = 0; z < pointList.Count(); z++)
                        {
                            newPoints.Add(new PointF((float)pointList[z].X, (float)pointList[z].Y));

                            // Console.WriteLine(PointList[z]);
                        }

                        // triangulation
                        // http://csharphelper.com/blog/2014/07/triangulate-a-polygon-in-c/
                        if (newPoints.ToArray().Length >= 3)
                        {
                            Polygon2        triPoly   = new Polygon2(newPoints.ToArray());
                            List <Triangle> triangles = triPoly.Triangulate();

                            foreach (Triangle tri in triangles)
                            {
                                newPoly          = new Polygon();
                                pointsCollection = new PointCollection();
                                for (int o = 0; o < tri.Points.Length; o++)
                                {
                                    pointsCollection.Add(new System.Windows.Point(tri.Points[o].X / 300, tri.Points[o].Y / 300));
                                }

                                // Console.WriteLine(pointsCollection.ToString());
                                this.Print3DTriangle(pointsCollection, this.colorCounter, this.arrayToUse);

                                // this.PrintPolygonTriangle(naam, pointsCollection, newPoly);
                            }

                            /*
                             * pointsCollection = new PointCollection();
                             *
                             * // Redraw the polygon.
                             * if (newPoints.Count >= 3)
                             * {
                             *  newPoly = new Polygon();
                             *  for (int o = 0; o < newPoints.Count; o++)
                             *  {
                             *      pointsCollection.Add(new System.Windows.Point(newPoints[o].X, newPoints[o].Y));
                             *  }
                             *
                             *  // Draw the polygon.
                             * // this.PrintPolygon(naam, pointsCollection, newPoly);
                             * }*/
                            GetNewColor();
                            this.colorCounter++;
                        }
                    }
                }
            }
        }