Skip to content

Instantly share code, notes, and snippets.

@ikr7
Last active March 20, 2021 06:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikr7/d31b0ead87c73e6378e6911e85661b93 to your computer and use it in GitHub Desktop.
Save ikr7/d31b0ead87c73e6378e6911e85661b93 to your computer and use it in GitHub Desktop.
Complex functions for GLSL (Note: not very optimized)
// absolute value of z (equivalent of built-in function `length`)
float cabs (vec2 z) {
return length(z);
}
// complex conjugate of z
vec2 cconj (vec2 z) {
return vec2(z.x, -z.y);
}
// a times b
vec2 cmul (vec2 a, vec2 b) {
return vec2(a.x * b.x - a.y * b.y, a.x * b.y + b.x * a.y);
}
// a divided by b
vec2 cdiv (vec2 a, vec2 b) {
float den = dot(b, b);
return vec2(
(a.x * b.x + a.y * b.y) / den,
(a.x * b.x - a.y * b.y) / den
);
}
// exponential of z
vec2 cexp (vec2 z) {
return exp(z.x) * vec2(cos(z.y), sin(z.y));
}
// principal value of logarithm of z
vec2 clog (vec2 z) {
return vec2(log(cabs(z)), atan(z.y, z.x));
}
// principal value of z to the a power
vec2 cpow (vec2 z, vec2 a) {
return cexp(cmul(a, clog(z)));
}
// cosine of z
vec2 ccos (vec2 z) {
return (cexp(vec2(-z.y, z.x)) + cexp(vec2(z.y, -z.x))) / 2.0;
}
// sine of z
vec2 csin (vec2 z) {
vec2 t = (cexp(vec2(-z.y, z.x)) - cexp(vec2(z.y, -z.x))) / 2.0;
return vec2(-t.y, -t.x);
}
// tangent of z
vec2 ctan (vec2 z) {
return cdiv(csin(z), ccos(z));
}
// hyperbolic cosine of z
vec2 ccosh (vec2 z) {
return (cexp(z) + cexp(-z)) / 2.0;
}
// hyperbolic sine of z
vec2 csinh (vec2 z) {
return (cexp(z) - cexp(-z)) / 2.0;
}
// hyperbolic tangent of z
vec2 ctanh (vec2 z) {
return cdiv(csinh(z), ccosh(z));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment