diff options
author | Sven Gothel <[email protected]> | 2023-01-31 04:00:04 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-01-31 04:00:04 +0100 |
commit | 54760cd667365277c9c2473350bdc56ba5398348 (patch) | |
tree | c18abdad27f1f2cadf6431734e0807f62ea95321 /src/nativewindow/classes/com/jogamp | |
parent | bebb7e9b078f6eaed478143ffbdeeece5ca0e037 (diff) |
Nativewindow Rectangle*: Add contains, scale*(float..) and make union(List<Rectangle*>) public; Fix union/intersection 'off-by-1' for pos2.
Diffstat (limited to 'src/nativewindow/classes/com/jogamp')
-rw-r--r-- | src/nativewindow/classes/com/jogamp/nativewindow/util/Rectangle.java | 73 | ||||
-rw-r--r-- | src/nativewindow/classes/com/jogamp/nativewindow/util/RectangleImmutable.java | 15 |
2 files changed, 71 insertions, 17 deletions
diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/util/Rectangle.java b/src/nativewindow/classes/com/jogamp/nativewindow/util/Rectangle.java index 0b853b08f..2fa68f67c 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/util/Rectangle.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/util/Rectangle.java @@ -94,58 +94,54 @@ public class Rectangle implements Cloneable, RectangleImmutable { @Override public final Rectangle union(final RectangleImmutable r) { - return union(r.getX(), r.getY(), r.getX() + r.getWidth(), r.getY() + r.getHeight()); + return union(r.getX(), r.getY(), r.getX() + r.getWidth() - 1, r.getY() + r.getHeight() - 1); } @Override public final Rectangle union(final int rx1, final int ry1, final int rx2, final int ry2) { final int x1 = Math.min(x, rx1); final int y1 = Math.min(y, ry1); - final int x2 = Math.max(x + width, rx2); - final int y2 = Math.max(y + height, ry2); - return new Rectangle(x1, y1, x2 - x1, y2 - y1); + final int x2 = Math.max(x + width - 1, rx2); + final int y2 = Math.max(y + height - 1, ry2); + return new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1); } - /** - * Calculates the union of the given rectangles, stores it in this instance and returns this instance. - * @param rectangles given list of rectangles - * @return this instance holding the union of given rectangles. - */ + @Override public final Rectangle union(final List<RectangleImmutable> rectangles) { int x1=Integer.MAX_VALUE, y1=Integer.MAX_VALUE; int x2=Integer.MIN_VALUE, y2=Integer.MIN_VALUE; for(int i=rectangles.size()-1; i>=0; i--) { final RectangleImmutable vp = rectangles.get(i); x1 = Math.min(x1, vp.getX()); - x2 = Math.max(x2, vp.getX() + vp.getWidth()); + x2 = Math.max(x2, vp.getX() + vp.getWidth()); // exclusive y1 = Math.min(y1, vp.getY()); - y2 = Math.max(y2, vp.getY() + vp.getHeight()); + y2 = Math.max(y2, vp.getY() + vp.getHeight()); // exclusive } return new Rectangle(x1, y1, x2 - x1, y2 - y1); } @Override public final Rectangle intersection(final RectangleImmutable r) { - return intersection(r.getX(), r.getY(), r.getX() + r.getWidth(), r.getY() + r.getHeight()); + return intersection(r.getX(), r.getY(), r.getX() + r.getWidth() - 1, r.getY() + r.getHeight() - 1); } @Override public final Rectangle intersection(final int rx1, final int ry1, final int rx2, final int ry2) { final int x1 = Math.max(x, rx1); final int y1 = Math.max(y, ry1); - final int x2 = Math.min(x + width, rx2); - final int y2 = Math.min(y + height, ry2); + final int x2 = Math.min(x + width - 1, rx2); + final int y2 = Math.min(y + height - 1, ry2); final int ix, iy, iwidth, iheight; if( x2 < x1 ) { ix = 0; iwidth = 0; } else { ix = x1; - iwidth = x2 - x1; + iwidth = x2 - x1 + 1; } if( y2 < y1 ) { iy = 0; iheight = 0; } else { iy = y1; - iheight = y2 - y1; + iheight = y2 - y1 + 1; } return new Rectangle (ix, iy, iwidth, iheight); } @@ -157,6 +153,21 @@ public class Rectangle implements Cloneable, RectangleImmutable { return sqI / sqT; } + @Override + public final boolean contains(final RectangleImmutable r) { + final int x2 = x + width - 1; + final int y2 = y + height - 1; + final int rx1 = r.getX(); + final int ry1 = r.getY(); + final int rx2 = rx1 + r.getWidth() - 1; + final int ry2 = ry1 + r.getHeight() - 1; + if( rx1 < x || ry1 < y || + rx2 > x2 || ry2 > y2 ) { + return false; + } + return true; + } + /** * Scale this instance's components, * i.e. multiply them by the given scale factors. @@ -173,6 +184,21 @@ public class Rectangle implements Cloneable, RectangleImmutable { } /** + * Scale this instance's components, + * i.e. multiply them by the given scale factors (rounded). + * @param sx scale factor for x + * @param sy scale factor for y + * @return this instance for scaling + */ + public final Rectangle scale(final float sx, final float sy) { + x = (int)( x * sx + 0.5f ); + y = (int)( y * sy + 0.5f ); + width = (int)( width * sx + 0.5f ); + height = (int)( height * sy + 0.5f ); + return this; + } + + /** * Inverse scale this instance's components, * i.e. divide them by the given scale factors. * @param sx inverse scale factor for x @@ -187,6 +213,21 @@ public class Rectangle implements Cloneable, RectangleImmutable { return this; } + /** + * Inverse scale this instance's components, + * i.e. divide them by the given scale factors (rounded). + * @param sx inverse scale factor for x + * @param sy inverse scale factor for y + * @return this instance for scaling + */ + public final Rectangle scaleInv(final float sx, final float sy) { + x = (int)( x / sx + 0.5f ); + y = (int)( y / sy + 0.5f ); + width = (int)( width / sx + 0.5f ); + height = (int)( height / sy + 0.5f ); + return this; + } + @Override public int compareTo(final RectangleImmutable d) { { diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/util/RectangleImmutable.java b/src/nativewindow/classes/com/jogamp/nativewindow/util/RectangleImmutable.java index a5508c5f6..35779ad1f 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/util/RectangleImmutable.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/util/RectangleImmutable.java @@ -28,23 +28,34 @@ package com.jogamp.nativewindow.util; +import java.util.List; + import com.jogamp.common.type.WriteCloneable; -/** Immutable Rectangle interface */ +/** Immutable Rectangle interface, with its position on the top-left. */ public interface RectangleImmutable extends WriteCloneable, Comparable<RectangleImmutable> { int getHeight(); int getWidth(); + /** x-position, left of rectangle. */ int getX(); + /** y-position, top of rectangle. */ int getY(); /** Returns a new {@link Rectangle} instance containing the union of this rectangle and the given rectangle. */ Rectangle union(final RectangleImmutable r); /** Returns a new {@link Rectangle} instance containing the union of this rectangle and the given coordinates. */ Rectangle union(final int rx1, final int ry1, final int rx2, final int ry2); + /** + * Calculates the union of the given rectangles, stores it in this instance and returns this instance. + * @param rectangles given list of rectangles + * @return this instance holding the union of given rectangles. + */ + Rectangle union(final List<RectangleImmutable> rectangles); + /** Returns a new {@link Rectangle} instance containing the intersection of this rectangle and the given rectangle. */ Rectangle intersection(RectangleImmutable r); /** Returns a new {@link Rectangle} instance containing the intersection of this rectangle and the given coordinates. */ @@ -61,6 +72,8 @@ public interface RectangleImmutable extends WriteCloneable, Comparable<Rectangle */ float coverage(RectangleImmutable r); + boolean contains(RectangleImmutable r); + /** * <p> * Compares square of size 1st, if equal the square of position. |