mathr / blog / #

Recursive star polygons

Recursive Eight Star

I drew the image above last August. Yesterday I translated it into GLSL source code in Fragmentarium and generalized it to different numbers of points and points to skip when forming the star:

#extension GL_EXT_gpu_shader4 : enable

#include "Progressive2D.frag"

#group Star
uniform int Points; slider[5,8,16]
uniform int Stride; slider[2,3,7]

const int depth = 128;
const float pi = 3.141592653;

float a = pi / float(Points);
float R = 1.0;
float r = R * cos(a * float(Stride));
float f = r / cos(a);
mat2 rot = mat2(cos(a), sin(a), -sin(a), cos(a));

bool clockwise(vec2 a, vec2 b, vec2 c) {
  return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x) < 0.0;
}

vec3 color(vec2 p) {
  vec2 z = p.yx;
  bool inside = (Stride & 1) != 1;
  for (int i = 0; i < depth; ++i) {
    if (length(z) < f) {
      z /= f;
      if ((Stride & 1) != 1) {
        z *= rot;
        inside = !inside;
      }
    } else if (length(z) < R) {
      for (int j = 0; j < Points; ++j) {
        float a0 = 2.0 * float(j) * a;
        float a1 = 2.0 * float(j + Stride) * a;
        vec2 p0 = R * vec2(cos(a0), sin(a0));
        vec2 p1 = R * vec2(cos(a1), sin(a1));
        if (clockwise(p0, p1, z)) {
          inside = !inside;
        }
      }
      break;
    } else {
      inside = true;
      break;
    }
  }
  return vec3(inside ? 1.0 : 0.0);
}

They look like this:

Fragmentarium script for recursive star polygons.