/* This file was generated by scramble.py. */
#line 1 "src/perlin.py"
#include "perlin.h"
#line 2
#include "rgba.h"
#line 10
static float cosine_lerp(float a0, float a1, float w) {
    float ft = w * LAND_PI;
    float f = (1 - cos(ft)) * 0.5;
    return a0 * (1 - f) + a1 * f;
}
Perlin * perlin_create(int w, int h) {
    Perlin * self;
#line 16
    land_alloc(self);
    self->w = w;
    self->h = h;
    self->x = land_calloc(w * h * sizeof (* self->x));
    self->y = land_calloc(w * h * sizeof (* self->y));
    for (int j = 0; j < h; j += 1) {
        for (int i = 0; i < w; i += 1) {
            float a = land_rnd(0, LAND_PI * 2);
            self->x [i + j * w] = cos(a);
            self->y [i + j * w] = sin(a);
        }
    }
#line 26
    return self;
}
void perlin_destroy(Perlin * self) {
    land_free(self->x);
    land_free(self->y);
    land_free(self);
}
static float gradient_x(Perlin * self, int x, int y) {
    x %= self->w;
    if (x < 0) {
#line 35
        x += self->w;
    }
#line 36
    y %= self->h;
    if (y < 0) {
#line 37
        y += self->h;
    }
#line 38
    return self->x [x + y * self->w];
}
static float gradient_y(Perlin * self, int x, int y) {
    x %= self->w;
    if (x < 0) {
#line 42
        x += self->w;
    }
#line 43
    y %= self->h;
    if (y < 0) {
#line 44
        y += self->h;
    }
#line 45
    return self->y [x + y * self->w];
}
static float dot(Perlin * self, int ix, int iy, float x, float y) {
    float dx = x - ix;
    float dy = y - iy;
#line 51
    return dx * gradient_x(self, ix, iy) + dy * gradient_y(self, ix, iy);
}
float perlin_at(Perlin * self, float x, float y) {
    int x0 = floor(x);
    int x1 = x0 + 1;
    int y0 = floor(y);
    int y1 = y0 + 1;
    float sx = x - x0;
    float sy = y - y0;
#line 61
    float n0, n1, ix0, ix1, value;
    n0 = dot(self, x0, y0, x, y);
    n1 = dot(self, x1, y0, x, y);
    ix0 = cosine_lerp(n0, n1, sx);
    n0 = dot(self, x0, y1, x, y);
    n1 = dot(self, x1, y1, x, y);
    ix1 = cosine_lerp(n0, n1, sx);
    value = cosine_lerp(ix0, ix1, sy);
#line 70
    return value;
}
void perlin_displace(Perlin * self, float x, float y, float * xd, float * yd) {
    int x0 = floor(x);
    int x1 = x0 + 1;
    int y0 = floor(y);
    int y1 = y0 + 1;
    float sx = x - x0;
    float sy = y - y0;
#line 80
    float n0, n1, ix0, ix1;
    n0 = gradient_x(self, x0, y0);
    n1 = gradient_x(self, x1, y0);
    ix0 = cosine_lerp(n0, n1, sx);
    n0 = gradient_x(self, x0, y1);
    n1 = gradient_x(self, x1, y1);
    ix1 = cosine_lerp(n0, n1, sx);
    * xd = cosine_lerp(ix0, ix1, sy);
#line 89
    n0 = gradient_y(self, x0, y0);
    n1 = gradient_y(self, x1, y0);
    ix0 = cosine_lerp(n0, n1, sx);
    n0 = gradient_y(self, x0, y1);
    n1 = gradient_y(self, x1, y1);
    ix1 = cosine_lerp(n0, n1, sx);
    * yd = cosine_lerp(ix0, ix1, sy);
}
/* This file was generated by scramble.py. */
