| 1 | // Aseprite |
| 2 | // Copyright (C) 2001-2016 David Capello |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifndef APP_WRAP_VALUE_H_INCLUDED |
| 8 | #define APP_WRAP_VALUE_H_INCLUDED |
| 9 | #pragma once |
| 10 | |
| 11 | #include <cmath> |
| 12 | |
| 13 | namespace app { |
| 14 | |
| 15 | template<typename T> |
| 16 | inline T wrap_value(const T x, const T size) { |
| 17 | if (x < T(0)) |
| 18 | return size - (-(x+1) % size) - 1; |
| 19 | else |
| 20 | return x % size; |
| 21 | } |
| 22 | |
| 23 | template<> |
| 24 | inline double wrap_value(const double x, const double size) { |
| 25 | if (x < 0.0) |
| 26 | return size - std::fmod(-(x+1.0), size) - 1.0; |
| 27 | else |
| 28 | return std::fmod(x, size); |
| 29 | } |
| 30 | |
| 31 | } // namespace app |
| 32 | |
| 33 | #endif |
| 34 | |