Skip to content

Commit

Permalink
short and byte versions of clamps methods & fixed int version of …
Browse files Browse the repository at this point in the history
…thoses
  • Loading branch information
Desoroxxx committed Aug 28, 2023
1 parent aa811fe commit e907032
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ You now have `read` and `write` methods for all Vectors from Red Core, it simply

- **NetworkUtil:** Designed to streamline network coding practices, this utility makes writing cleaner, safer, and more efficient networking code effortless.
- Added `read` and `write` methods to all vectors that allows for easy networking
- Added `int` versions of `clampTest`, `clampMinFirst` and `clampMaxFirst` in `MathUtil`
- Added `int`, `short` and `byte` versions of `clampTest`, `clampMinFirst` and `clampMaxFirst` in `MathUtil`
- Added `int` versions of the vectors
- Added a `Vec2f` constructor for the 2-dimensional vectors
- Added a `zero` method for the 2-dimensional vector
Expand Down
124 changes: 121 additions & 3 deletions src/main/java/io/redstudioragnarok/redcore/utils/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static double clampTest(final double input, final double min, final doubl
* @param max The maximum value to clamp to
* @return The clamped value
*/
public static double clampMinFirst(final int input, final int min, final int max) {
public static byte clampMinFirst(final byte input, final byte min, final byte max) {
return input < min ? min : input > max ? max : input;
}

Expand All @@ -160,7 +160,7 @@ public static double clampMinFirst(final int input, final int min, final int max
* @param max The maximum value to clamp to
* @return The clamped value
*/
public static double clampMaxFirst(final int input, final int min, final int max) {
public static byte clampMaxFirst(final byte input, final byte min, final byte max) {
return input > max ? max : input < min ? min : input;
}

Expand All @@ -178,7 +178,125 @@ public static double clampMaxFirst(final int input, final int min, final int max
* @param max The maximum value to clamp to
* @return The clamped value
*/
public static double clampTest(final int input, final int min, final int max) {
public static byte clampTest(final byte input, final byte min, final byte max) {
if (input < min) {
LOG.info("Clamped to minimum");
return min;
} else if (input > max) {
LOG.info("Clamped to maximum");
return max;
} else {
LOG.info("Did not clamp");
return input;
}
}

/**
* Clamps a value within a specified range [min, max], checking for the minimum value first.
* <p>
* If the input is less than min, it returns min. If the input is greater than max, it returns max.
* <p>
* Otherwise, it returns the input.
*
* @param input The input value to clamp
* @param min The minimum value to clamp to
* @param max The maximum value to clamp to
* @return The clamped value
*/
public static short clampMinFirst(final short input, final short min, final short max) {
return input < min ? min : input > max ? max : input;
}

/**
* Clamps a value within a specified range [min, max], checking for the maximum value first.
* <p>
* If the input is greater than max, it returns max. If the input is less than min, it returns min.
* <p>
* Otherwise, it returns the input.
*
* @param input The input value to clamp
* @param min The minimum value to clamp to
* @param max The maximum value to clamp to
* @return The clamped value
*/
public static short clampMaxFirst(final short input, final short min, final short max) {
return input > max ? max : input < min ? min : input;
}

/**
* Clamps a value within a specified range [min, max], checking for the maximum value first.
* <p>
* If the input is greater than max, it returns max. If the input is less than min, it returns min.
* <p>
* Otherwise, it returns the input.
* <p>
* This will log the result to check if clampMinFirst or clampMaxFirst should be used.
*
* @param input The input value to clamp
* @param min The minimum value to clamp to
* @param max The maximum value to clamp to
* @return The clamped value
*/
public static short clampTest(final short input, final short min, final short max) {
if (input < min) {
LOG.info("Clamped to minimum");
return min;
} else if (input > max) {
LOG.info("Clamped to maximum");
return max;
} else {
LOG.info("Did not clamp");
return input;
}
}

/**
* Clamps a value within a specified range [min, max], checking for the minimum value first.
* <p>
* If the input is less than min, it returns min. If the input is greater than max, it returns max.
* <p>
* Otherwise, it returns the input.
*
* @param input The input value to clamp
* @param min The minimum value to clamp to
* @param max The maximum value to clamp to
* @return The clamped value
*/
public static int clampMinFirst(final int input, final int min, final int max) {
return input < min ? min : input > max ? max : input;
}

/**
* Clamps a value within a specified range [min, max], checking for the maximum value first.
* <p>
* If the input is greater than max, it returns max. If the input is less than min, it returns min.
* <p>
* Otherwise, it returns the input.
*
* @param input The input value to clamp
* @param min The minimum value to clamp to
* @param max The maximum value to clamp to
* @return The clamped value
*/
public static int clampMaxFirst(final int input, final int min, final int max) {
return input > max ? max : input < min ? min : input;
}

/**
* Clamps a value within a specified range [min, max], checking for the maximum value first.
* <p>
* If the input is greater than max, it returns max. If the input is less than min, it returns min.
* <p>
* Otherwise, it returns the input.
* <p>
* This will log the result to check if clampMinFirst or clampMaxFirst should be used.
*
* @param input The input value to clamp
* @param min The minimum value to clamp to
* @param max The maximum value to clamp to
* @return The clamped value
*/
public static int clampTest(final int input, final int min, final int max) {
if (input < min) {
LOG.info("Clamped to minimum");
return min;
Expand Down

0 comments on commit e907032

Please sign in to comment.