From e94ff3de5e9fd7e1713ab552aa032764e4fa6fbd Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 29 Oct 2002 06:07:22 +0000 Subject: [PATCH] Added shiftToContain(). git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1856 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/geom/GeomUtil.java | 30 +++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/java/com/threerings/geom/GeomUtil.java b/src/java/com/threerings/geom/GeomUtil.java index 5b98121be..e21ab412f 100644 --- a/src/java/com/threerings/geom/GeomUtil.java +++ b/src/java/com/threerings/geom/GeomUtil.java @@ -1,9 +1,11 @@ // -// $Id: GeomUtil.java,v 1.2 2002/06/28 01:29:08 mdb Exp $ +// $Id: GeomUtil.java,v 1.3 2002/10/29 06:07:22 mdb Exp $ package com.threerings.geom; import java.awt.Point; +import java.awt.Rectangle; + import com.samskivert.util.StringUtil; /** @@ -112,4 +114,30 @@ public class GeomUtil // we're on the left hand side and if it's zero, we're on the line return dot(p1.x, p1.y, p2.x, p2.y, x, y); } + + /** + * Shifts the position of the tainer rectangle to ensure + * that it contains the tained rectangle. The + * tainer rectangle must be larger than or equal to the + * size of the tained rectangle. + */ + public static void shiftToContain (Rectangle tainer, Rectangle tained) + { + if (tainer.width < tained.width || tainer.height < tained.height) { + throw new IllegalArgumentException( + tainer + " cannot contain " + tained); + } + if (tained.x < tainer.x) { + tainer.x = tained.x; + } + if (tained.y < tainer.y) { + tainer.y = tained.y; + } + if (tained.x + tained.width > tainer.x + tainer.width) { + tainer.x = tained.x - (tainer.width - tained.width); + } + if (tained.y + tained.height > tainer.y + tainer.height) { + tainer.y = tained.y - (tainer.height - tained.height); + } + } }