} // used to search // Polygon must be convex and clockwise, brute force but it works. static public void FitInsideConvexPoly(List <Vector2> points, Vector2 comparepoint, Vector2 startsize, Vector2 offsets, ref float mindist, ref Vector2 bestpos, ref Vector2 bestsize, float minwidthallowed = 1, float scaling = 0.9F) { Vector2 startpoint = comparepoint; if (!InsidePolygon(points, startpoint)) // if given a point outside the polygon, no point starting there, pick the centroid { float area; startpoint = PolygonTriangulator.Centroid(points, out area); } while (startsize.X > minwidthallowed) { Vector2 trysize = startsize; startsize *= scaling; bool foundoneatthissize = false; for (int i = 0; i < 1000; i++) { Vector2 trypos = new Vector2(startpoint.X + FlipOffset(i / 50) * offsets.X, startpoint.Y + FlipOffset(i % 50) * offsets.Y); bool inside = PolygonTriangulator.InsidePolygon(points, trypos, trysize.X, trysize.Y); float fromcompare = (trypos - comparepoint).Length; //string debugline = String.Format(" Poly {0} At {1} {2} try {3} dist {4} inside {5} step {6}", polynum, trypos.X, trypos.Y, trysize, textfromgeocentre, inside, i); //writer.WriteLine(debugline); if (inside) { if (fromcompare < mindist) { mindist = fromcompare; bestpos = trypos; bestsize = trysize; //writer.WriteLine(String.Format(" {0} Best pos at {1} size {2} dist {3}", polynum, i, trysize, mindist)); foundoneatthissize = true; } } } if (foundoneatthissize) { break; } } }
// KEEP for debug for now.. #if false static public void Test() { ///EDDiscovery._3DMap.PolygonTriangulator.Test(); if (true) { List <Vector2> points = new List <Vector2>(); points.Add(new Vector2(100, 100)); points.Add(new Vector2(100, 200)); points.Add(new Vector2(200, 200)); points.Add(new Vector2(200, 100)); Vector2 centroid = PolygonTriangulator.Centroid(points); Console.WriteLine("{0} ", centroid); } if (false) { List <Vector2> points = new List <Vector2>(); points.Add(new Vector2(0, 100)); points.Add(new Vector2(100, 200)); points.Add(new Vector2(100, 100)); points.Add(new Vector2(200, 200)); points.Add(new Vector2(300, 100)); points.Add(new Vector2(150, 0)); points.Add(new Vector2(0, 50)); List <List <Vector2> > res = PolygonTriangulator.Triangulate(points, false); foreach (List <Vector2> poly in res) { Console.WriteLine("Poly:"); foreach (Vector2 p in poly) { Console.WriteLine(" {0},{1}", p.X, p.Y); } } } Console.WriteLine("END"); }