public bool Contains(Vector2 point) { List <Triangle2D> polygonTriangles = PolygonTriangulator.Triangulate(this); foreach (Triangle2D triangle in polygonTriangles) { if (triangle.Contains(point)) { return(true); } } return(false); }
public void OpenTK() { { 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, out float area); Check.That(centroid.X == 150 && centroid.Y == 150 && area == 10000); } { 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); Check.That(res.Count == 3); Check.That(res[0].Count == 3); Check.That(res[0][0] == new Vector2(0, 100)); Check.That(res[0][1] == new Vector2(100, 200)); Check.That(res[0][2] == new Vector2(100, 100)); Check.That(res[1].Count == 4); Check.That(res[1][0] == new Vector2(100, 100)); Check.That(res[1][1] == new Vector2(200, 200)); Check.That(res[1][2] == new Vector2(300, 100)); Check.That(res[1][3] == new Vector2(150, 0)); Check.That(res[2].Count == 4); Check.That(res[2][0] == new Vector2(150, 0)); Check.That(res[2][1] == new Vector2(0, 50)); Check.That(res[2][2] == new Vector2(0, 100)); Check.That(res[1][3] == new Vector2(100, 100)); } }
public void CreateObjects(string name, GLItemsList items, GLRenderProgramSortedList rObjects, GalacticMapping galmap, float sizeofname = 5000, ManualCorrections[] corr = null) { List <Vector4> vertexcolourregions = new List <Vector4>(); List <Vector4> vertexregionsoutlines = new List <Vector4>(); List <ushort> vertexregionoutlineindex = new List <ushort>(); Size bitmapsize = new Size(250, 22); textrenderer = new GLBitmaps(name + "-bitmaps", rObjects, bitmapsize, depthtest: false, yfixed: true); items.Add(textrenderer); StringFormat fmt = new StringFormat(StringFormatFlags.NoWrap) { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; Font fnt = new Font("MS Sans Serif", 12F); int cindex = 0; foreach (GalacticMapObject gmo in galmap.galacticMapObjects) { if (gmo.galMapType.Group == GalMapType.GalMapGroup.Regions) { string gmoname = gmo.name; List <Vector2> polygonxz = new List <Vector2>(); // needs it in x/z and in vector2's foreach (var pd in gmo.points) { polygonxz.Add(new Vector2((float)pd.X, (float)pd.Z)); // can be concave and wound the wrong way.. vertexregionoutlineindex.Add((ushort)(vertexregionsoutlines.Count)); vertexregionsoutlines.Add(new Vector4((float)pd.X, 0, (float)pd.Z, 1)); } vertexregionoutlineindex.Add(0xffff); // primitive restart to break polygon List <List <Vector2> > polys = PolygonTriangulator.Triangulate(polygonxz, false); // cut into convex polygons first - because we want the biggest possible area for naming purposes Vector2 avgcentroid = new Vector2(0, 0); int pointsaveraged = 0; if (polys.Count > 0) // just in case.. { foreach (List <Vector2> points in polys) // now for every poly { List <List <Vector2> > polytri; if (points.Count == 3) // already a triangle.. { polytri = new List <List <Vector2> >() { new List <Vector2>() { points[0], points[1], points[2] } } } ; else { polytri = PolygonTriangulator.Triangulate(points, true); // cut into triangles not polygons } foreach (List <Vector2> pt in polytri) { vertexcolourregions.Add(pt[0].ToVector4XZ(w: cindex)); vertexcolourregions.Add(pt[2].ToVector4XZ(w: cindex)); vertexcolourregions.Add(pt[1].ToVector4XZ(w: cindex)); var cx = (pt[0].X + pt[1].X + pt[2].X) / 3; var cy = (pt[0].Y + pt[1].Y + pt[2].Y) / 3; avgcentroid = new Vector2(avgcentroid.X + cx, avgcentroid.Y + cy); pointsaveraged++; //foreach (var pd in pt) // debug //{ // vertexregionoutlineindex.Add((ushort)(vertexregionsoutlines.Count)); // vertexregionsoutlines.Add(new Vector4((float)pd.X, 0, (float)pd.Y, 1)); //} //vertexregionoutlineindex.Add(0xffff); // primitive restart to break polygon } } cindex = (cindex + 1) % array.Length; Vector2 centeroid = PolygonTriangulator.WeightedCentroids(polys); if (corr != null) // allows the centeroid to be nerfed slightly { var entry = Array.Find(corr, x => gmo.name.Contains(x.name, StringComparison.InvariantCultureIgnoreCase)); if (entry != null) { centeroid = new Vector2(centeroid.X + entry.x, centeroid.Y + entry.y); } } var final = PolygonTriangulator.FitInsideConvexPoly(polys, centeroid, new Vector2(sizeofname, sizeofname * (float)bitmapsize.Height / (float)bitmapsize.Width)); Vector3 bestpos = new Vector3(final.Item1.X, 0, final.Item1.Y); Vector3 bestsize = new Vector3(final.Item2.X, 1, final.Item2.Y); textrenderer.Add(null, gmo.name, fnt, Color.White, Color.Transparent, bestpos, bestsize, new Vector3(0, 0, 0), fmt, alphafadescalar: 5000, alphafadepos: 500); } } } fmt.Dispose(); fnt.Dispose(); // regions var vertregion = new GLPLVertexShaderWorldPalletColor(array.ToVector4(0.1f), true); var fragregion = new GLPLFragmentShaderVSColor(); regionshader = new GLShaderPipeline(vertregion, fragregion, null, null); items.Add(regionshader); GLRenderState rt = GLRenderState.Tri(); rt.DepthTest = false; var ridisplay = GLRenderableItem.CreateVector4(items, OpenTK.Graphics.OpenGL4.PrimitiveType.Triangles, rt, vertexcolourregions.ToArray()); rObjects.Add(regionshader, name + "-regions", ridisplay); // outlines var vertoutline = new GLPLVertexShaderWorldCoord(true); var fragoutline = new GLPLFragmentShaderFixedColor(Color.Cyan); outlineshader = new GLShaderPipeline(vertoutline, fragoutline, null, null); items.Add(outlineshader); GLRenderState ro = GLRenderState.Lines(); ro.DepthTest = false; ro.PrimitiveRestart = 0xffff; var rioutline = GLRenderableItem.CreateVector4(items, OpenTK.Graphics.OpenGL4.PrimitiveType.LineStrip, ro, vertexregionsoutlines.ToArray()); rioutline.CreateElementIndexUShort(items.NewBuffer(), vertexregionoutlineindex.ToArray()); rObjects.Add(outlineshader, name + "-outlines", rioutline); renderstate = 7; }