public bool PolygonClip(GraphPolygon Source, ref GraphPolygon Result, GraphPolygon Clipper) { if ((Clipper.Count < 3) || (Clipper.Convexity == -1)) { MessageBox.Show("Polygon is not convex"); return(false); } int i, j; PointD R; PointD S = new PointD(), F = new PointD(); List <PointD> TempSource = new List <PointD>(); if (curstep == 0) { Result.Vertexes = new List <PointD>(Source.Vertexes); } Result.Closed = true; int t; if (Clipper.Square > 0) { t = 1; } else { t = -1; } Clipper.Add(Clipper[0]); for (i = curstep; i < Clipper.Count - 1; i++) { TempSource.Clear(); F = Result.Vertexes[0]; // end vertex S = Result.Vertexes[0]; // begin vertex if (Geom2d.Square(S, Clipper.Vertexes[i], Clipper.Vertexes[i + 1]) * t >= 0) { TempSource.Add(S); } for (j = 1; j < Result.Vertexes.Count; j++) { if (Geom2d.SegmentLineIntersect(S, Result.Vertexes[j], Clipper.Vertexes[i], Clipper.Vertexes[i + 1], out R) == 1) { TempSource.Add(R); } S = Result.Vertexes[j]; if (Geom2d.Square(S, Clipper.Vertexes[i], Clipper.Vertexes[i + 1]) * t > 0) { TempSource.Add(S); } } if ((TempSource.Count != 0) && (Geom2d.SegmentLineIntersect(S, F, Clipper.Vertexes[i], Clipper.Vertexes[i + 1], out R) == 1)) { TempSource.Add(R); } Result.Clear(); Result.Vertexes.AddRange(TempSource); if (step) { curstep = i + 1; step = false; Clipper.RemoveLast(); return(true); } } Clipper.RemoveLast(); return(true); }
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { Brush brush = new SolidBrush(pnlBackground.BackColor); G.FillRectangle(brush, 0, 0, B.Width, B.Height); PointD P = new PointD(e.X, e.Y); if (rbIntercept.Checked) { if (e.Button == MouseButtons.Left) { if (Polygons[Polygons.Count - 1].Count != 0) { if (IsShiftDown() == true) { PointD PT = new PointD(); PT = Polygons[Polygons.Count - 1][Polygons[Polygons.Count - 1].Count - 1]; if (Math.Abs(e.X - PT.X) < Math.Abs(e.Y - PT.Y)) { P.X = PT.X; P.Y = e.Y; } else { P.X = e.X; P.Y = PT.Y; } } } Polygons[Polygons.Count - 1].Add(P); //Polygons[Polygons.Count-1].DrawLastSegment(); } else if (e.Button == MouseButtons.Right) { Polygons[Polygons.Count - 1].Closed = true; //Polygons[Polygons.Count - 1].DrawBeginEndSegment(); Polygons.Add(new GraphPolygon(B, G, new Pen(pnlLine.BackColor))); } } else { if (e.Button == MouseButtons.Left) { if (ClipPolygon.Closed == true) { ClipPolygon.Clear(); ClipPolygon.Closed = false; } else { if ((ClipPolygon.Count >= 1) && (IsShiftDown() == true)) { Point PT = new Point(); PT = ClipPolygon[ClipPolygon.Count - 1]; if (Math.Abs(e.X - PT.X) < Math.Abs(e.Y - PT.Y)) { P.X = PT.X; P.Y = e.Y; } else { P.X = e.X; P.Y = PT.Y; } } } ClipPolygon.Add(P); ClipPolygon.UpdatePolygon(); } else if (e.Button == MouseButtons.Right) { //ClipPolygon.Add(ClipPolygon[0]); ClipPolygon.Closed = true; } //ClipPolygon.DrawLastSegment(); if (ClipPolygon.Square > 0) { lblBypass.Text = "Против час. стрелки"; } else if (ClipPolygon.Square < 0) { lblBypass.Text = "По час. стрелке"; } else { lblBypass.Text = "---"; } if (ClipPolygon.Convexity > 0) { lblConvex.Text = "Да"; } else if (ClipPolygon.Convexity < 0) { lblConvex.Text = "Нет"; } else { lblConvex.Text = "---"; } if (ClipPolygon.SelfIntersection > 0) { lblSelfintersect.Text = "Есть"; } else if (ClipPolygon.SelfIntersection < 0) { lblSelfintersect.Text = "Нет"; } else { lblSelfintersect.Text = "---"; } } UpdateAll(); //ClipAndDraw(); pictureBox1.Refresh(); }