| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "PerlinNoise.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <algorithm> | ||
| 5 | |||
| 6 | ✗ | float PerlinNoise::noise(float x, float y) { | |
| 7 | ✗ | int X = (int)floor(x) & 255; | |
| 8 | ✗ | int Y = (int)floor(y) & 255; | |
| 9 | x -= floor(x); | ||
| 10 | y -= floor(y); | ||
| 11 | ✗ | float u = fade(x); | |
| 12 | ✗ | float v = fade(y); | |
| 13 | ✗ | int A = _p[X] + Y; | |
| 14 | ✗ | int AA = _p[A]; | |
| 15 | ✗ | int AB = _p[A + 1]; | |
| 16 | ✗ | int B = _p[X + 1] + Y; | |
| 17 | ✗ | int BA = _p[B]; | |
| 18 | ✗ | int BB = _p[B + 1]; | |
| 19 | ✗ | return lerp(v, lerp(u, grad(_p[AA], x, y), | |
| 20 | ✗ | grad(_p[BA], x - 1, y)), | |
| 21 | ✗ | lerp(u, grad(_p[AB], x, y - 1), | |
| 22 | ✗ | grad(_p[BB], x - 1, y - 1))); | |
| 23 | } | ||
| 24 | |||
| 25 | ✗ | float PerlinNoise::fade(float t) { | |
| 26 | ✗ | return t * t * t * (t * (t * 6 - 15) + 10); | |
| 27 | } | ||
| 28 | |||
| 29 | ✗ | float PerlinNoise::lerp(float t, float a, float b) { | |
| 30 | ✗ | return a + t * (b - a); | |
| 31 | } | ||
| 32 | |||
| 33 | ✗ | float PerlinNoise::grad(int hash, float x, float y) { | |
| 34 | ✗ | int h = hash & 15; | |
| 35 | ✗ | float u = h < 8 ? x : y; | |
| 36 | ✗ | float v = h < 4 ? y : h == 12 || h == 14 ? x : 0; | |
| 37 | ✗ | return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); | |
| 38 | } | ||
| 39 | |||
| 40 | ✗ | void PerlinNoise::initPermutationVector(unsigned int seed) { | |
| 41 | ✗ | _p.clear(); | |
| 42 | ✗ | for (int i = 0; i < 256; ++i) _p.push_back(i); | |
| 43 | ✗ | std::shuffle(_p.begin(), _p.end(), std::default_random_engine(seed)); | |
| 44 | ✗ | for (int i = 0; i < 256; ++i) _p.push_back(_p[i]); | |
| 45 | ✗ | } | |
| 46 |