From ec538d02c24b6c92a1c610cfd9ed1253cde3fd27 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 10 Jun 2011 13:46:10 -0700 Subject: [PATCH] Added intersect and union helpers. --- src/main/java/pythagoras/f/Rectangles.java | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/pythagoras/f/Rectangles.java diff --git a/src/main/java/pythagoras/f/Rectangles.java b/src/main/java/pythagoras/f/Rectangles.java new file mode 100644 index 0000000..87188f7 --- /dev/null +++ b/src/main/java/pythagoras/f/Rectangles.java @@ -0,0 +1,32 @@ +// +// $Id$ + +package pythagoras.f; + +/** + * Rectangle-related utility methods. + */ +public class Rectangles +{ + /** + * Intersects the supplied two rectangles, writing the result into {@link dst}. + */ + public static void intersect (IRectangle src1, IRectangle src2, Rectangle dst) { + float x1 = Math.max(src1.getMinX(), src2.getMinX()); + float y1 = Math.max(src1.getMinY(), src2.getMinY()); + float x2 = Math.min(src1.getMaxX(), src2.getMaxX()); + float y2 = Math.min(src1.getMaxY(), src2.getMaxY()); + dst.setBounds(x1, y1, x2 - x1, y2 - y1); + } + + /** + * Unions the supplied two rectangles, writing the result into {@link dst}. + */ + public static void union (IRectangle src1, IRectangle src2, Rectangle dst) { + float x1 = Math.min(src1.getMinX(), src2.getMinX()); + float y1 = Math.min(src1.getMinY(), src2.getMinY()); + float x2 = Math.max(src1.getMaxX(), src2.getMaxX()); + float y2 = Math.max(src1.getMaxY(), src2.getMaxY()); + dst.setBounds(x1, y1, x2 - x1, y2 - y1); + } +}