Skip to content

Instantly share code, notes, and snippets.

@n1ckfg
Forked from DonKarlssonSan/complex.glsl
Created September 22, 2024 18:56
Show Gist options
  • Save n1ckfg/847bcbcd75be28b6dccb8032c277feaa to your computer and use it in GitHub Desktop.
Save n1ckfg/847bcbcd75be28b6dccb8032c277feaa to your computer and use it in GitHub Desktop.
Complex Number Math In GLSL
// Hyperboloc functions by toneburst from
// https://machinesdontcare.wordpress.com/2008/03/10/glsl-cosh-sinh-tanh/
// These are missing in GLSL 1.10 and 1.20, uncomment if you need them
/*
/// COSH Function (Hyperbolic Cosine)
float cosh(float val)
{
float tmp = exp(val);
float cosH = (tmp + 1.0 / tmp) / 2.0;
return cosH;
}
// TANH Function (Hyperbolic Tangent)
float tanh(float val)
{
float tmp = exp(val);
float tanH = (tmp - 1.0 / tmp) / (tmp + 1.0 / tmp);
return tanH;
}
// SINH Function (Hyperbolic Sine)
float sinh(float val)
{
float tmp = exp(val);
float sinH = (tmp - 1.0 / tmp) / 2.0;
return sinH;
}
*/
// Complex Number math by julesb
// https://github.com/julesb/glsl-util
// Additions by Johan Karlsson (DonKarlssonSan)
#define cx_mul(a, b) vec2(a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x)
#define cx_div(a, b) vec2(((a.x*b.x+a.y*b.y)/(b.x*b.x+b.y*b.y)),((a.y*b.x-a.x*b.y)/(b.x*b.x+b.y*b.y)))
#define cx_modulus(a) length(a)
#define cx_conj(a) vec2(a.x, -a.y)
#define cx_arg(a) atan(a.y, a.x)
#define cx_sin(a) vec2(sin(a.x) * cosh(a.y), cos(a.x) * sinh(a.y))
#define cx_cos(a) vec2(cos(a.x) * cosh(a.y), -sin(a.x) * sinh(a.y))
vec2 cx_sqrt(vec2 a) {
float r = length(a);
float rpart = sqrt(0.5*(r+a.x));
float ipart = sqrt(0.5*(r-a.x));
if (a.y < 0.0) ipart = -ipart;
return vec2(rpart,ipart);
}
vec2 cx_tan(vec2 a) {return cx_div(cx_sin(a), cx_cos(a)); }
vec2 cx_log(vec2 a) {
float rpart = sqrt((a.x*a.x)+(a.y*a.y));
float ipart = atan(a.y,a.x);
if (ipart > PI) ipart=ipart-(2.0*PI);
return vec2(log(rpart),ipart);
}
vec2 cx_mobius(vec2 a) {
vec2 c1 = a - vec2(1.0,0.0);
vec2 c2 = a + vec2(1.0,0.0);
return cx_div(c1, c2);
}
vec2 cx_z_plus_one_over_z(vec2 a) {
return a + cx_div(vec2(1.0,0.0), a);
}
vec2 cx_z_squared_plus_c(vec2 z, vec2 c) {
return cx_mul(z, z) + c;
}
vec2 cx_sin_of_one_over_z(vec2 z) {
return cx_sin(cx_div(vec2(1.0,0.0), z));
}
////////////////////////////////////////////////////////////
// end Complex Number math by julesb
////////////////////////////////////////////////////////////
// My own additions to complex number math
#define cx_sub(a, b) vec2(a.x - b.x, a.y - b.y)
#define cx_add(a, b) vec2(a.x + b.x, a.y + b.y)
#define cx_abs(a) length(a)
vec2 cx_to_polar(vec2 a) {
float phi = atan(a.y / a.x);
float r = length(a);
return vec2(r, phi);
}
// Complex power
// Let z = r(cos θ + i sin θ)
// Then z^n = r^n (cos nθ + i sin nθ)
vec2 cx_pow(vec2 a, float n) {
float angle = atan(a.y, a.x);
float r = length(a);
float real = pow(r, n) * cos(n*angle);
float im = pow(r, n) * sin(n*angle);
return vec2(real, im);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment