@@ -9,45 +9,42 @@ PerlinNoise::PerlinNoise(unsigned int seed) : m_seed(seed)
99 perm.insert (perm.end (), perm.begin (), perm.end ());
1010}
1111
12- PerlinNoise::~PerlinNoise ()
13- {
14- }
15-
16- float PerlinNoise::perlin (float x, float y)
12+ float PerlinNoise::perlin (float x, float y) const
1713{
1814 x *= 0 .00999999978f ;
1915 y *= 0 .00999999978f ;
2016
2117 int x0 = FastFloor (x);
2218 int y0 = FastFloor (y);
2319
24- float f_x0 = x - static_cast <float >(x0);
25- float f_y0 = y - static_cast <float >(y0);
26- float f_x1 = f_x0 - 1 .0f ;
27- float f_y1 = f_y0 - 1 .0f ;
20+ const float f_x0 = x - static_cast <float >(x0);
21+ const float f_y0 = y - static_cast <float >(y0);
22+ const float f_x1 = f_x0 - 1 .0f ;
23+ const float f_y1 = f_y0 - 1 .0f ;
2824
29- float xs = fade (f_x0);
30- float ys = fade (f_y0);
25+ const float xs = static_cast < float >( fade (f_x0) );
26+ const float ys = static_cast < float >( fade (f_y0) );
3127
3228 x0 *= 501125321 ;
3329 y0 *= 1136930381 ;
34- int x1 = x0 + 501125321 ;
35- int y1 = y0 + 1136930381 ;
30+ const int x1 = x0 + 501125321 ;
31+ const int y1 = y0 + 1136930381 ;
3632
37- float n0 = lerp (xs, dotgridgradient (x0, y0, f_x0, f_y0), dotgridgradient (x1, y0, f_x1, f_y0));
38- float n1 = lerp (xs, dotgridgradient (x0, y1, f_x0, f_y1), dotgridgradient (x1, y1, f_x1, f_y1));
33+ const double n0 = lerp (xs, dotgridgradient (x0, y0, f_x0, f_y0), dotgridgradient (x1, y0, f_x1, f_y0));
34+ const double n1 = lerp (xs, dotgridgradient (x0, y1, f_x0, f_y1), dotgridgradient (x1, y1, f_x1, f_y1));
3935
40- return lerp (ys, n0, n1) * 1 .4247691104677813f ;
36+ const auto r = lerp (ys, static_cast <float >(n0), static_cast <float >(n1));
37+ return static_cast <float >(r) * 1 .4247691104677813f ;
4138}
4239
43- void PerlinNoise::exportppm ()
40+ void PerlinNoise::exportppm () const
4441{
4542 std::ofstream file;
4643 file.open (" image.ppm" );
4744
4845 // Image
49- const int image_width = 500 ;
50- const int image_height = 500 ;
46+ constexpr int image_width = 500 ;
47+ constexpr int image_height = 500 ;
5148
5249 // Render
5350 file << " P3\n " << image_width << ' ' << image_height << " \n 255\n " ;
@@ -74,33 +71,33 @@ void PerlinNoise::exportppm()
7471 file.close ();
7572}
7673
77- float PerlinNoise::dotgridgradient (int ix, int iy, float x, float y)
74+ float PerlinNoise::dotgridgradient (int ix, int iy, float x, float y) const
7875{
7976 int hash = m_seed ^ ix ^ iy;
8077 hash *= 0x27d4eb2d ;
8178 hash ^= hash >> 15 ;
8279 hash &= 127 << 1 ;
8380
84- float xg = Lookup<float >::Gradients2D[hash];
85- float yg = Lookup<float >::Gradients2D[hash | 1 ];
81+ const float xg = Lookup<float >::Gradients2D[hash];
82+ const float yg = Lookup<float >::Gradients2D[hash | 1 ];
8683
8784 return x * xg + y * yg;
8885}
8986
90- double PerlinNoise::gradiant (int hash, double x, double y)
87+ double PerlinNoise::gradiant (int hash, double x, double y) const
9188{
9289 int h = hash & 7 ;
9390 double u = h < 4 ? x : y;
9491 double v = h < 4 ? y : x;
9592 return ((h & 1 ) ? -u : u) + ((h & 2 ) ? -2.0 * v : 2.0 * v);
9693}
9794
98- double PerlinNoise::lerp (float t, float a, float b)
95+ double PerlinNoise::lerp (float t, float a, float b) const
9996{
10097 return a + t * (b - a);
10198}
10299
103- double PerlinNoise::fade (double t)
100+ double PerlinNoise::fade (double t) const
104101{
105102 return t * t * t * (t * (t * 6.0 - 15.0 ) + 10.0 );
106103}
0 commit comments