math_util.h
1 // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project 2 // Licensed under GPLv2 or any later version 3 // Refer to the license.txt file included. 4 5 #pragma once 6 7 #include <cstdlib> 8 #include <type_traits> 9 10 namespace Common { 11 12 constexpr float PI = 3.14159265f; 13 14 template <class T> 15 struct Rectangle { 16 T left{}; 17 T top{}; 18 T right{}; 19 T bottom{}; 20 21 constexpr Rectangle() = default; 22 23 constexpr Rectangle(T left, T top, T right, T bottom) 24 : left(left), top(top), right(right), bottom(bottom) {} 25 26 [[nodiscard]] constexpr bool operator==(const Rectangle<T>& rhs) const { 27 return (left == rhs.left) && (top == rhs.top) && (right == rhs.right) && 28 (bottom == rhs.bottom); 29 } 30 31 [[nodiscard]] constexpr bool operator!=(const Rectangle<T>& rhs) const { 32 return !operator==(rhs); 33 } 34 35 [[nodiscard]] constexpr Rectangle<T> operator*(const T value) const { 36 return Rectangle{left * value, top * value, right * value, bottom * value}; 37 } 38 [[nodiscard]] constexpr Rectangle<T> operator/(const T value) const { 39 return Rectangle{left / value, top / value, right / value, bottom / value}; 40 } 41 42 [[nodiscard]] T GetWidth() const { 43 return std::abs(static_cast<std::make_signed_t<T>>(right - left)); 44 } 45 [[nodiscard]] T GetHeight() const { 46 return std::abs(static_cast<std::make_signed_t<T>>(bottom - top)); 47 } 48 [[nodiscard]] Rectangle<T> TranslateX(const T x) const { 49 return Rectangle{left + x, top, right + x, bottom}; 50 } 51 [[nodiscard]] Rectangle<T> TranslateY(const T y) const { 52 return Rectangle{left, top + y, right, bottom + y}; 53 } 54 [[nodiscard]] Rectangle<T> Scale(const float s) const { 55 return Rectangle{left, top, static_cast<T>(left + GetWidth() * s), 56 static_cast<T>(top + GetHeight() * s)}; 57 } 58 }; 59 60 template <typename T> 61 Rectangle(T, T, T, T) -> Rectangle<T>; 62 63 } // namespace Common