private void ClipLine(Edge e) { var dy = Height; var dx = Width; var d = (dx > dy) ? dx : dy; var pxMin = -(d - dx) / 2; var pxMax = Width + (d - dx) / 2; var pyMin = -(d - dy) / 2; var pyMax = Height + (d - dy) / 2; Site s1, s2; float x1, x2, y1, y2; Side side; if (Math.Abs(e.A - 1) < Geometry.Tolerance && e.B >= 0) { side = Side.Right; s1 = e.Endpoint[Side.Right]; s2 = e.Endpoint[Side.Left]; } else { side = Side.Left; s1 = e.Endpoint[Side.Left]; s2 = e.Endpoint[Side.Right]; } if (s1 != null && s2 != null) { if ((s1.Y < pyMin && s2.Y > pyMax) || (s1.Y > pyMax && s2.Y < pyMin)) { return; } } if (Math.Abs(e.A - 1) < Geometry.Tolerance) { y1 = pyMin; if (s1 != null && s1.Y > pyMin) { y1 = s1.Y; } if (y1 > pyMax) { return; } x1 = e.C - e.B * y1; y2 = pyMax; if (s2 != null && s2.Y < pyMax) { y2 = s2.Y; } if (y2 < pyMin) { return; } x2 = e.C - e.B * y2; if (((x1 > pxMax) && (x2 > pxMax)) || ((x1 < pxMin) && (x2 < pxMin))) { return; } if (x1 > pxMax) { x1 = pxMax; y1 = (e.C - x1) / e.B; } if (x1 < pxMin) { x1 = pxMin; y1 = (e.C - x1) / e.B; } if (x2 > pxMax) { x2 = pxMax; y2 = (e.C - x2) / e.B; } if (x2 < pxMin) { x2 = pxMin; y2 = (e.C - x2) / e.B; } } else { x1 = pxMin; if (s1 != null && s1.X > pxMin) { x1 = s1.X; } if (x1 > pxMax) { return; } y1 = e.C - e.A * x1; x2 = pxMax; if (s2 != null && s2.X < pxMax) { x2 = s2.X; } if (x2 < pxMin) { return; } y2 = e.C - e.A * x2; if (((y1 > pyMax) && (y2 > pyMax)) || ((y1 < pyMin) && (y2 < pyMin))) { return; } if (y1 > pyMax) { y1 = pyMax; x1 = (e.C - y1) / e.A; } if (y1 < pyMin) { y1 = pyMin; x1 = (e.C - y1) / e.A; } if (y2 > pyMax) { y2 = pyMax; x2 = (e.C - y2) / e.A; } if (y2 < pyMin) { y2 = pyMin; x2 = (e.C - y2) / e.A; } } var p1 = new PointF(x1, y1); var p2 = new PointF(x2, y2); var clipped = CohenSutherland.ClipSegment(new RectangleF(0,0, Width, Height), p1, p2 ); if (clipped != null) { var site1 = new Site(clipped.Item1); var site2 = new Site(clipped.Item2); var s = new Segment(site1, site2) { New = true }; Segments.Add(s); /*if (s1 == null) { e.Endpoint[side] = site1; } if (s2 == null) { e.Endpoint[Side.Other(side)] = site2; }*/ } }
/// <summary> /// Somewhat redundant line clipping routine /// </summary> /// <param name="e"></param> private void ClipLine(Edge e) { var clipped = e.GetClippedEnds(new Rectangle(0, 0, Width, Height)); if (clipped != null) { var site1 = new Site(clipped.Item1); var site2 = new Site(clipped.Item2); var s = new Segment(site1, site2) { New = true }; Segments.Add(s); } }