public static Polygon Grid2Cartes(Polygon gPolygon, int gridX, int gridY) { PointF[] pointList = gPolygon.ToPointFArray(); List<Point2D> cPointList = new List<Point2D>(); for (int i = 0; i < pointList.Length; i++) { Point2D pt = new Point2D(gridX * 256 + pointList[i].X, gridY * 256 + pointList[i].Y); cPointList.Add(pt); } Polygon cPolygon = new Polygon(cPointList); return cPolygon; }
public void AddPoint(Point2D pt, int index) { pointList.Insert(index, pt); }
public static Polygon GeneratePolygon(Vector3 scale, Vector3 pos, Quaternion quat) { Matrix rotationMat = QuaternionToMatrix(quat); Matrix transMat = new Matrix(3,1); transMat[0,0] = pos.X; transMat[1,0] = pos.Y; transMat[2,0] = pos.Z; // generate original Point list List<Point3D> oriPointList = new List<Point3D>(8); oriPointList.Add(new Point3D(scale.X / 2, scale.Y / 2, scale.Z / 2)); oriPointList.Add(new Point3D(scale.X / 2, scale.Y / 2, -scale.Z / 2)); oriPointList.Add(new Point3D(scale.X / 2, -scale.Y / 2, scale.Z / 2)); oriPointList.Add(new Point3D(scale.X / 2, -scale.Y / 2, -scale.Z / 2)); oriPointList.Add(new Point3D(-scale.X / 2, scale.Y / 2, scale.Z / 2)); oriPointList.Add(new Point3D(-scale.X / 2, scale.Y / 2, -scale.Z / 2)); oriPointList.Add(new Point3D(-scale.X / 2, -scale.Y / 2, scale.Z / 2)); oriPointList.Add(new Point3D(-scale.X / 2, -scale.Y / 2, -scale.Z / 2)); // rotate the point by the rotationMat List<Point3D> rotPointList = new List<Point3D>(8); foreach (var point in oriPointList) { Point3D rPoint = new Point3D(Matrix.MatMul(rotationMat, point.ToMatrix())); rotPointList.Add(rPoint); } // translate the point by translatMat List<Point3D> transPointList = new List<Point3D>(8); foreach (var point in rotPointList) { Point3D tPoint = new Point3D(Matrix.MatAdd(transMat, point.ToMatrix())); transPointList.Add(tPoint); } // project points to xy plane List<Point2D> point2dList = new List<Point2D>(); foreach (var point3d in transPointList) { Point2D point2d = new Point2D(point3d.x, point3d.y); point2dList.Add(point2d); } Polygon polygon = ConvenxHull(point2dList); return polygon; }
private static bool TurnLeft(Point2D pt1, Point2D pt2, Point2D pt3) { Vector2 vt1 = new Vector2(pt2.x - pt1.x, pt2.y - pt1.y); Vector2 vt2 = new Vector2(pt3.x - pt2.x, pt3.y - pt2.y); double det = vt1.X * vt2.Y - vt1.Y * vt2.X; if (det > 0) return true; else return false; }
// Calculte the 2Dconvcex hull of the point list on XY plane private static Polygon ConvenxHull(List<Point2D> pointList) { List<Point2D> polyPointList = new List<Point2D>(); pointList.Sort(ComparePointByY); Point2D p0 = new Point2D(pointList[0].x, pointList[0].y); polyPointList.Add(p0); pointList.RemoveAt(0); tempOrigin = p0; pointList.Sort(ComparePointByPolarAngle); //reogranize the pointlist remove duplicate points which have same polar angles int index = 0; while (index + 1 < pointList.Count) { double rad1 = Radius(pointList[index].x - tempOrigin.x, pointList[index].y - tempOrigin.y); double rad2 = Radius(pointList[index + 1].x - tempOrigin.x, pointList[index + 1].y - tempOrigin.y); if (rad1 == rad2) { double dist1 = Distance(pointList[index].x - tempOrigin.x, pointList[index].y - tempOrigin.y); double dist2 = Distance(pointList[index + 1].x - tempOrigin.x, pointList[index + 1].y - tempOrigin.y); if (dist1 < dist2) pointList.RemoveAt(index); else pointList.RemoveAt(index + 1); } else index++; } Point2D p1 = pointList[0]; pointList.RemoveAt(0); Point2D p2 = pointList[0]; pointList.RemoveAt(0); polyPointList.Add(p1); polyPointList.Add(p2); for (int i = 0; i < pointList.Count; i++) { while (TurnLeft(polyPointList[polyPointList.Count - 2], polyPointList[polyPointList.Count - 1], pointList[i]) == false) polyPointList.RemoveAt(polyPointList.Count - 1); polyPointList.Add(pointList[i]); } return new Polygon(polyPointList); }
private static int ComparePointByY(Point2D pt1, Point2D pt2) { if (pt1.y != pt2.y) return pt1.y.CompareTo(pt2.y); else return pt1.x.CompareTo(pt2.x); }
private static int ComparePointByPolarAngle(Point2D pt1, Point2D pt2) { double rad1 = Radius(pt1.x - tempOrigin.x, pt1.y - tempOrigin.y); double rad2 = Radius(pt2.x - tempOrigin.x, pt2.y - tempOrigin.y); return rad1.CompareTo(rad2); }