Smooth 2D Noise Viewer
Perlin and Value Noise
Helpers.cpp
Go to the documentation of this file.
1
4
5// MIT License
6//
7// Copyright (c) 2022 Ian Parberry
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy
10// of this software and associated documentation files (the "Software"), to
11// deal in the Software without restriction, including without limitation the
12// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13// sell copies of the Software, and to permit persons to whom the Software is
14// furnished to do so, subject to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included in
17// all copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25// IN THE SOFTWARE.
26
27#include <algorithm>
28#include <sstream>
29
30#include "Helpers.h"
31
35
36const float spline3(float t){
37 return t*t*(3.0f - 2.0f*t);
38} //spline3
39
44
45const float spline5(float t){
46 return t*t*t*(10.0f + 3.0f*t*(2.0f*t - 5.0f));
47} //spline5
48
55
56const float lerp(float t, float a, float b){
57 return a + t*(b - a);
58} //lerp
59
66
67const float clamp(float a, float x, float b){
68 return std::max(a, std::min(x, b));
69} //clamp
70
75
76std::wstring to_wstring_f(float x, size_t n){
77 std::wstringstream s; //convertor
78 s.precision(n); //set precision
79 s << std::fixed << x; //convert x
80 return s.str(); //return wstring
81} //to_wstring_f
82
93
94const bool isPowerOf2(size_t n){
95 return n != 0 && (n & (n - 1)) == 0;
96} //isPowerOf2
const float lerp(float t, float a, float b)
Linear interpolation.
Definition: Helpers.cpp:56
const float spline5(float t)
Quintic spline.
Definition: Helpers.cpp:45
const bool isPowerOf2(size_t n)
Power of 2 test.
Definition: Helpers.cpp:94
const float spline3(float t)
Cubic spline.
Definition: Helpers.cpp:36
std::wstring to_wstring_f(float x, size_t n)
Float to fixed precision wstring.
Definition: Helpers.cpp:76
const float clamp(float a, float x, float b)
Clamp between two values.
Definition: Helpers.cpp:67
Interface for helper functions.