/** * Compares this rectangle to the specified rectangle. * * @param other the other rectangle * @return {@code true} if this rectangle equals {@code other}; * {@code false} otherwise */ public override bool Equals(Object other) { if (other == this) { return(true); } if (other == null) { return(false); } if (other.GetType() != this.GetType()) { return(false); } RectHV that = (RectHV)other; if (this.xmin != that.xmin) { return(false); } if (this.ymin != that.ymin) { return(false); } if (this.xmax != that.xmax) { return(false); } if (this.ymax != that.ymax) { return(false); } return(true); }
private void nearest(Node candidate, Point2D goal, RectHV subrange, Node neares) { if (candidate == null) { return; } if (candidate.p.distanceTo(goal) < neares.p.distanceTo(goal)) { neares.p = candidate.p; neares.p.GetNode = candidate.p.GetNode; } if (candidate.vertical) { RectHV one = new RectHV(subrange.Xmin, subrange.Ymin, candidate.x, subrange.Ymax); RectHV tw = new RectHV(candidate.x, subrange.Ymin, subrange.Xmax, subrange.Ymax); if (one.contains(goal)) { nearest(candidate.left, goal, one, neares); if (tw.distanceTo(goal) < neares.p.distanceTo(goal)) { nearest(candidate.right, goal, tw, neares); } } else { nearest(candidate.right, goal, tw, neares); if (one.distanceTo(goal) < neares.p.distanceTo(goal)) { nearest(candidate.left, goal, one, neares); } } } else { RectHV one = new RectHV(subrange.Xmin, subrange.Ymin, subrange.Xmax, candidate.y); RectHV tw = new RectHV(subrange.Xmin, candidate.y, subrange.Xmax, subrange.Ymax); if (one.contains(goal)) { nearest(candidate.left, goal, one, neares); if (tw.distanceTo(goal) < neares.p.distanceTo(goal)) { nearest(candidate.right, goal, tw, neares); } } else { nearest(candidate.right, goal, tw, neares); if (one.distanceTo(goal) < neares.p.distanceTo(goal)) { nearest(candidate.left, goal, one, neares); } } } }
public IEnumerator <Point2D> range(RectHV rect) { if (rect == null) { throw new Exception(); } Queue <Point2D> q = new Queue <Point2D>(); rangeM(root, rect, new RectHV(0, 0, 1, 1), q); foreach (Point2D item in q) { yield return(item); } }
private void rangeM(Node n, RectHV range, RectHV subrange, Queue <Point2D> q) { if (n == null) { return; } if (!range.intersects(subrange)) { return; } if (range.contains(n.p)) { q.Enqueue(n.p); } if (n.vertical) { RectHV one = new RectHV(subrange.Xmin, subrange.Ymin, n.x, subrange.Ymax); RectHV tw = new RectHV(n.x, subrange.Ymin, subrange.Xmax, subrange.Ymax); if (one.intersects(range)) { rangeM(n.left, range, one, q); } if (tw.intersects(range)) { rangeM(n.right, range, tw, q); } } else { RectHV one = new RectHV(subrange.Xmin, subrange.Ymin, subrange.Xmax, n.y); RectHV tw = new RectHV(subrange.Xmin, n.y, subrange.Xmax, subrange.Ymax); if (one.intersects(range)) { rangeM(n.left, range, one, q); } if (tw.intersects(range)) { rangeM(n.right, range, tw, q); } } }
/** * Returns true if the two rectangles intersect. This includes * <em>improper intersections</em> (at points on the boundary * of each rectangle) and <em>nested intersctions</em> * (when one rectangle is contained inside the other) * * @param that the other rectangle * @return {@code true} if this rectangle intersect the argument * rectangle at one or more points */ public bool intersects(RectHV that) { return(this.xmax >= that.xmin && this.ymax >= that.ymin && that.xmax >= this.xmin && that.ymax >= this.ymin); }