private void clipBtn_Click(object sender, RoutedEventArgs e) { CGPolygon polygon = new CGPolygon(PolygonPoints); List <CGLine> segments = new List <CGLine>(); foreach (var l in this.Lines) { Point a = new Point(l.X1, l.Y1); Point b = new Point(l.X2, l.Y2); CGLine S = new CGLine(a, b); segments.Add(S); } List <Point> pList = new List <Point>(); var x = polygon.Clip(segments); foreach (var item in x) { Line ln = new Line(); ln.X1 = item.A.X; ln.Y1 = item.A.Y; ln.X2 = item.B.X; ln.Y2 = item.B.Y; ln.Stroke = Brushes.Pink; ln.StrokeThickness = 2; mainContainer.Children.Add(ln); } }
public double IntersectionParameter(CGLine that) { var edge = that; var segmentToEdge = edge.A.Substract(this.A); var segmentDir = this.Direction; var edgeDir = edge.Direction; var t = edgeDir.CrossProduct(segmentToEdge) / edgeDir.CrossProduct(segmentDir); if (double.IsNaN(t)) { t = 0; } return(t); }
private CGLine ClipLine(CGLine line) { double tE = 0, tL = 1; var dir = line.Direction; Point nL; bool PE = false; foreach (var edge in this.Edges) { nL = edge.Normal; var t = line.IntersectionParameter(edge); double slope = nL.DotProduct(dir); PE = (slope < 0 ? true : false); //potentionally entering if (PE) { //Choose for parameter tE the largest t ≤ 1 from the PE set if (t > tE) { tE = t; } //potentionally leaving } else { //Choose for parameter tL the smallest t ≥ 0 from the PL set if (t < tL) { tL = t; } } } //Line is discarded if tE > tL if (tE > tL) { return(null); } //Clipped line is defined by [tE , tL] return(line.Morph(tE, tL)); }
public List <CGLine> Clip(List <CGLine> lines) { if (!this.IsConvex()) { this.Points.Reverse(); } var clippedLines = new List <CGLine>(); foreach (var line in lines) { CGLine clippedSegment = this.ClipLine(line); if (clippedSegment != null) { clippedLines.Add(clippedSegment); } } return(clippedLines); }