Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a convenient method to clamp the vector's components. #7369

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
7 changes: 7 additions & 0 deletions gdx/src/com/badlogic/gdx/math/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public interface Vector<T extends Vector<T>> {
* @return This vector for chaining */
T clamp (float min, float max);

/** Clamps the vector's components with the given boundaries.
*
* @param min The minimum value to clamp the vector's components
* @param max The maximum value to clamp the vector's components
* @return this vector for chaining */
T clampComponents (float min, float max);

/** Sets this vector from the given vector
* @param v The vector
* @return This vector for chaining */
Expand Down
18 changes: 18 additions & 0 deletions gdx/src/com/badlogic/gdx/math/Vector2.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ public Vector2 clamp (float min, float max) {
return this;
}

@Override
public Vector2 clampComponents (float min, float max) {
return clampComponents(min, max, min, max);
}

@Override
public Vector2 setLength (float len) {
return setLength2(len * len);
Expand Down Expand Up @@ -542,6 +547,19 @@ public boolean epsilonEquals (float x, float y) {
return epsilonEquals(x, y, MathUtils.FLOAT_ROUNDING_ERROR);
}

/** Clamps the vector's X and Y with the given boundaries.
*
* @param xMin The minimum value to clamp the vector's X-component
* @param xMax The maximum value to clamp the vector's X-component
* @param yMin The minimum value to clamp the vector's Y-component
* @param yMax The maximum value to clamp the vector's Y-component
* @return this vector for chaining */
public Vector2 clampComponents (float xMin, float xMax, float yMin, float yMax) {
x = MathUtils.clamp(x, xMin, xMax);
y = MathUtils.clamp(y, yMin, yMax);
return this;
}

@Override
public boolean isUnit () {
return isUnit(0.000000001f);
Expand Down
21 changes: 21 additions & 0 deletions gdx/src/com/badlogic/gdx/math/Vector3.java
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,11 @@ public Vector3 clamp (float min, float max) {
return this;
}

@Override
public Vector3 clampComponents (float min, float max) {
return clampComponents(min, max, min, max, min, max);
}

@Override
public int hashCode () {
final int prime = 31;
Expand Down Expand Up @@ -716,6 +721,22 @@ public boolean epsilonEquals (float x, float y, float z) {
return epsilonEquals(x, y, z, MathUtils.FLOAT_ROUNDING_ERROR);
}

/** Clamps the vector's X, Y and Z with the given boundaries.
*
* @param xMin The minimum value to clamp the vector's X-component
* @param xMax The maximum value to clamp the vector's X-component
* @param yMin The minimum value to clamp the vector's Y-component
* @param yMax The maximum value to clamp the vector's Y-component
* @param zMin The minimum value to clamp the vector's Z-component
* @param zMax The maximum value to clamp the vector's Z-component
* @return this vector for chaining */
public Vector3 clampComponents (float xMin, float xMax, float yMin, float yMax, float zMin, float zMax) {
x = MathUtils.clamp(x, xMin, xMax);
y = MathUtils.clamp(y, yMin, yMax);
z = MathUtils.clamp(z, zMin, zMax);
return this;
}

@Override
public Vector3 setZero () {
this.x = 0;
Expand Down
25 changes: 25 additions & 0 deletions gdx/src/com/badlogic/gdx/math/Vector4.java
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@ public Vector4 clamp (float min, float max) {
return this;
}

@Override
public Vector4 clampComponents (float min, float max) {
return clampComponents(min, max, min, max, min, max, min, max);
}

@Override
public int hashCode () {
final int prime = 31;
Expand Down Expand Up @@ -668,6 +673,26 @@ public boolean epsilonEquals (float x, float y, float z, float w) {
return epsilonEquals(x, y, z, w, MathUtils.FLOAT_ROUNDING_ERROR);
}

/** Clamps the vector's X, Y, Z and W with the given boundaries.
*
* @param xMin The minimum value to clamp the vector's X-component
* @param xMax The maximum value to clamp the vector's X-component
* @param yMin The minimum value to clamp the vector's Y-component
* @param yMax The maximum value to clamp the vector's Y-component
* @param zMin The minimum value to clamp the vector's Z-component
* @param zMax The maximum value to clamp the vector's Z-component
* @param wMin The minimum value to clamp the vector's W-component
* @param wMax The maximum value to clamp the vector's W-component
* @return this vector for chaining */
public Vector4 clampComponents (float xMin, float xMax, float yMin, float yMax, float zMin, float zMax, float wMin,
float wMax) {
x = MathUtils.clamp(x, xMin, xMax);
y = MathUtils.clamp(y, yMin, yMax);
z = MathUtils.clamp(z, zMin, zMax);
w = MathUtils.clamp(w, wMin, wMax);
return this;
}

@Override
public Vector4 setZero () {
this.x = 0;
Expand Down