Skip to content

Instantly share code, notes, and snippets.

@cyberpwnn
Created January 22, 2019 17:23
Show Gist options
  • Save cyberpwnn/e2b0fdb19a9cff934440ff5b15509644 to your computer and use it in GitHub Desktop.
Save cyberpwnn/e2b0fdb19a9cff934440ff5b15509644 to your computer and use it in GitHub Desktop.
package com.volmit.wormholes.geometry;
public class CachedMath
{
private static final int precision = 128;
private static final int modulus = 360 * precision;
private static final float[] sin = new float[modulus];
/**
* Fast sin function
*
* @param a
* the number
* @return the sin
*/
public static float sin(float a)
{
return sinLookup((int) (a * precision + 0.5f));
}
/**
* Fast cos function
*
* @param a
* the number
* @return the cos
*/
public static float cos(float a)
{
return sinLookup((int) ((a + 90f) * precision + 0.5f));
}
private static float sinLookup(int a)
{
return a >= 0 ? sin[a % (modulus)] : -sin[-a % (modulus)];
}
static
{
for(int i = 0; i < sin.length; i++)
{
sin[i] = (float) Math.sin((i * Math.PI) / (precision * 180));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment