113
src/Math.cppm
113
src/Math.cppm
@@ -1,51 +1,54 @@
|
||||
module;
|
||||
|
||||
import std;
|
||||
|
||||
export module LunarWM.Math;
|
||||
|
||||
export namespace LunarWM {
|
||||
namespace Math {
|
||||
import std;
|
||||
|
||||
export namespace LunarWM::Math {
|
||||
|
||||
template<typename T = float>
|
||||
requires std::is_arithmetic_v<T> struct Vec2 {
|
||||
template<typename U = T>
|
||||
requires std::is_arithmetic_v<U> Vec2<U> operator+(Vec2<U> const &other)
|
||||
requires std::is_arithmetic_v<U>
|
||||
auto operator+(Vec2<U> const &other) -> Vec2<U>
|
||||
{
|
||||
return { x + other.x, y + other.y };
|
||||
}
|
||||
template<typename U = T>
|
||||
requires std::is_arithmetic_v<U> Vec2<U> operator-(Vec2<U> const &other)
|
||||
requires std::is_arithmetic_v<U>
|
||||
auto operator-(Vec2<U> const &other) -> Vec2<U>
|
||||
{
|
||||
return { x - other.x, y - other.y };
|
||||
}
|
||||
template<typename U = T>
|
||||
requires std::is_arithmetic_v<U> Vec2<U> operator*(Vec2<U> const &other)
|
||||
requires std::is_arithmetic_v<U>
|
||||
auto operator*(Vec2<U> const &other) -> Vec2<U>
|
||||
{
|
||||
return { x * other.x, y * other.x };
|
||||
}
|
||||
template<typename U = T>
|
||||
requires std::is_arithmetic_v<U> Vec2<U> operator*(T scalar)
|
||||
requires std::is_arithmetic_v<U> auto operator*(T scalar) -> Vec2<U>
|
||||
{
|
||||
return { x * scalar, y * scalar };
|
||||
}
|
||||
template<typename U = T>
|
||||
requires std::is_arithmetic_v<U> Vec2<U> operator/(T scalar)
|
||||
requires std::is_arithmetic_v<U> auto operator/(T scalar) -> Vec2<U>
|
||||
{
|
||||
return { x / scalar, y / scalar };
|
||||
}
|
||||
template<typename U = T>
|
||||
requires std::is_arithmetic_v<U> Vec2 operator-()
|
||||
requires std::is_arithmetic_v<U> auto operator-() -> Vec2
|
||||
{
|
||||
return { -x, -y };
|
||||
}
|
||||
T length() const { return std::sqrt(x * x + y * y); }
|
||||
T lengthSquared() const { return x * x + y * y; }
|
||||
Vec2<T> normalized() const
|
||||
auto length() const -> T { return std::sqrt(x * x + y * y); }
|
||||
auto lengthSquared() const -> T { return x * x + y * y; }
|
||||
auto normalized() const -> Vec2<T>
|
||||
{
|
||||
T len = length();
|
||||
if (len == T(0))
|
||||
if (len == T(0)) {
|
||||
return { T(0), T(0) };
|
||||
}
|
||||
return *this / len;
|
||||
}
|
||||
|
||||
@@ -53,7 +56,8 @@ requires std::is_arithmetic_v<T> struct Vec2 {
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
requires std::is_arithmetic_v<T> Vec2<T> operator*(T scalar, Vec2<T> const &v)
|
||||
requires std::is_arithmetic_v<T>
|
||||
auto operator*(T scalar, Vec2<T> const &v) -> Vec2<T>
|
||||
{
|
||||
return { v.x * scalar, v.y * scalar };
|
||||
}
|
||||
@@ -71,23 +75,23 @@ requires std::is_arithmetic_v<T> struct Rect {
|
||||
{
|
||||
}
|
||||
|
||||
T &x() { return pos.x; }
|
||||
T &y() { return pos.y; }
|
||||
T &w() { return size.x; }
|
||||
T &h() { return size.y; }
|
||||
auto x() -> T & { return pos.x; }
|
||||
auto y() -> T & { return pos.y; }
|
||||
auto w() -> T & { return size.x; }
|
||||
auto h() -> T & { return size.y; }
|
||||
|
||||
T x() const { return pos.x; }
|
||||
T y() const { return pos.y; }
|
||||
T w() const { return size.x; }
|
||||
T h() const { return size.y; }
|
||||
auto x() const -> T { return pos.x; }
|
||||
auto y() const -> T { return pos.y; }
|
||||
auto w() const -> T { return size.x; }
|
||||
auto h() const -> T { return size.y; }
|
||||
|
||||
T left() const { return x(); }
|
||||
T right() const { return x() + w(); }
|
||||
T top() const { return y(); }
|
||||
T bottom() const { return y() + h(); }
|
||||
auto left() const -> T { return x(); }
|
||||
auto right() const -> T { return x() + w(); }
|
||||
auto top() const -> T { return y(); }
|
||||
auto bottom() const -> T { return y() + h(); }
|
||||
|
||||
T &left() { return x(); }
|
||||
T &top() { return y(); }
|
||||
auto left() -> T & { return x(); }
|
||||
auto top() -> T & { return y(); }
|
||||
|
||||
Vec2<T> pos, size;
|
||||
};
|
||||
@@ -95,57 +99,63 @@ requires std::is_arithmetic_v<T> struct Rect {
|
||||
template<typename T = float>
|
||||
requires std::is_arithmetic_v<T> struct Box {
|
||||
template<typename U = int>
|
||||
requires std::is_arithmetic_v<U> Vec2<T> &operator[](U const index)
|
||||
requires std::is_arithmetic_v<U> auto operator[](U const index) -> Vec2<T> &
|
||||
{
|
||||
if (index < 0 || index > 1)
|
||||
if (index < 0 || index > 1) {
|
||||
throw std::out_of_range("A box only has two points");
|
||||
}
|
||||
return m_data[index];
|
||||
}
|
||||
Vec2<T> &first() { return m_data[0]; }
|
||||
Vec2<T> &second() { return m_data[1]; }
|
||||
T &x0() { return m_data[0].x; }
|
||||
T &y0() { return m_data[0].y; }
|
||||
T &x1() { return m_data[1].x; }
|
||||
T &y1() { return m_data[1].y; }
|
||||
T &left()
|
||||
auto first() -> Vec2<T> & { return m_data[0]; }
|
||||
auto second() -> Vec2<T> & { return m_data[1]; }
|
||||
auto x0() -> T & { return m_data[0].x; }
|
||||
auto y0() -> T & { return m_data[0].y; }
|
||||
auto x1() -> T & { return m_data[1].x; }
|
||||
auto y1() -> T & { return m_data[1].y; }
|
||||
auto left() -> T &
|
||||
{
|
||||
if (x0() < x1())
|
||||
if (x0() < x1()) {
|
||||
return x0();
|
||||
}
|
||||
return x1();
|
||||
}
|
||||
T &right()
|
||||
auto right() -> T &
|
||||
{
|
||||
if (x0() > x1())
|
||||
if (x0() > x1()) {
|
||||
return x0();
|
||||
}
|
||||
return x1();
|
||||
}
|
||||
T &top()
|
||||
auto top() -> T &
|
||||
{
|
||||
if (y0() < y1())
|
||||
if (y0() < y1()) {
|
||||
return y0();
|
||||
}
|
||||
return y1();
|
||||
}
|
||||
T &bottom()
|
||||
auto bottom() -> T &
|
||||
{
|
||||
if (y0() > y1())
|
||||
if (y0() > y1()) {
|
||||
return y0();
|
||||
}
|
||||
return y1();
|
||||
}
|
||||
|
||||
Box() { }
|
||||
Box(Rect<T> rect)
|
||||
Box() = default;
|
||||
explicit Box(Rect<T> rect)
|
||||
{
|
||||
this->m_data[0] = rect.pos;
|
||||
this->m_data[1] = rect.pos + rect.size;
|
||||
}
|
||||
|
||||
private:
|
||||
Vec2<T> m_data[2] = {};
|
||||
std::array<Vec2<T>, 2> m_data = {};
|
||||
};
|
||||
|
||||
template<typename T = int>
|
||||
requires std::is_arithmetic_v<T> std::vector<Math::Rect<T>> subtract_rect(
|
||||
Math::Rect<T> const &src, Math::Rect<T> const &clip)
|
||||
requires std::is_arithmetic_v<T>
|
||||
auto subtract_rect(Math::Rect<T> const &src, Math::Rect<T> const &clip)
|
||||
-> std::vector<Math::Rect<T>>
|
||||
{
|
||||
std::vector<Math::Rect<T>> result;
|
||||
|
||||
@@ -208,5 +218,4 @@ requires std::is_arithmetic_v<T> struct Viewport {
|
||||
Vec2<T> depth_limits;
|
||||
};
|
||||
|
||||
} // namespace Math
|
||||
} // namespace LunarWM
|
||||
} // namespace LunarWM::Math
|
||||
|
||||
Reference in New Issue
Block a user