public boolean insert(Point2D p) { if (p.equals(value)) { return false; } if (onLeft(p)) { if (left == null) { final TreeNode treeNode = newNode(p, this); treeNode.rect = even ? new RectHV(rect.xmin(), rect.ymin(), value.x(), rect.ymax()) : new RectHV(rect.xmin(), rect.ymin(), rect.xmax(), value.y()); left = treeNode; return true; } else { return left.insert(p); } } else if (onRight(p)) { if (right == null) { TreeNode treeNode = newNode(p, this); treeNode.rect = even ? new RectHV(rect.xmin(), value.y(), rect.xmax(), rect.ymax()) : new RectHV(rect.xmin(), value.y(), rect.xmax(), rect.ymax()); right = treeNode; return true; } else { return right.insert(p); } } return false; }
private boolean onTheLeft(final Point2D point, final RectHV rect, final boolean vertical) { if (vertical) { return rect.xmax() < point.x(); } else { return rect.ymax() < point.y(); } }
// all points that are inside the rectangle public Iterable<Point2D> range(RectHV rect) { if (root == null) { return new ArrayList<>(); } requireNonNull(rect); final Point2D lower = new Point2D(rect.xmin(), rect.ymin()); final Point2D higher = new Point2D(rect.xmax(), rect.ymax()); return root.subSet(lower, higher); }