Skip to content

Instantly share code, notes, and snippets.

static double[] table = null;
static double step;
static double invStep;
static int size = 0;
static
{
size = 10000;
table = new double[size];
step = 2d * Math.PI / size;
public static final class Riven {
private static final int SIN_BITS, SIN_MASK, SIN_COUNT;
private static final float radFull, radToIndex;
private static final float degFull, degToIndex;
private static final float[] sin, cos;
static {
SIN_BITS = 12;
SIN_MASK = ~(-1 << SIN_BITS);
@micycle1
micycle1 / ASCII Character Ramp.txt
Last active July 30, 2018 14:40
ASCII Character Ramp
"Standard" character ramp for grey scale pictures, black -> white:
"$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. "
A more convincing but shorter sequence for representing 10 levels of grey is:
" .:-=+*#%@"
@micycle1
micycle1 / example.java
Created October 24, 2018 11:46
Guava ArrayTable from int[]
import com.google.common.collect.ArrayTable;
import com.google.common.collect.Table;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
int[] rows = new int[]{1,2,3}
int[] columns = new int[]{4,5,6,7}
@micycle1
micycle1 / Triangle Wave Function
Last active November 17, 2018 21:23
Triangle Wave Function
Function for triangle-wave y coordinate (oscillate between 0 and m).
y = m - abs(t % (2*m) - m)
Where:
y = output
m = max value (peak)
t = time
@micycle1
micycle1 / reflect.java
Created March 10, 2019 11:55
Reflecting Java instance methods with primitives
private static final Map<Class<?>, Class<?>> W2P; // wrapper to primitive
static {
W2P = new HashMap<>();
W2P.put(Boolean.class, boolean.class);
W2P.put(Byte.class, byte.class);
W2P.put(Character.class, char.class);
W2P.put(Double.class, double.class);
W2P.put(Float.class, float.class);
W2P.put(Integer.class, int.class);
public static long pow2(long a, long b) {
long re = 1;
while (b > 0) {
if ((b & 1) == 1) {
re *= a;
}
b >>= 1;
a *= a;
}
return re;
http://stereopsis.com/2div.html
Ben and 2 Divides
Two years ago, Ben Weiss (the guy who wrote Kai's Power Tools, Goo, Soap, etc.) was telling me about this idea he had to combine two divides. I just didn't pay attention.
Luckily, he told me again.
Ben's idea is really simple, and I use it everywhere now.
2 divides at once
Suppose you want to compute:
@micycle1
micycle1 / clonePGraphics.java
Created January 4, 2021 19:00
Code to clone PGraphics via reflection
static PGraphics cloneGraphics(PGraphics source) {
final String renderer;
switch (source.parent.sketchRenderer()) {
case "processing.opengl.PGraphics2D":
renderer = P2D;
break;
case "processing.opengl.PGraphics3D":
renderer = P3D;
break;
@micycle1
micycle1 / fastCubic
Created February 7, 2021 22:40
A fast solution to cubic equations with three real roots
http://momentsingraphics.de/CubicRoots.html#_Blinn06a
Returns the three real roots of the cubic polynomial (of form 1 + 2x + 3x^2 + 4x^3)
float3 SolveCubic(float4 Coefficient){
// Normalize the polynomial
Coefficient.xyz/=Coefficient.w;
// Divide middle coefficients by three
Coefficient.yz/=3.0f;
// Compute the Hessian and the discrimant