//private private CohenSutherlandBitCode ComputeOutCode(PreciseRectangle r, PrecisePoint p, double epsilon) { CohenSutherlandBitCode code = CohenSutherlandBitCode.INSIDE; if (r.Left - p.X >= epsilon) { code |= CohenSutherlandBitCode.LEFT; } else if (p.X - r.Right >= epsilon) { code |= CohenSutherlandBitCode.RIGHT; } if (r.Top - p.Y >= epsilon) { code |= CohenSutherlandBitCode.BOTTOM; } else if (p.Y - r.Bottom >= epsilon) { code |= CohenSutherlandBitCode.TOP; } return(code); }
public PrecisePoint Intersects(PreciseRectangle rectangle, double epsilon = 1e-4) { if (epsilon <= 0.0d) { throw new InvalidOperationException("epsilon cannot be <= 0"); } PrecisePoint p0 = (PrecisePoint)start.Clone(); PrecisePoint p1 = (PrecisePoint)end.Clone(); CohenSutherlandBitCode outCode0 = ComputeOutCode(rectangle, p0, epsilon); CohenSutherlandBitCode outCode1 = ComputeOutCode(rectangle, p1, epsilon); while (true) { if (0 == (outCode0 | outCode1)) { // Both points are inside return((!p0.Equals(start) || Intersects(p0, epsilon)) ? p0 : null); } else if (0 != (outCode0 & outCode1)) { // Both points are outside and does not intersect return((!p0.Equals(start) || Intersects(p0, epsilon)) ? p0 : null); } else { PrecisePoint p = new PrecisePoint(); CohenSutherlandBitCode outCodeOut = (outCode0 != 0) ? outCode0 : outCode1; if (0 != (outCodeOut & CohenSutherlandBitCode.TOP)) { p.X = p0.X + (p1.X - p0.X) * (rectangle.Bottom - p0.Y) / (p1.Y - p0.Y); p.Y = rectangle.Bottom; } else if (0 != (outCodeOut & CohenSutherlandBitCode.BOTTOM)) { p.X = p0.X + (p1.X - p0.X) * (rectangle.Top - p0.Y) / (p1.Y - p0.Y); p.Y = rectangle.Top; } else if (0 != (outCodeOut & CohenSutherlandBitCode.RIGHT)) { p.Y = p0.Y + (p1.Y - p0.Y) * (rectangle.Right - p0.X) / (p1.X - p0.X); p.X = rectangle.Right; } else if (0 != (outCodeOut & CohenSutherlandBitCode.LEFT)) { p.Y = p0.Y + (p1.Y - p0.Y) * (rectangle.Left - p0.X) / (p1.X - p0.X); p.X = rectangle.Left; } if (outCodeOut == outCode0) { outCode0 = ComputeOutCode(rectangle, p0 = p, epsilon); } else { outCode1 = ComputeOutCode(rectangle, p1 = p, epsilon); } } } }