Gaussian mountains

Inspired by maraz

Log in to post a comment.

// Forked from "Unknown Turtles" by maraz
// https://turtletoy.net/turtle/ca12448d34

Canvas.setpenopacity(-1);

let dens = 4 // min=1, max=10, step=1
dens=1/dens
const turtle = new Turtle();
const width = 90;
const zstart = 80;
const layer = dens*6;
const layers = 27/dens;
const perspective = 0.7*dens+0.2;

turtle.penup();
let h=0;
const leeway=1 // min=0, max=30, step=1
const amp=1 // min=0, max=20, step=0.1
const res =1 // min=1, max=10, step=1

const rows = 200
const scale = 150 // min=0, max=400, step=1
const mean = 0
const rad = 18 // min=0, max=50, step=1
const vermult = 9 // min=0, max=20, step=1
const cols = (layers+3)*(vermult)
const filledge = 1 //min=0, max=1, step=1
const gaussMat1 = generateGaussianNoiseMatrix(rows, cols, scale, mean)
const gaussMat= applyGaussianBlur(gaussMat1,rad)

function walk(i) {
    const left = -width + perspective * i;
    const right = width - perspective * i;
    const z = zstart - layer * i;
    
    if (filledge==1){
            
        turtle.penup();
        turtle.goto(-500, z);
        turtle.pendown();
        turtle.goto(left, z);
        turtle.penup();
        turtle.goto(500, z);
        turtle.pendown();
        turtle.goto(right, z);
        turtle.penup()
        
    }
    turtle.goto(left, z);

    for (let n = left; n < right; n += res) {
        
        
        
        const xIndex = Math.floor((n + width) / res);


        if (n < left + leeway || n > right - leeway) {
            h = z;
        } else {
            h = z + gaussMat[xIndex][i*vermult] * amp;
        }

        turtle.goto(n, h);
        turtle.pendown();
    }

    turtle.goto(right, z);
    turtle.penup();
    return i < layers;
}


function generateGaussianNoiseMatrix(rows, cols, scale, mean) {
  const noiseMatrix = [];

  for (let i = 0; i < rows; i++) {
    const row = [];
    for (let j = 0; j < cols; j++) {
      const u1 = 1 - Math.random();
      const u2 = 1 - Math.random();

      const z0 = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
      const z1 = Math.sqrt(-2 * Math.log(u1)) * Math.sin(2 * Math.PI * u2);

      const noiseValue = mean + scale * z0;
      row.push(noiseValue);
    }
    noiseMatrix.push(row);
  }

  return noiseMatrix;
}

// Function to apply Gaussian blur to a matrix
function applyGaussianBlur(matrix, radius) {
  const kernelSize = radius * 2 + 1;
  const kernel = [];

  for (let i = 0; i < kernelSize; i++) {
    const row = [];
    for (let j = 0; j < kernelSize; j++) {
      const x = i - radius;
      const y = j - radius;
      const weight = Math.exp(-(x * x + y * y) / (2 * radius * radius));
      row.push(weight);
    }
    kernel.push(row);
  }

  const blurredMatrix = [];

  for (let i = 0; i < matrix.length; i++) {
    const newRow = [];
    for (let j = 0; j < matrix[i].length; j++) {
      let sum = 0;
      let totalWeight = 0;
      for (let ki = 0; ki < kernelSize; ki++) {
        for (let kj = 0; kj < kernelSize; kj++) {
          const mi = i - radius + ki;
          const mj = j - radius + kj;
          if (mi >= 0 && mi < matrix.length && mj >= 0 && mj < matrix[i].length) {
            sum += matrix[mi][mj] * kernel[ki][kj];
            totalWeight += kernel[ki][kj];
          }
        }
      }
      newRow.push(sum / totalWeight);
    }
    blurredMatrix.push(newRow);
  }

  return blurredMatrix;
}