public static int TrianglePoints(List <Vector2> pts, int Anum, int Bnum, int Cnum) { int cnt = 0; var triangleABC = new Polygon2D(); triangleABC.AddVertex(pts[Anum]); triangleABC.AddVertex(pts[Bnum]); triangleABC.AddVertex(pts[Cnum]); for (int i = 0; i < pts.Count; i++) { if ((i == Bnum) || (i == Anum) || (i == Cnum)) { continue; } if (triangleABC.ContainsInside(pts[i])) { cnt++; } } //Debug.Log(cnt); triangleABC.Clear(); return(cnt); }
public Clipper(int xmin, int xmax, int ymin, int ymax) { m_xMin = xmin; m_xMax = xmax; m_yMin = ymin; m_yMax = ymax; m_clipPoly = new Polygon2D(m_xMin, m_yMin); //Need to add vertices in clockwise order m_clipPoly.AddVertex(new Vector3 <double>(m_xMax, m_yMin, 1)); m_clipPoly.AddVertex(new Vector3 <double>(m_xMax, m_yMax, 1)); m_clipPoly.AddVertex(new Vector3 <double>(m_xMin, m_yMax, 1)); }
public Clipper(int xmin, int xmax, int ymin, int ymax) { m_xMin = xmin; m_xMax = xmax; m_yMin = ymin; m_yMax = ymax; m_clipPoly = new Polygon2D(m_xMin, m_yMin); //Need to add vertices in clockwise order m_clipPoly.AddVertex(new Vector3<double>(m_xMax, m_yMin, 1)); m_clipPoly.AddVertex(new Vector3<double>(m_xMax, m_yMax, 1)); m_clipPoly.AddVertex(new Vector3<double>(m_xMin, m_yMax, 1)); }
//Vec2D SmallestX(List<Vec2D> l) //{ // Vec2D r = l[0]; // for (int i = 1; i < l.Count; i++) // { // if (l[i].X < r.X) // { // r = l[i]; // } // } // return r; //} //Vec2D SmallestY(List<Vec2D> l) //{ // Vec2D r = l[0]; // for (int i = 1; i < l.Count; i++) // { // if (l[i].Y < r.Y) // { // r = l[i]; // } // } // return r; //} //Vec2D BiggestY(List<Vec2D> l) //{ // Vec2D r = l[0]; // for (int i = 1; i < l.Count; i++) // { // if (l[i].Y > r.Y) // { // r = l[i]; // } // } // return r; //} //Polygon2D GetPoly2(Vec2D p1, Vec2D p2) //{ // double halfwidth = m_avoidPathWidth / 2; // Polygon2D poly = new Polygon2D(); // List<Vec2D> vertices = new List<Vec2D>(); // Vec2D v1 = GetPerpendicularPoint(p1, p2, -1* halfwidth); // Vec2D v2 = GetPerpendicularPoint(p1, p2, 1 * halfwidth); // Vec2D v3 = GetPerpendicularPoint(p2, p1, 1 * halfwidth); // Vec2D v4 = GetPerpendicularPoint(p2, p1, -1 * halfwidth); // if (v1 == null || v2 == null || v3 == null || v4 == null) // { // return null; // } // else // { // vertices.Add(v1); // vertices.Add(v2); // vertices.Add(v3); // vertices.Add(v4); // Vec2D first = SmallestX(vertices); // vertices.Remove(first); // Vec2D second = SmallestX(vertices); // vertices.Remove(second); // Vec2D third; // if (second.Y > first.Y) // { // third = BiggestY(vertices); // vertices.Remove(third); // } // else // { // third = SmallestY(vertices); // vertices.Remove(third); // } // Vec2D fourth = vertices[0]; // poly.AddVertex(first); // poly.AddVertex(second); // poly.AddVertex(third); // poly.AddVertex(fourth); // return poly; // } //} //Vec2D GetPerpendicularPoint(Vec2D p1, Vec2D p2, double d) //{ // Vec2D p3 = new Vec2D(0, 0); // p3.X = p2.X + 1; // double m = -1 * ((p2.Y - p1.Y) / (p2.X - p1.X)); // double b = p2.Y + ((p2.Y - p1.Y) / (p2.X - p1.X)); // p3.Y = (m * p3.X) + b; // return ProjectPointOnLine(p2, p3, d); //} Polygon2D GetPoly(Vec2D p1, Vec2D p2) { double halfwidth = m_avoidPathWidth / 2; Polygon2D poly = new Polygon2D(); double dx = p2.X - p1.X >= 0 ? halfwidth : halfwidth * -1; double dy = p2.Y - p1.Y >= 0 ? halfwidth : halfwidth * -1; if (Math.Abs(p2.X - p1.X) > Math.Abs(p2.Y - p1.Y)) { poly.AddVertex(new Vec2D(p1.X, p1.Y - halfwidth)); poly.AddVertex(new Vec2D(p1.X, p1.Y + halfwidth)); poly.AddVertex(new Vec2D(p2.X + dx, p2.Y + halfwidth)); poly.AddVertex(new Vec2D(p2.X + dx, p2.Y - halfwidth)); return(poly); } else { poly.AddVertex(new Vec2D(p1.X - halfwidth, p1.Y)); poly.AddVertex(new Vec2D(p1.X + halfwidth, p1.Y)); poly.AddVertex(new Vec2D(p2.X + halfwidth, p2.Y + dy)); poly.AddVertex(new Vec2D(p2.X - halfwidth, p2.Y + dy)); return(poly); } }
public void Update(DDDServerConnection serverConnection, DMView dmView) { SimObject me = dmView.AllObjects[m_thisID]; SimActiveRegion zone = dmView.ActiveRegions[m_zoneID]; LocationValue otherLocation = null; Polygon2D azPoly = new Polygon2D(); foreach (PolygonValue.PolygonPoint p in zone.Shape.points) { azPoly.AddVertex(new Vec2D(p.X, p.Y)); } bool clear = true; foreach (String id in dmView.AllObjects.Keys) { if (id == m_thisID) { continue; } otherLocation = dmView.AllObjects[id].Location; if (Polygon2D.IsPointInside(azPoly, new Vec2D(otherLocation))) { clear = false; break; } } if (clear) { m_done = true; } }
public void AddVertexTest() { var poly = new Polygon2D(m_arrowVertices); var pos = new Vector2(0, 1); poly.AddVertex(pos); Assert.AreEqual(5, poly.VertexCount); Assert.AreEqual(pos, poly.Vertices.ToList()[4]); }
public Polygon2D PolygonOnCanvas(Polygon2D p) { var np = new Polygon2D(); foreach (var v in p.Vertices) { int index = m_points.IndexOf(v); Debug.Log("index in oncanvas(): " + index); np.AddVertex(m_canvas_points[index]); } return(np); }
void UpdateInActiveRegions(ref SimObject ob) { ob.InActiveRegions.Clear(); foreach (SimActiveRegion sar in m_activeRegions.Values) { Polygon2D azPoly = new Polygon2D(); foreach (PolygonValue.PolygonPoint p in sar.Shape.points) { azPoly.AddVertex(new Vec2D(p.X, p.Y)); } if (Polygon2D.IsPointInside(azPoly, new Vec2D(ob.Location))) { ob.InActiveRegions.Add(sar.ID); } } }
static void Main(string[] args) { Polygon2D poly = new Polygon2D(); poly.AddVertex(new Vec2D(1, 1)); poly.AddVertex(new Vec2D(4, 1)); poly.AddVertex(new Vec2D(2.5, 3)); poly.AddVertex(new Vec2D(4, 3)); poly.AddVertex(new Vec2D(4, 4)); poly.AddVertex(new Vec2D(2, 3.5)); bool r; r = Polygon2D.IsPointInside(poly, new Vec2D(5, 3)); //false r = Polygon2D.IsPointInside(poly, new Vec2D(2, 2)); //true r = Polygon2D.IsPointInside(poly, new Vec2D(3.5, 3.5)); //true r = Polygon2D.IsPointInside(poly, new Vec2D(1, 3.5)); //false r = Polygon2D.IsPointInside(poly, new Vec2D(3, 2.5)); //false r = Polygon2D.DoLinesIntersect(new Vec2D(0, 2), new Vec2D(3, 0), new Vec2D(1, 1), new Vec2D(3, 3)); //true r = Polygon2D.DoLinesIntersect(new Vec2D(0, 2), new Vec2D(3, 0), new Vec2D(2, 1), new Vec2D(3, 3)); //false DataValue dv = DataValueFactory.BuildValue("CapabilityType"); ((CapabilityValue)dv).effects.Add(new CapabilityValue.Effect("foo", 45, 10, .50)); ((CapabilityValue)dv).effects.Add(new CapabilityValue.Effect("foo", 100, 5, .25)); string s = DataValueFactory.XMLSerialize(dv); DataValue dv2 = DataValueFactory.XMLDeserialize(s); DataValue dv3 = DataValueFactory.BuildValue("VulnerabilityType"); VulnerabilityValue.Transition t = new VulnerabilityValue.Transition("dead"); t.conditions.Add(new VulnerabilityValue.TransitionCondition("foo", 50, 0, 0)); t.conditions.Add(new VulnerabilityValue.TransitionCondition("bar", 20, 0, 0)); ((VulnerabilityValue)dv3).transitions.Add(t); t = new VulnerabilityValue.Transition("hurt"); t.conditions.Add(new VulnerabilityValue.TransitionCondition("foo", 25, 0, 0)); ((VulnerabilityValue)dv3).transitions.Add(t); s = DataValueFactory.XMLSerialize(dv3); DataValue dv4 = DataValueFactory.XMLDeserialize(s); System.Console.WriteLine(s == DataValueFactory.XMLSerialize(dv4)); DataValue dv5 = DataValueFactory.BuildValue("StateTableType"); DataValue dv6 = DataValueFactory.BuildValue("AttributeCollectionType"); ((StateTableValue)dv5).states["foo"] = DataValueFactory.BuildValue("DoubleType"); ((AttributeCollectionValue)dv6).attributes["foo2"] = DataValueFactory.BuildValue("DoubleType"); ((StateTableValue)dv5).states["bar"] = dv6; s = DataValueFactory.XMLSerialize(dv5); DataValue dv7 = DataValueFactory.XMLDeserialize(s); DataValue dv8 = DataValueFactory.BuildValue("StringListType"); ((StringListValue)dv8).strings.Add("Foo"); ((StringListValue)dv8).strings.Add("Bar"); s = DataValueFactory.XMLSerialize(dv8); dv8 = DataValueFactory.XMLDeserialize(s); DataValue dv9 = DataValueFactory.BuildValue("PolygonType"); ((PolygonValue)dv9).points.Add(new PolygonValue.PolygonPoint(0, 0)); ((PolygonValue)dv9).points.Add(new PolygonValue.PolygonPoint(10.234, 34.097)); ((PolygonValue)dv9).points.Add(new PolygonValue.PolygonPoint(10.234, 1.2)); s = DataValueFactory.XMLSerialize(dv9); DataValue dv10 = DataValueFactory.XMLDeserialize(s); }
/// <summary> /// Looking for K-gon with maximum area /// </summary> /// <param name="a_points"></param> public static Polygon2D ComputeMaximumAreaKgon(IEnumerable <Vector2> a_vertices, int pointLimit) { var oldvertices = a_vertices.Distinct(); var vertices = ConvexHull.ComputeConvexHull(oldvertices).Vertices.ToList(); var x0 = vertices[0].x; var y0 = vertices[0].y; foreach (var vertex in vertices) { if (vertex.x < x0) { x0 = vertex.x; } if (vertex.y < y0) { y0 = vertex.y; } } vertices.Sort((x, y) => ((x.x - x0) * (y.y - y0) - (x.y - y0) * (y.x - x0)).CompareTo(0)); float[,,] f = new float [vertices.Count, vertices.Count, pointLimit]; int[,,] g = new int [vertices.Count, vertices.Count, pointLimit]; for (var i = 0; i < vertices.Count; i++) { for (var j = 0; j < vertices.Count; j++) { for (var k = 0; k < pointLimit; k++) { f[i, j, k] = 0f; } } } for (var plimit = 3; plimit <= pointLimit; plimit++) { var limit = plimit - 3; for (var startPoint = 0; startPoint < vertices.Count; startPoint++) { for (var oldEndPoint = startPoint + 1; oldEndPoint < vertices.Count; oldEndPoint++) { for (var newEndPoint = oldEndPoint + 1; newEndPoint < vertices.Count; newEndPoint++) { float total; if (plimit == 3) { total = TriangleArea(vertices[startPoint], vertices[oldEndPoint], vertices[newEndPoint]); } else { total = f[startPoint, oldEndPoint, limit - 1] + TriangleArea(vertices[startPoint], vertices[oldEndPoint], vertices[newEndPoint]); } if (total > f[startPoint, newEndPoint, limit]) { f[startPoint, newEndPoint, limit] = total; g[startPoint, newEndPoint, limit] = oldEndPoint; } } } } } var optStart = 0; var optEnd = 0; float optArea = 0; for (var startPoint = 0; startPoint < vertices.Count; startPoint++) { for (var endPoint = startPoint + 2; endPoint < vertices.Count; endPoint++) { if (f[startPoint, endPoint, pointLimit - 3] > optArea) { optArea = f[startPoint, endPoint, pointLimit - 3]; optStart = startPoint; optEnd = endPoint; } } } Polygon2D m_optimalSolution = new Polygon2D(); m_optimalSolution.AddVertex(vertices[optStart]); m_optimalSolution.AddVertex(vertices[optEnd]); for (var cnt = pointLimit - 3; cnt >= 0; cnt--) { m_optimalSolution.AddVertex(vertices[g[optStart, optEnd, cnt]]); optEnd = g[optStart, optEnd, cnt]; } return(m_optimalSolution); }
/// <summary> /// Looking for K-gon with maximum point number /// </summary> /// <param name="a_points"></param> public static Polygon2D ComputeMaximumPointsKgon(IEnumerable <Vector2> a_vertices, int pointLimit) { var oldvertices = a_vertices.Distinct(); var vertices = oldvertices.ToList(); var x0 = vertices[0].x; var y0 = vertices[0].y; foreach (var vertex in vertices) { if (vertex.x < x0) { x0 = vertex.x; } if (vertex.y < y0) { y0 = vertex.y; } } vertices.Sort((x, y) => ((x.x - x0) * (y.y - y0) - (x.y - y0) * (y.x - x0)).CompareTo(0)); int [,,] f = new int [vertices.Count, vertices.Count, pointLimit]; int [,,] g = new int [vertices.Count, vertices.Count, pointLimit]; Polygon2D [,,] h = new Polygon2D [vertices.Count, vertices.Count, pointLimit]; for (var i = 0; i < vertices.Count; i++) { for (var j = 0; j < vertices.Count; j++) { for (var k = 0; k < pointLimit; k++) { f[i, j, k] = 0; } } } for (var plimit = 3; plimit <= pointLimit; plimit++) { var limit = plimit - 3; for (var startPoint = 0; startPoint < vertices.Count; startPoint++) { for (var oldEndPoint = startPoint + limit + 1; oldEndPoint < vertices.Count; oldEndPoint++) { for (var newEndPoint = oldEndPoint + 1; newEndPoint < vertices.Count; newEndPoint++) { Polygon2D newgon = new Polygon2D(); if (plimit <= 3) { newgon.AddVertex(vertices[startPoint]); newgon.AddVertex(vertices[oldEndPoint]); } else { foreach (var v in h[startPoint, oldEndPoint, limit - 1].Vertices) { newgon.AddVertex(v); } } newgon.AddVertex(vertices[newEndPoint]); newgon = ConvexHull.ComputeConvexHull(newgon); int tot = PolygonPoints(vertices, newgon); if (tot >= f[startPoint, newEndPoint, limit]) { f[startPoint, newEndPoint, limit] = tot; h[startPoint, newEndPoint, limit] = newgon; } /*int total = 0; * if (plimit == 3) * { * total = 3 + TrianglePoints(vertices, startPoint, oldEndPoint, newEndPoint); * } * else * { * total = f[startPoint, oldEndPoint, limit - 1] + 1 + TrianglePoints(vertices, startPoint, oldEndPoint, newEndPoint); * } * * Debug.Log("Total: " + total); * * if (total > f[startPoint, newEndPoint, limit]) * { * f[startPoint, newEndPoint, limit] = total; * g[startPoint, newEndPoint, limit] = oldEndPoint; * } */ } } } } var optStart = 0; var optEnd = 0; int optNumber = 0; for (var startPoint = 0; startPoint < vertices.Count; startPoint++) { for (var endPoint = startPoint + 2; endPoint < vertices.Count; endPoint++) { if (f[startPoint, endPoint, pointLimit - 3] > optNumber) { optNumber = f[startPoint, endPoint, pointLimit - 3]; optStart = startPoint; optEnd = endPoint; } } } Debug.Log(optNumber); Polygon2D m_optimalSolution = new Polygon2D(); m_optimalSolution = h[optStart, optEnd, pointLimit - 3]; m_optimalSolution.SetPointNumber(optNumber); /*m_optimalSolution.AddVertex(vertices[optStart]); * m_optimalSolution.AddVertex(vertices[optEnd]); * * for (var cnt = pointLimit - 3; cnt >= 0; cnt--) * { * Debug.Log("ggggg:" + TrianglePoints(vertices, optStart, optEnd, g[optStart, optEnd, cnt])); * m_optimalSolution.AddVertex(vertices[g[optStart, optEnd, cnt]]); * optEnd = g[optStart, optEnd, cnt]; * } * * Debug.Log("Optimal Vertices: " + m_optimalSolution.Vertices); */ return(m_optimalSolution); }