From e0556e338ddab074eabc02cb6232d6ac0480b2f3 Mon Sep 17 00:00:00 2001 From: Tobias Nett Date: Thu, 1 Apr 2021 20:51:05 +0200 Subject: [PATCH] feat: add 'getMin'/'getMax' to Rectangled --- .../org/terasology/joml/geom/Rectangled.java | 315 +++++++----------- 1 file changed, 113 insertions(+), 202 deletions(-) diff --git a/joml-geometry/src/main/java/org/terasology/joml/geom/Rectangled.java b/joml-geometry/src/main/java/org/terasology/joml/geom/Rectangled.java index b12b35b..1dddede 100644 --- a/joml-geometry/src/main/java/org/terasology/joml/geom/Rectangled.java +++ b/joml-geometry/src/main/java/org/terasology/joml/geom/Rectangled.java @@ -8,7 +8,6 @@ import org.joml.Vector2d; import org.joml.Vector2dc; import org.joml.Vector2fc; -import org.joml.Vector2ic; import java.io.Externalizable; import java.io.IOException; @@ -48,8 +47,7 @@ public Rectangled() { /** * Create a new {@link Rectangledc} as a copy of the given source. * - * @param source - * the {@link Rectangledc} to copy from + * @param source the {@link Rectangledc} to copy from */ public Rectangled(Rectangledc source) { this.minX = source.minX(); @@ -61,10 +59,8 @@ public Rectangled(Rectangledc source) { /** * Create a new {@link Rectangled} with the given min and max corner coordinates. * - * @param min - * the minimum coordinates - * @param max - * the maximum coordinates + * @param min the minimum coordinates + * @param max the maximum coordinates */ public Rectangled(Vector2dc min, Vector2dc max) { this.minX = min.x(); @@ -76,14 +72,10 @@ public Rectangled(Vector2dc min, Vector2dc max) { /** * Create a new {@link Rectangled} with the given minimum and maximum corner coordinates. * - * @param minX - * the x coordinate of the minimum corner - * @param minY - * the y coordinate of the minimum corner - * @param maxX - * the x coordinate of the maximum corner - * @param maxY - * the y coordinate of the maximum corner + * @param minX the x coordinate of the minimum corner + * @param minY the y coordinate of the minimum corner + * @param maxX the x coordinate of the maximum corner + * @param maxY the y coordinate of the maximum corner */ public Rectangled(double minX, double minY, double maxX, double maxY) { this.minX = minX; @@ -124,6 +116,10 @@ public double minY() { return this.minY; } + public Vector2d getMin(Vector2d dest) { + return dest.set(minX, minY); + } + @Override public double maxX() { return this.maxX; @@ -134,6 +130,10 @@ public double maxY() { return this.maxY; } + public Vector2d getMax(Vector2d dest) { + return dest.set(maxX, maxY); + } + @Override public double getSizeX() { return this.maxX - this.minX; @@ -159,7 +159,7 @@ public Rectangled setSize(double dx, double dy, Rectangled dest) { } public Rectangled setSize(double dx, double dy) { - return setSize(dx,dy, this); + return setSize(dx, dy, this); } @Override @@ -181,71 +181,49 @@ public Rectangled setSize(Vector2dc size) { } /** - * Set this rectangle to be a clone of source. + * Set this {@link Rectangled} to be a clone of source. * - * @param source the rectangle to copy from + * @param source the {@link Rectangledc} to copy from * @return this */ public Rectangled set(Rectangledc source) { - return set(source.minX(), source.minY(), source.maxX(), source.maxY()); - } - - /** - * Set the minimum and maximum corner of this rectangle to given values. - * - * @param min the minimum corner - * @param min the maximum corner - * @return this - */ - public Rectangled set(Vector2dc min, Vector2dc max) { - return set(min.x(), min.y(), max.x(), max.y()); + this.minX = source.minX(); + this.minY = source.minY(); + this.maxX = source.maxX(); + this.maxY = source.maxY(); + return this; } /** - * Set the minimum and maximum corner of this rectangle to given values. + * Set this rectangle to the given point (x, y) with zero size. * - * @param minX the minimum x coordinate - * @param minY the minimum y coordinate - * @param maxX the maximum x coordinate - * @param maxY the maximum y coordinate + * @param x the x coordinate of both minimum and maximum corner + * @param y the y coordinate of both minimum and maximum corner * @return this */ - public Rectangled set(double minX, double minY, double maxX, double maxY) { - this.minX = minX; - this.minY = minY; - this.maxX = maxX; - this.maxY = maxY; + public Rectangled set(double x, double y) { + this.minX = x; + this.minY = y; + this.maxX = x; + this.maxY = y; return this; } /** - * Set this rectangle to a rectangle of size zero at the given point. + * Set this rectangle to the given point with zero size. * * @param point the coordinate of both minimum and maximum corner * @return this */ - public Rectangled set(Vector2dc point) { + public Rectangled set(Vector2fc point) { return set(point.x(), point.y()); } - /** - * Set this rectangle to a rectangle of size zero at the given point. - * - * @param x the coordinate of both minimum and maximum x coordinate - * @param y the coordinate of both minimum and maximum y coordinate - * @return this - */ - public Rectangled set(double x, double y) { - return set(x, y, x, y); - } - /** * Set the minimum corner coordinates. * - * @param minX - * the x coordinate of the minimum corner - * @param minY - * the y coordinate of the minimum corner + * @param minX the x coordinate of the minimum corner + * @param minY the y coordinate of the minimum corner * @return this */ public Rectangled setMin(double minX, double minY) { @@ -257,8 +235,7 @@ public Rectangled setMin(double minX, double minY) { /** * Set the minimum corner coordinates. * - * @param min - * the minimum coordinates + * @param min the minimum coordinates * @return this */ public Rectangled setMin(Vector2dc min) { @@ -271,10 +248,8 @@ public Rectangled setMin(Vector2dc min) { /** * Set the maximum corner coordinates. * - * @param maxX - * the x coordinate of the maximum corner - * @param maxY - * the y coordinate of the maximum corner + * @param maxX the x coordinate of the maximum corner + * @param maxY the y coordinate of the maximum corner * @return this */ public Rectangled setMax(double maxX, double maxY) { @@ -286,8 +261,7 @@ public Rectangled setMax(double maxX, double maxY) { /** * Set the maximum corner coordinates. * - * @param max - * the maximum coordinates + * @param max the maximum coordinates * @return this */ public Rectangled setMax(Vector2dc max) { @@ -314,9 +288,13 @@ public double lengthY() { return maxY - minY; } - @Override + /** + * Return the area of the rectangle + * + * @return area + */ public double area() { - return getSizeX() * getSizeY(); + return lengthX() * lengthY(); } private Rectangled validate() { @@ -350,11 +328,10 @@ public Rectangled intersection(Rectangledc other, Rectangled dest) { /** * Compute the rectangle of intersection between this and the given rectangle. *

- * If the two rectangles do not intersect, then {@link Double#NaN} is stored in each component - * of dest. + * If the two rectangles do not intersect, then {@link Double#NaN} is stored in each component of + * dest. * - * @param other - * the other rectangle + * @param other the other rectangle * @return this */ public Rectangled intersection(Rectangledc other) { @@ -371,16 +348,14 @@ public Rectangled intersection(Rectanglefc other, Rectangled dest) { } /** - * Compute the rectangle of intersection between this and the given rectangle and - * store the result in dest. + * Compute the rectangle of intersection between this and the given rectangle and store the result in + * dest. *

- * If the two rectangles do not intersect, then {@link Double#NaN} is stored in each component - * of dest. + * If the two rectangles do not intersect, then {@link Double#NaN} is stored in each component of + * dest. * - * @param other - * the other rectangle - * @param dest - * will hold the result + * @param other the other rectangle + * @param dest will hold the result * @return dest */ @Override @@ -395,8 +370,7 @@ public Rectangled intersection(Rectangleic other, Rectangled dest) { /** * Return the length of this rectangle in the X and Y dimensions and store the result in dest. * - * @param dest - * will hold the result + * @param dest will hold the result * @return dest * @deprecated Use {@link #getSize(Vector2d)} */ @@ -407,49 +381,45 @@ public Vector2d lengths(Vector2d dest) { /** * Check if this rectangle contains the given rectangle. * - * @param rectangle - * the rectangle to test + * @param rectangle the rectangle to test * @return true iff this rectangle contains the rectangle; false otherwise */ @Override public boolean containsRectangle(Rectangledc rectangle) { return rectangle.minX() >= minX && rectangle.maxX() <= maxX && - rectangle.minY() >= minY && rectangle.maxY() <= maxY; + rectangle.minY() >= minY && rectangle.maxY() <= maxY; } /** * Check if this rectangle contains the given rectangle. * - * @param rectangle - * the rectangle to test + * @param rectangle the rectangle to test * @return true iff this rectangle contains the rectangle; false otherwise */ @Override public boolean containsRectangle(Rectanglefc rectangle) { return rectangle.minX() >= minX && rectangle.maxX() <= maxX && - rectangle.minY() >= minY && rectangle.maxY() <= maxY; + rectangle.minY() >= minY && rectangle.maxY() <= maxY; } + /** * Check if this rectangle contains the given rectangle. * - * @param rectangle - * the rectangle to test + * @param rectangle the rectangle to test * @return true iff this rectangle contains the rectangle; false otherwise */ @Override public boolean containsRectangle(Rectangleic rectangle) { return rectangle.minX() >= minX && rectangle.maxX() <= maxX && - rectangle.minY() >= minY && rectangle.maxY() <= maxY; + rectangle.minY() >= minY && rectangle.maxY() <= maxY; } /** * Set this to the union of this and the given point p. * - * @param x - * the x coordinate of the point - * @param y - * the y coordinate of the point + * @param x the x coordinate of the point + * @param y the y coordinate of the point * @return this */ public Rectangled union(double x, double y) { @@ -459,8 +429,7 @@ public Rectangled union(double x, double y) { /** * Set this to the union of this and the given point p. * - * @param p - * the point + * @param p the point * @return this */ public Rectangled union(Vector2dc p) { @@ -486,8 +455,7 @@ public Rectangled union(Vector2dc p, Rectangled dest) { /** * Set this to the union of this and other. * - * @param other - * the other {@link Rectanglef} + * @param other the other {@link Rectanglef} * @return this */ public Rectangled union(Rectangledc other) { @@ -497,10 +465,8 @@ public Rectangled union(Rectangledc other) { /** * Compute the union of this and other and store the result in dest. * - * @param other - * the other {@link Rectangled} - * @param dest - * will hold the result + * @param other the other {@link Rectangled} + * @param dest will hold the result * @return dest */ @Override @@ -515,40 +481,37 @@ public Rectangled union(Rectangledc other, Rectangled dest) { /** * Check if this and the given rectangle intersect. * - * @param other - * the other rectangle + * @param other the other rectangle * @return true iff both rectangles intersect; false otherwise */ @Override public boolean intersectsRectangle(Rectangledc other) { return minX < other.maxX() && maxX > other.minX() && - maxY > other.minY() && minY < other.maxY(); + maxY > other.minY() && minY < other.maxY(); } /** * Check if this and the given rectangle intersect. * - * @param other - * the other rectangle + * @param other the other rectangle * @return true iff both rectangles intersect; false otherwise */ @Override public boolean intersectsRectangle(Rectanglefc other) { return minX < other.maxX() && maxX > other.minX() && - maxY > other.minY() && minY < other.maxY(); + maxY > other.minY() && minY < other.maxY(); } /** * Check if this and the given rectangle intersect. * - * @param other - * the other rectangle + * @param other the other rectangle * @return true iff both rectangles intersect; false otherwise */ @Override public boolean intersectsRectangle(Rectanglei other) { return minX < other.maxX && maxX > other.minX && - maxY > other.minY && minY < other.maxY; + maxY > other.minY && minY < other.maxY; } @Override @@ -561,31 +524,10 @@ public boolean containsPoint(double x, double y) { return x > minX && y > minY && x < maxX && y < maxY; } - @Override - public boolean containsPoint(Vector2ic point) { - return containsPoint(point.x(), point.y()); - } - - @Override - public boolean containsPoint(int x, int y) { - return x > minX && y > minY && x < maxX && y < maxY; - } - - @Override - public boolean containsPoint(Vector2fc point) { - return containsPoint(point.x(), point.y()); - } - - @Override - public boolean containsPoint(float x, float y) { - return x > minX && y > minY && x < maxX && y < maxY; - } - /** * Translate this by the given vector xy. * - * @param xy - * the vector to translate by + * @param xy the vector to translate by * @return this */ public Rectangled translate(Vector2dc xy) { @@ -595,10 +537,8 @@ public Rectangled translate(Vector2dc xy) { /** * Translate this by the given vector xy and store the result in dest. * - * @param xy - * the vector to translate by - * @param dest - * will hold the result + * @param xy the vector to translate by + * @param dest will hold the result * @return dest */ @Override @@ -609,8 +549,7 @@ public Rectangled translate(Vector2dc xy, Rectangled dest) { /** * Translate this by the given vector xy. * - * @param xy - * the vector to translate by + * @param xy the vector to translate by * @return this */ public Rectangled translate(Vector2fc xy) { @@ -625,10 +564,8 @@ public Rectangled translate(Vector2fc xy, Rectangled dest) { /** * Translate this by the vector (x, y). * - * @param x - * the x coordinate to translate by - * @param y - * the y coordinate to translate by + * @param x the x coordinate to translate by + * @param y the y coordinate to translate by * @return this */ public Rectangled translate(double x, double y) { @@ -647,8 +584,7 @@ public Rectangled translate(double x, double y, Rectangled dest) { /** * Scale this about the origin. * - * @param sf - * the scaling factor in the x and y axis. + * @param sf the scaling factor in the x and y axis. * @return this */ public Rectangled scale(double sf) { @@ -665,12 +601,9 @@ public Rectangled scale(double sf, Rectangled dest) { *

* This is equivalent to translate(-ax, -ay).scale(sf).translate(ax, ay) * - * @param sf - * the scaling factor in the x and y axis - * @param ax - * the x coordinate of the anchor - * @param ay - * the y coordinate of the anchor + * @param sf the scaling factor in the x and y axis + * @param ax the x coordinate of the anchor + * @param ay the y coordinate of the anchor * @return this */ public Rectangled scale(double sf, double ax, double ay) { @@ -687,10 +620,8 @@ public Rectangled scale(double sf, double ax, double ay, Rectangled dest) { *

* This is equivalent to translate(anchor.negate()).scale(sf).translate(anchor.negate()) * - * @param sf - * the scaling factor in the x and y axis - * @param anchor - * the location of the anchor + * @param sf the scaling factor in the x and y axis + * @param anchor the location of the anchor * @return this */ public Rectangled scale(double sf, Vector2dc anchor) { @@ -705,10 +636,8 @@ public Rectangled scale(double sf, Vector2dc anchor, Rectangled dest) { /** * Scale this about the origin. * - * @param sx - * the scaling factor on the x axis - * @param sy - * the scaling factor on the y axis + * @param sx the scaling factor on the x axis + * @param sy the scaling factor on the y axis * @return this */ public Rectangled scale(double sx, double sy) { @@ -718,12 +647,9 @@ public Rectangled scale(double sx, double sy) { /** * Scale this about the origin and store the result in dest. * - * @param sx - * the scaling factor on the x axis - * @param sy - * the scaling factor on the y axis - * @param dest - * will hold the result + * @param sx the scaling factor on the x axis + * @param sy the scaling factor on the y axis + * @param dest will hold the result * @return dest */ @Override @@ -736,14 +662,10 @@ public Rectangled scale(double sx, double sy, Rectangled dest) { *

* This is equivalent to translate(-ax, -ay).scale(sx, sy).translate(ax, ay) * - * @param sx - * the scaling factor on the x axis - * @param sy - * the scaling factor on the y axis - * @param ax - * the x coordinate of the anchor - * @param ay - * the y coordinate of the anchor + * @param sx the scaling factor on the x axis + * @param sy the scaling factor on the y axis + * @param ax the x coordinate of the anchor + * @param ay the y coordinate of the anchor * @return this */ public Rectangled scale(double sx, double sy, double ax, double ay) { @@ -759,12 +681,9 @@ public Rectangled scale(double sx, double sy, double ax, double ay) { *

* This is equivalent to translate(anchor.negate()).scale(sx, sy).translate(anchor.negate()) * - * @param sx - * the scaling factor on the x axis - * @param sy - * the scaling factor on the y axis - * @param anchor - * the location of the anchor + * @param sx the scaling factor on the x axis + * @param sy the scaling factor on the y axis + * @param anchor the location of the anchor * @return this */ public Rectangled scale(double sx, double sy, Vector2dc anchor) { @@ -776,16 +695,11 @@ public Rectangled scale(double sx, double sy, Vector2dc anchor) { *

* This is equivalent to translate(-ax, -ay, dest).scale(sx, sy).translate(ax, ay) * - * @param sx - * the scaling factor on the x axis - * @param sy - * the scaling factor on the y axis - * @param ax - * the x coordinate of the anchor - * @param ay - * the y coordinate of the anchor - * @param dest - * will hold the result + * @param sx the scaling factor on the x axis + * @param sy the scaling factor on the y axis + * @param ax the x coordinate of the anchor + * @param ay the y coordinate of the anchor + * @param dest will hold the result * @return dest */ @Override @@ -802,14 +716,10 @@ public Rectangled scale(double sx, double sy, double ax, double ay, Rectangled d *

* This is equivalent to translate(anchor.negate(), dest).scale(sx, sy).translate(anchor.negate()) * - * @param sx - * the scaling factor on the x axis - * @param sy - * the scaling factor on the y axis - * @param anchor - * the location of the anchor - * @param dest - * will hold the result + * @param sx the scaling factor on the x axis + * @param sy the scaling factor on the y axis + * @param anchor the location of the anchor + * @param dest will hold the result * @return dest */ @Override @@ -854,7 +764,8 @@ public boolean equals(Object obj) { /** * Return a string representation of this rectangle. *

- * This method creates a new {@link DecimalFormat} on every invocation with the format string "0.000E0;-". + * This method creates a new {@link DecimalFormat} on every invocation with the format string + * "0.000E0;-". * * @return the string representation */ @@ -863,15 +774,15 @@ public String toString() { } /** - * Return a string representation of this rectangle by formatting the vector components with the given {@link NumberFormat}. + * Return a string representation of this rectangle by formatting the vector components with the given {@link + * NumberFormat}. * - * @param formatter - * the {@link NumberFormat} used to format the vector components with + * @param formatter the {@link NumberFormat} used to format the vector components with * @return the string representation */ public String toString(NumberFormat formatter) { return "(" + Runtime.format(minX, formatter) + " " + Runtime.format(minY, formatter) + ") < " - + "(" + Runtime.format(maxX, formatter) + " " + Runtime.format(maxY, formatter) + ")"; + + "(" + Runtime.format(maxX, formatter) + " " + Runtime.format(maxY, formatter) + ")"; } public void writeExternal(ObjectOutput out) throws IOException {