public object Clone() { Polygon result = new Polygon(); foreach (Point p in points) { result.AddPoint(p.x, p.y); } return(result); }
void OnPressPixelPolygon(int x, int y) { if (isEntering) { Shell.Instance.SetPixel(x, y); pol.AddPoint(x, y); TBPointsCount.Text = "PointsCount - " + pol.Length.ToString(); if (pol.Length > 2) { BTNStroke.IsEnabled = true; BTNFillEO.IsEnabled = true; BTNFillNZW.IsEnabled = true; TBType.Text = "Type: " + (!pol.IsSimple ? "complicated" : ("simple, " + (pol.IsConvex ? "convex" : "nonconvex"))); } } }
public static List <Polygon> ClipWA(Polygon p1, Polygon p2) { //учитваем движение по часовой стрелке if (!p1.IsClockWise) { p1.ReOrient(); } if (!p2.IsClockWise) { p2.ReOrient(); } if (p1.IsInside(p2)) { return new List <Polygon>() { p2 } } ; if (p2.IsInside(p1)) { return new List <Polygon>() { p1 } } ; //собираем 2 массива точек List <Point> S = GetWAPoints(p1, p2); List <Point> C = GetWAPoints(p2, p1); List <Polygon> result = new List <Polygon>(); while (true) { //ищем общую точку Point p = Utils.FindCommonElement <Point>(S, C); if (p == null) { break; } bool isFirst = StartWAWithFirst(p1.points, p2.points, p); Polygon polygon = new Polygon(); List <Point> completed = new List <Point>(); completed.Add(p); do { polygon.AddPoint(p); if (isFirst) { p = S[(S.IndexOf(p) + 1) % S.Count()]; } else { p = C[(C.IndexOf(p) + 1) % C.Count()]; } if (S.Any(point => point.Equals(p)) && C.Any(point => point.Equals(p))) { isFirst = !isFirst; completed.Add(p); } }while (!p.Equals(polygon.points[0])); result.Add(polygon); foreach (Point po in completed) { S.Remove(S.FirstOrDefault(poi => poi.Equals(po))); C.Remove(C.FirstOrDefault(poi => poi.Equals(po))); } } return(result); }
/// <summary> /// нарисовать линию /// </summary> /// <param name="w">толщина</param> /// <param name="color">цвет</param> /// <param name="type">тип законцовки</param> public static void DrawLine(this Shell shell, int x1, int y1, int x2, int y2, int w, Color color, LineCap type = LineCap.FLAP) { int x1old = x1, x2old = x2, y1old = y1, y2old = y2; if (w < 3) { shell.DrawLine(x1, y1, x2, y2, color); return; } double alpha = Math.Atan(((double)(x2 - x1)) / ((double)(y2 - y1))); if (y1 <= y2) { Utils.Swap(ref x1, ref x2); Utils.Swap(ref y1, ref y2); } if (type == LineCap.SQUARE) { x1 += (int)(w * Math.Sin(alpha) / 2); y1 += (int)(w * Math.Cos(alpha) / 2); x2 -= (int)(w * Math.Sin(alpha) / 2); y2 -= (int)(w * Math.Cos(alpha) / 2); } Polygon pol = new Polygon(); double dx = w * Math.Cos(alpha) / 2; double dy = w * Math.Sin(alpha) / 2; // pol.AddPoint(x1 - (int)dx, y1 + (int)dy); if (type == LineCap.ROUND) { int n = w / 2; if (n < 5) { n = 5; } double al = alpha; for (int i = 0; i < n; i++) { al += Math.PI / n; pol.AddPoint(x1 - (int)(w * Math.Cos(al) / 2), y1 + (int)(w * Math.Sin(al) / 2)); } for (int i = 0; i < n; i++) { pol.AddPoint(x2 - (int)(w * Math.Cos(al) / 2), y2 + (int)(w * Math.Sin(al) / 2)); al += Math.PI / n; } } else { pol.AddPoint(x1 - (int)dx, y1 + (int)dy); pol.AddPoint(x1 + (int)dx, y1 - (int)dy); pol.AddPoint(x2 + (int)dx, y2 - (int)dy); pol.AddPoint(x2 - (int)dx, y2 + (int)dy); } shell.FillPolygon(pol, color); //shell.DrawLine(x1old, y1old, x2old, y2old, Colors.Red); }