/** * Calculates the squared value of the distance between the minimum and maximum coordinates of this * bounding box. * * @return the squared value of this bounding box diagonal. * @throws RuntimeException if this bounding box is improper according to {@link #checkBounds()}. */ default double getDiagonalLengthSquared() { checkBounds(); return getMinPoint().distanceSquared(getMaxPoint()); }
/** * Calculates the coordinate of the center of this bounding box and stores it in the given * {@code centerToPack}. * * @param centerToPack point 2D in which the center of this bounding box is stored. Modified. * @throws RuntimeException if this bounding box is improper according to {@link #checkBounds()}. */ default void getCenterPoint(Point2DBasics centerToPack) { checkBounds(); centerToPack.interpolate(getMinPoint(), getMaxPoint(), 0.5); }
/** * Computes the coordinates of the two intersections between a line and this bounding box. * <p> * In the case the line and the bounding box do not intersect, this method returns {@code 0} and * {@code firstIntersectionToPack} and {@code secondIntersectionToPack} are set to * {@link Double#NaN}. * </p> * * @param pointOnLine a point located on the infinitely long line. Not modified. * @param lineDirection the line direction. Not modified. * @param firstIntersectionToPack the coordinate of the first intersection. Can be {@code null}. * Modified. * @param secondIntersectionToPack the coordinate of the second intersection. Can be {@code null}. * Modified. * @return the number of intersections between the line and this bounding box. It is either equal to * 0 or 2. * @throws RuntimeException if this bounding box is improper according to {@link #checkBounds()}. */ default int intersectionWithLine2D(Point2DReadOnly pointOnLine, Vector2DReadOnly lineDirection, Point2DBasics firstIntersectionToPack, Point2DBasics secondIntersectionToPack) { checkBounds(); return intersectionBetweenLine2DAndBoundingBox2D(getMinPoint(), getMaxPoint(), pointOnLine, lineDirection, firstIntersectionToPack, secondIntersectionToPack); }
Point2DBasics secondIntersectionToPack) checkBounds(); return intersectionBetweenRay2DAndBoundingBox2D(getMinPoint(), getMaxPoint(), rayOrigin, rayDirection, firstIntersectionToPack, secondIntersectionToPack);
Point2DBasics secondIntersectionToPack) checkBounds(); return intersectionBetweenLineSegment2DAndBoundingBox2D(getMinPoint(), getMaxPoint(), lineSegmentStart, lineSegmentEnd, firstIntersectionToPack, secondIntersectionToPack);
/** * Tests if the {@code query} is located inside this bounding box. * <p> * The query is considered to be outside if located exactly on an edge of this bounding box. * </p> * * @param x the x-coordinate of the query to test if it is located inside this bounding box. Not * modified. * @param y the y-coordinate of the query to test if it is located inside this bounding box. Not * modified. * @return {@code true} if the query is inside, {@code false} if outside or located on an edge of * this bounding box. * @throws RuntimeException if this bounding box is improper according to {@link #checkBounds()}. */ default boolean isInsideExclusive(double x, double y) { checkBounds(); if (x <= getMinX() || x >= getMaxX()) return false; if (y <= getMinY() || y >= getMaxY()) return false; return true; }
/** * Tests if the {@code query} is located inside this bounding box. * <p> * The query is considered to be inside if located exactly on an edge of this bounding box. * </p> * * @param x the x-coordinate of the query to test if it is located inside this bounding box. Not * modified. * @param y the y-coordinate of the query to test if it is located inside this bounding box. Not * modified. * @return {@code true} if the query is inside or located on an edge of this bounding box, * {@code false} if outside. * @throws RuntimeException if this bounding box is improper according to {@link #checkBounds()}. */ default boolean isInsideInclusive(double x, double y) { checkBounds(); if (x < getMinX() || x > getMaxX()) return false; if (y < getMinY() || y > getMaxY()) return false; return true; }
checkBounds(); if (x <= getMinX() - epsilon || x >= getMaxX() + epsilon) return false;
/** * Tests if this bounding box and {@code other} intersects. * <p> * The two bounding boxes are considered to not be intersecting if they share a corner or an edge. * </p> * * @param other the other bounding box to test if it is intersecting with this bounding box. Not * Modified. * @return {@code true} if the two bounding boxes intersect, {@code false} otherwise. */ default boolean intersectsExclusive(BoundingBox2DReadOnly other) { checkBounds(); if (other.getMinX() >= getMaxX() || other.getMaxX() <= getMinX()) return false; if (other.getMinY() >= getMaxY() || other.getMaxY() <= getMinY()) return false; return true; }
/** * Tests if this bounding box and {@code other} intersects. * <p> * The two bounding boxes are considered to be intersecting if they share a corner or an edge. * </p> * * @param other the other bounding box to test if it is intersecting with this bounding box. Not * Modified. * @return {@code true} if the two bounding boxes intersect, {@code false} otherwise. * @throws RuntimeException if this bounding box is improper according to {@link #checkBounds()}. */ default boolean intersectsInclusive(BoundingBox2DReadOnly other) { checkBounds(); if (other.getMinX() > getMaxX() || other.getMaxX() < getMinX()) return false; if (other.getMinY() > getMaxY() || other.getMaxY() < getMinY()) return false; return true; }
/** * Tests if this bounding box and {@code other} intersects. * <p> * <ul> * <li>if {@code epsilon == 0}, this method is equivalent to * {@link #intersectsExclusive(BoundingBox2DReadOnly)}. * <li>if {@code epsilon > 0}, the size of this bounding box is scaled up by shifting the edges of * {@code epsilon} toward the outside. * <li>if {@code epsilon > 0}, the size of this bounding box is scaled down by shifting the edges of * {@code epsilon} toward the inside. * </ul> * </p> * * @param other the other bounding box to test if it is intersecting with this bounding box. Not * Modified. * @param epsilon the tolerance to use in this test. * @return {@code true} if the two bounding boxes intersect, {@code false} otherwise. * @throws RuntimeException if this bounding box is improper according to {@link #checkBounds()}. */ default boolean intersectsEpsilon(BoundingBox2DReadOnly other, double epsilon) { checkBounds(); if (other.getMinX() >= getMaxX() + epsilon || other.getMaxX() <= getMinX() - epsilon) return false; if (other.getMinY() >= getMaxY() + epsilon || other.getMaxY() <= getMinY() - epsilon) return false; return true; }
/** * Performs a linear interpolation between this bounding box minimum and maximum coordinates using * independent parameters {@code xParameter} and {@code yParameter} for the x-axis and y-axis * respectively. The result is stored in {@code pointToPack}. * <p> * <ul> * <li>{@code (xParameter == 0)} results in: {@code (pointToPack.getX() == this.getMinX())}. * <li>{@code (xParameter == 1)} results in: {@code (pointToPack.getX() == this.getMaxX())}. * <li>{@code (yParameter == 0)} results in: {@code (pointToPack.getY() == this.getMinY())}. * <li>{@code (yParameter == 1)} results in: {@code (pointToPack.getY() == this.getMaxY())}. * </ul> * </p> * * @param xParameter the parameter to use for the interpolation along the x-axis. * @param yParameter the parameter to use for the interpolation along the y-axis. * @param pointToPack the point 2D in which the result is stored. Modified. * @throws RuntimeException if this bounding box is improper according to {@link #checkBounds()}. */ default void getPointGivenParameters(double xParameter, double yParameter, Point2DBasics pointToPack) { checkBounds(); pointToPack.setX(getMinX() + xParameter * (getMaxX() - getMinX())); pointToPack.setY(getMinY() + yParameter * (getMaxY() - getMinY())); }