From c9c336309b31e596372b1477e9d36f60cc96c0d2 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 20 Dec 2011 19:09:19 -0800 Subject: [PATCH] Tidied javadocs, generated double implementation. --- src/main/java/pythagoras/d/Rectangles.java | 38 ++++++++++++++++++++++ src/main/java/pythagoras/f/Rectangles.java | 23 +++++++------ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/main/java/pythagoras/d/Rectangles.java b/src/main/java/pythagoras/d/Rectangles.java index 043770e..636e734 100644 --- a/src/main/java/pythagoras/d/Rectangles.java +++ b/src/main/java/pythagoras/d/Rectangles.java @@ -30,4 +30,42 @@ public class Rectangles double y2 = Math.max(src1.maxY(), src2.maxY()); dst.setBounds(x1, y1, x2 - x1, y2 - y1); } + + /** + * Computes the point inside the bounds of the rectangle that's closest to the given point, + * writing the result into {@code out}. + * @return {@code out} for call chaining convenience. + */ + public static Point closestInteriorPoint (IRectangle r, IPoint p, Point out) { + out.set(MathUtil.clamp(p.x(), r.minX(), r.maxX()), + MathUtil.clamp(p.y(), r.minY(), r.maxY())); + return out; + } + + /** + * Computes and returns the point inside the bounds of the rectangle that's closest to the + * given point. + */ + public static Point closestInteriorPoint (IRectangle r, IPoint p) { + return closestInteriorPoint(r, p, new Point()); + } + + /** + * Returns the squared Euclidean distance between the given point and the nearest point inside + * the bounds of the given rectangle. If the supplied point is inside the rectangle, the + * distance will be zero. + */ + public static double pointRectDistanceSq (IRectangle r, IPoint p) { + Point p2 = closestInteriorPoint(r, p); + return Points.distanceSq(p.x(), p.y(), p2.x, p2.y); + } + + /** + * Returns the Euclidean distance between the given point and the nearest point inside the + * bounds of the given rectangle. If the supplied point is inside the rectangle, the distance + * will be zero. + */ + public static double pointRectDistance (IRectangle r, IPoint p) { + return Math.sqrt(pointRectDistanceSq(r, p)); + } } diff --git a/src/main/java/pythagoras/f/Rectangles.java b/src/main/java/pythagoras/f/Rectangles.java index c1e54c0..245e5d3 100644 --- a/src/main/java/pythagoras/f/Rectangles.java +++ b/src/main/java/pythagoras/f/Rectangles.java @@ -30,28 +30,30 @@ public class Rectangles float y2 = Math.max(src1.maxY(), src2.maxY()); dst.setBounds(x1, y1, x2 - x1, y2 - y1); } - + /** - * Computes the Point on the rectangle that's closest to the given point, writing - the result int {@code out} + * Computes the point inside the bounds of the rectangle that's closest to the given point, + * writing the result into {@code out}. + * @return {@code out} for call chaining convenience. */ - public static Point closestInteriorPoint (IRectangle r, IPoint p, Point out) - { + public static Point closestInteriorPoint (IRectangle r, IPoint p, Point out) { out.set(MathUtil.clamp(p.x(), r.minX(), r.maxX()), MathUtil.clamp(p.y(), r.minY(), r.maxY())); return out; } /** - * Computes the Point on the rectangle that's closest to the given point + * Computes and returns the point inside the bounds of the rectangle that's closest to the + * given point. */ public static Point closestInteriorPoint (IRectangle r, IPoint p) { return closestInteriorPoint(r, p, new Point()); } /** - * Returns the squared Euclidean distance between the given point and the nearest point on the - * given rectangle. + * Returns the squared Euclidean distance between the given point and the nearest point inside + * the bounds of the given rectangle. If the supplied point is inside the rectangle, the + * distance will be zero. */ public static float pointRectDistanceSq (IRectangle r, IPoint p) { Point p2 = closestInteriorPoint(r, p); @@ -59,8 +61,9 @@ public class Rectangles } /** - * Returns the Euclidean distance between the given point and the nearest point on the - * given rectangle. + * Returns the Euclidean distance between the given point and the nearest point inside the + * bounds of the given rectangle. If the supplied point is inside the rectangle, the distance + * will be zero. */ public static float pointRectDistance (IRectangle r, IPoint p) { return FloatMath.sqrt(pointRectDistanceSq(r, p));