[nostalgia/geo] Cleanup geo package

This commit is contained in:
Gary Talent 2022-02-21 20:02:27 -06:00
parent 9b1275e704
commit 9bd10bac78
5 changed files with 59 additions and 75 deletions

View File

@ -1,12 +1,11 @@
add_library( add_library(
NostalgiaGeo NostalgiaGeo
bounds.cpp vec.cpp
vec.cpp
) )
target_link_libraries( target_link_libraries(
NostalgiaGeo NostalgiaGeo PUBLIC
OxStd OxStd
) )

View File

@ -1,40 +0,0 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "bounds.hpp"
namespace nostalgia::geo {
Bounds::Bounds(int x, int y, int w, int h) noexcept {
this->x = x;
this->y = y;
this->width = w;
this->height = h;
}
bool Bounds::intersects(const Bounds &o) const noexcept {
return o.x2() >= x && x2() >= o.x && o.y2() >= y && y2() >= o.y;
}
bool Bounds::contains(int x, int y) const noexcept {
return x >= this->x && y >= this->y && x <= x2() && y <= y2();
}
int Bounds::x2() const noexcept {
return x + width;
}
int Bounds::y2() const noexcept {
return y + height;
}
Point Bounds::pt1() const noexcept {
return {x, y};
}
Point Bounds::pt2() const noexcept {
return {x2(), y2()};
}
}

View File

@ -4,6 +4,9 @@
#pragma once #pragma once
#include <ox/model/def.hpp>
#include <ox/std/error.hpp>
#include "point.hpp" #include "point.hpp"
namespace nostalgia::geo { namespace nostalgia::geo {
@ -20,36 +23,64 @@ class Bounds {
constexpr Bounds() noexcept = default; constexpr Bounds() noexcept = default;
Bounds(int x, int y, int w, int h) noexcept; constexpr Bounds(int x, int y, int w, int h) noexcept;
[[nodiscard]] [[nodiscard]]
bool intersects(const Bounds &other) const noexcept; constexpr bool intersects(const Bounds &other) const noexcept;
[[nodiscard]] [[nodiscard]]
bool contains(int x, int y) const noexcept; constexpr bool contains(int x, int y) const noexcept;
[[nodiscard]] [[nodiscard]]
int x2() const noexcept; constexpr int x2() const noexcept;
[[nodiscard]] [[nodiscard]]
int y2() const noexcept; constexpr int y2() const noexcept;
[[nodiscard]] [[nodiscard]]
Point pt1() const noexcept; constexpr Point pt1() const noexcept;
[[nodiscard]] [[nodiscard]]
Point pt2() const noexcept; constexpr Point pt2() const noexcept;
}; };
template<typename T> constexpr Bounds::Bounds(int x, int y, int w, int h) noexcept {
constexpr ox::Error model(T *io, Bounds *obj) noexcept { this->x = x;
io->template setTypeInfo<Point>(); this->y = y;
oxReturnError(io->field("x", &obj->x)); this->width = w;
oxReturnError(io->field("y", &obj->y)); this->height = h;
oxReturnError(io->field("width", &obj->width));
oxReturnError(io->field("height", &obj->height));
return OxError(0);
} }
constexpr bool Bounds::intersects(const Bounds &o) const noexcept {
return o.x2() >= x && x2() >= o.x && o.y2() >= y && y2() >= o.y;
}
constexpr bool Bounds::contains(int x, int y) const noexcept {
return x >= this->x && y >= this->y && x <= x2() && y <= y2();
}
constexpr int Bounds::x2() const noexcept {
return x + width;
}
constexpr int Bounds::y2() const noexcept {
return y + height;
}
constexpr Point Bounds::pt1() const noexcept {
return {x, y};
}
constexpr Point Bounds::pt2() const noexcept {
return {x2(), y2()};
}
oxModelBegin(Bounds)
oxModelField(x)
oxModelField(y)
oxModelField(width)
oxModelField(height)
oxModelEnd()
} }

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <ox/model/def.hpp>
#include <ox/std/error.hpp> #include <ox/std/error.hpp>
namespace nostalgia::geo { namespace nostalgia::geo {
@ -175,18 +176,13 @@ constexpr bool Point::operator==(const Point &p) const noexcept {
return x == p.x && y == p.y; return x == p.x && y == p.y;
} }
constexpr bool Point::operator!=(const Point &p) const noexcept { constexpr bool Point::operator!=(const Point &p) const noexcept {
return x != p.x || y != p.y; return x != p.x || y != p.y;
} }
oxModelBegin(Point)
template<typename T> oxModelField(x)
constexpr ox::Error model(T *io, Point *obj) noexcept { oxModelField(y)
io->template setTypeInfo<Point>(); oxModelEnd()
oxReturnError(io->field("x", &obj->x));
oxReturnError(io->field("y", &obj->y));
return OxError(0);
}
} }

View File

@ -145,6 +145,7 @@ constexpr Size Size::operator/(int i) const noexcept {
return out; return out;
} }
constexpr Size Size::operator+=(int i) noexcept { constexpr Size Size::operator+=(int i) noexcept {
width += i; width += i;
height += i; height += i;
@ -169,22 +170,19 @@ constexpr Size Size::operator/=(int i) noexcept {
return *this; return *this;
} }
constexpr bool Size::operator==(const Size &p) const noexcept { constexpr bool Size::operator==(const Size &p) const noexcept {
return width == p.width && height == p.height; return width == p.width && height == p.height;
} }
constexpr bool Size::operator!=(const Size &p) const noexcept { constexpr bool Size::operator!=(const Size &p) const noexcept {
return width != p.width || height != p.height; return width != p.width || height != p.height;
} }
template<typename T> oxModelBegin(Size)
constexpr ox::Error model(T *io, Size *obj) noexcept { oxModelField(width)
io->template setTypeInfo<Size>(); oxModelField(height)
oxReturnError(io->field("width", &obj->width)); oxModelEnd()
oxReturnError(io->field("height", &obj->height));
return OxError(0);
}
} }