Refactor/comment DhSectionPos, DhLodPos, and DhLodUnit
Also add BitShiftUtil to hold bit shift operation aliases for easier reading
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.seibel.lod.core.util;
|
||||
|
||||
/**
|
||||
* A list of helper methods to make code easier to read. <br>
|
||||
* Specifically written because bit shifts short circuit James' brain.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-11-6
|
||||
*/
|
||||
public class BitShiftUtil
|
||||
{
|
||||
/**
|
||||
* Equivalent to: <br>
|
||||
* 1 << value, <br>
|
||||
* 2^value, <br>
|
||||
* Math.pow(2, value) <br><br>
|
||||
*
|
||||
* Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same. <br><br>
|
||||
*
|
||||
* Can also be used to replace bit shifts in the format: <br>
|
||||
* multiplier << value; <br>
|
||||
* multiplier * powerOfTwo(value);
|
||||
*/
|
||||
public static int powerOfTwo(int value) { return 1 << value; }
|
||||
|
||||
/**
|
||||
* Equivalent to: <br>
|
||||
* value >> 1, <br>
|
||||
* value / 2 <br><br>
|
||||
*
|
||||
* Note: value / 2 isn't identical for negative values
|
||||
*/
|
||||
public static int half(int value) { return value >> 1; }
|
||||
|
||||
}
|
||||
@@ -6,43 +6,28 @@ public class MathUtil
|
||||
* Clamps the given value between the min and max values.
|
||||
* May behave strangely if min > max.
|
||||
*/
|
||||
public static int clamp(int min, int value, int max)
|
||||
{
|
||||
return Math.min(max, Math.max(value, min));
|
||||
}
|
||||
public static int clamp(int min, int value, int max) { return Math.min(max, Math.max(value, min)); }
|
||||
|
||||
/**
|
||||
* Clamps the given value between the min and max values.
|
||||
* May behave strangely if min > max.
|
||||
*/
|
||||
public static float clamp(float min, float value, float max)
|
||||
{
|
||||
return Math.min(max, Math.max(value, min));
|
||||
}
|
||||
public static float clamp(float min, float value, float max) { return Math.min(max, Math.max(value, min)); }
|
||||
|
||||
/**
|
||||
* Clamps the given value between the min and max values.
|
||||
* May behave strangely if min > max.
|
||||
*/
|
||||
public static double clamp(double min, double value, double max)
|
||||
{
|
||||
return Math.min(max, Math.max(value, min));
|
||||
}
|
||||
public static double clamp(double min, double value, double max) { return Math.min(max, Math.max(value, min)); }
|
||||
|
||||
/**
|
||||
* Like Math.floorDiv, but reverse in that it is a ceilDiv
|
||||
*/
|
||||
public static int ceilDiv(int value, int divider) {
|
||||
return -Math.floorDiv(-value, divider);
|
||||
}
|
||||
public static int ceilDiv(int value, int divider) { return -Math.floorDiv(-value, divider); }
|
||||
|
||||
// Why is this not in the standard library?! Come on Java!
|
||||
public static byte min(byte a, byte b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
public static byte max(byte a, byte b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
public static byte min(byte a, byte b) { return a < b ? a : b; }
|
||||
public static byte max(byte a, byte b) { return a > b ? a : b; }
|
||||
|
||||
|
||||
/** This is copied from Minecraft's MathHelper class */
|
||||
@@ -54,11 +39,10 @@ public class MathUtil
|
||||
numb = Float.intBitsToFloat(i);
|
||||
return numb * (1.5F - half * numb * numb);
|
||||
}
|
||||
public static float pow2(float x) {return x*x;}
|
||||
public static double pow2(double x) {return x*x;}
|
||||
public static int pow2(int x) {return x*x;}
|
||||
|
||||
public static long pow2(long x) {return x*x;}
|
||||
public static float pow2(float x) { return x * x; }
|
||||
public static double pow2(double x) { return x * x; }
|
||||
public static int pow2(int x) { return x * x; }
|
||||
|
||||
public static long pow2(long x) { return x * x; }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user