fault static method

void fault(
  1. Float32List g,
  2. TerrainOptions options
)

Generate random terrain using the Fault method.

Based on http://www.lighthouse3d.com/opengl/terrain/index.php3?fault Repeatedly draw random lines that cross the terrain. Raise the terrain on one side of the line and lower it on the other.

Parameters are the same as those for {@link static DiamondSquare}.

Implementation

static void fault(Float32List g, TerrainOptions options) {
  final d = math.sqrt(options.xSegments*options.xSegments + options.ySegments*options.ySegments),
      iterations = d * options.frequency,
      range = (options.maxHeight! - options.minHeight!) * 0.5,
      displacement = range / iterations,
      smoothDistance = math.min(options.xSize / options.xSegments, options.ySize / options.ySegments) * options.frequency;
  for (int k = 0; k < iterations; k++) {
    final v = math.Random().nextDouble(),
        a = math.sin(v * math.pi * 2),
        b = math.cos(v * math.pi * 2),
        c = math.Random().nextDouble() * d - d*0.5;
    for (int i = 0, xl = options.xSegments + 1; i < xl; i++) {
      for (int j = 0, yl = options.ySegments + 1; j < yl; j++) {
        final distance = a*i + b*j - c;
        if (distance > smoothDistance) {
          g[j * xl + i] += displacement;
        }
        else if (distance < -smoothDistance) {
          g[j * xl + i] -= displacement;
        }
        else {
          g[j * xl + i] += math.cos(distance / smoothDistance * math.pi * 2) * displacement;
        }
      }
    }
  }
  // static Smooth(g, options);
}