Hey, I found this in a realtime raytracing software thingie which I downloaded a long time ago. Could be interesting. Unfortunately there was no README or something in the package so I cannot give credit to the person who wrote this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| public class MoreMath { private static final int SIN_TABLE_SIZE_BITS = 12; private static final int SIN_TABLE_SIZE = 1 << SIN_TABLE_SIZE_BITS; private static final int SIN_TABLE_SIZE_MASK = SIN_TABLE_SIZE - 1; private static final int SIN_HALF_PI = SIN_TABLE_SIZE / 4; private static final float SIN_CONVERSION_FACTOR = (float)(SIN_TABLE_SIZE / (2*Math.PI)); private static float[] sinTable; private static final int ASIN_TABLE_SIZE = 2001; private static final float ASIN_CONVERSION_FACTOR = (float)((ASIN_TABLE_SIZE-1) / 2); private static float[] asinTable; private static final int SQRT_TABLE_SIZE = 1025; private static final float SQRT_CONVERSION_FACTOR = (float)((SQRT_TABLE_SIZE-1)); private static float[] sqrtTable; public static final float PI = (float)(Math.PI); public static final float HALF_PI = (float)(Math.PI / 2.0); public static final float TWO_PI = (float)(Math.PI * 2.0); public static final float PI_INV = (float)(1.0 / Math.PI); public static final float HALF_PI_INV = (float)(2.0 / Math.PI); public static final float TWO_PI_INV = (float)(1.0 / (Math.PI * 2.0)); private static final float ARGB_DECONVERT_SCALING = 1f / 255f; static { init(); } private static void init() { sinTable = new float[SIN_TABLE_SIZE]; for(int i=0; i<SIN_TABLE_SIZE; i++) { sinTable[i] = (float)Math.sin(i / SIN_CONVERSION_FACTOR); } asinTable = new float[ASIN_TABLE_SIZE]; for(int i=0; i<ASIN_TABLE_SIZE; i++) { asinTable[i] = (float)Math.asin((i / ASIN_CONVERSION_FACTOR) - 1); } sqrtTable = new float[SQRT_TABLE_SIZE]; for(int i=0; i<SQRT_TABLE_SIZE; i++) { sqrtTable[i] = (float)Math.sqrt(i / SQRT_CONVERSION_FACTOR); } } public static float sqrt(float x) { if(x>1) { return (float)Math.sqrt(x); } return sqrtTable[(int)(x*SQRT_CONVERSION_FACTOR)]; } public static float cos(float angle) { return sinTable[(SIN_HALF_PI-((int)(angle * SIN_CONVERSION_FACTOR))) & SIN_TABLE_SIZE_MASK]; } public static float sin(float angle) { return sinTable[((int)(angle * SIN_CONVERSION_FACTOR)) & SIN_TABLE_SIZE_MASK]; } public static float acos(float x) { if(x > 1) { x = 1; } else if(x < -1) { x = -1; } return (HALF_PI-asinTable[((int)((x+1) * ASIN_CONVERSION_FACTOR))]); } public static float asin(float x) { if(x > 1) { x = 1; } else if(x < -1) { x = -1; } return asinTable[((int)((x+1) * ASIN_CONVERSION_FACTOR))]; }
public static int sign(short v) { return (v>0)?1:(v<0)?-1:0; }
public static int sign(int v) { return (v>0)?1:(v<0)?-1:0; }
public static int sign(long v) { return (v>0)?1:(v<0)?-1:0; }
public static int sign(float v) { return (v>0)?1:(v<0)?-1:0; }
public static int sign(double v) { return (v>0)?1:(v<0)?-1:0; }
|
.. continued..