The Koch snowflake is a famous fractal.

The Koch Snowflake fractal

So is the Cantor set.

The Cantor set

Less famous, maybe, is Cantor dust, a version of the Cantor set made with squares instead of lines, which apparently earned it a much cooler name.

But as far as I know, we have no Cantor snowflake.

Since it’s Christmas, and since, in the odd quiet moments between holiday noise, Daniel Shiffman’s Nature of Code has been keeping me company, I wondered if we could make a Cantor snowflake.

Here’s what I came up with.

cantor-snowflake

As a bonus, it contains the Koch snowflake inside of it! I didn’t expect that.

I also rendered a Cantor snowflake PDF, which has a couple extra generations. It could make a nice bookmark.

Here’s the sourcecode, which is also running on openprocessing:

 1 void setup() {
 2   size(1450, 300);
 3 
 4   background(255);
 5   noStroke();
 6   fill(0);
 7 
 8   cantorSnowflake(0, height/2, 140, 280);
 9 }
10 
11 void cantorSnowflake(float x, float y, float length, float sideStep) {
12   if (length < 0.1) return;
13 
14   pushMatrix();
15 
16   hexagon(x, y, length);
17 
18   translate(sideStep, 0);
19 
20   for (int i = 0; i < 6; i++) {
21     PVector point = vector(i * THIRD_PI, length * 2 / 3);
22     cantorSnowflake(point.x, point.y, length / 3, sideStep);
23   }
24 
25   popMatrix();
26 }
27 
28 void hexagon(float centerX, float centerY, float length) {
29   translate(centerX, centerY);
30 
31   beginShape();
32   for (int i = 0; i < 6; i++) {
33     hexPoint(vector(i * THIRD_PI, length));
34   }
35   endShape(CLOSE);
36 }
37 
38 void hexPoint(PVector v) {
39   vertex(v.x, v.y);
40 }
41 
42 PVector vector(float rads, float length) {
43   return new PVector(cos(rads) * length, sin(rads) * length);
44 }

Happy Christmas!