gosper static method

dynamic gosper(
  1. dynamic size
)

Implementation

static gosper(size) {
  size = (size != null) ? size : 1;

  Function fractalize = (config) {
    var output;
    var input = config["axiom"];

    for (var i = 0, il = config["steps"];
        0 <= il ? i < il : i > il;
        0 <= il ? i++ : i--) {
      output = '';

      for (var j = 0, jl = input.length; j < jl; j++) {
        var char = input[j];

        if (config["rules"].keys.indexOf(char) >= 0) {
          output += config["rules"][char];
        } else {
          output += char;
        }
      }

      input = output;
    }

    return output;
  };

  Function toPoints = (Map<String, dynamic> config) {
    num currX = 0, currY = 0;
    num angle = 0;
    List<num> path = [0, 0, 0];
    var fractal = config["fractal"];

    for (var i = 0, l = fractal.length; i < l; i++) {
      var char = fractal[i];

      if (char == '+') {
        angle += config["angle"];
      } else if (char == '-') {
        angle -= config["angle"];
      } else if (char == 'F') {
        currX += config["size"] * Math.cos(angle);
        currY += -config["size"] * Math.sin(angle);
        path.addAll([currX, currY, 0]);
      }
    }

    return path;
  };

  //

  var gosper = fractalize({
    "axiom": 'A',
    "steps": 4,
    "rules": {"A": 'A+BF++BF-FA--FAFA-BF+', "B": '-FA+BFBF++BF+FA--FA-B'}
  });

  var points = toPoints({
    "fractal": gosper,
    "size": size,
    "angle": Math.PI / 3 // 60 degrees
  });

  return points;
}