Implement Geometry::Rect2D class, which will work in tandem with 2D geometry tools such as Transform2D, AABB2D, OBB2D, Polygon2D
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m31s
Build Docs With Doxygen / Explore-Gitea-Actions (push) Successful in 23s

This commit is contained in:
2025-04-18 15:31:22 -04:00
parent 3259a8e980
commit e4dd7058e1
5 changed files with 139 additions and 7 deletions

View File

@@ -7,8 +7,7 @@
/// @file AABB2D.hpp
/// @desc A 2D Axis-Aligned Bounding Box structure.
/// @edit 2024-08-01
/// @note On backlog, low-priority.
/// @edit 2025-04-18
#pragma once
@@ -23,8 +22,6 @@
template <typename Matrix>
void AABB2DTransformAsAABB2D(AABB2D& aabb, Matrix& m);
namespace J3ML::Geometry
{
using LinearAlgebra::Vector2;
@@ -46,7 +43,7 @@ namespace J3ML::Geometry
[[nodiscard]] float Width() const;
[[nodiscard]] float Height() const;
Vector2 Centroid();
Vector2 Centroid() const;
[[nodiscard]] float DistanceSq(const Vector2& pt) const;

View File

@@ -0,0 +1,86 @@
/// Josh's 3D Math Library
/// A C++20 Library for 3D Math, Computer Graphics, and Scientific Computing.
/// Developed and Maintained by Josh O'Leary @ Redacted Software.
/// Special Thanks to William Tomasine II and Maxine Hayes.
/// (c) 2024 Redacted Software
/// This work is dedicated to the public domain.
/// @file Rect2D.hpp
/// @desc A 2D AABB, represented by a top-left origin, and a w,h size.
/// @edit 2025-04-18
/// @note On backlog, low-priority.
#pragma once
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <J3ML/LinearAlgebra/Vector2i.hpp>
#include <J3ML/Geometry/Forward.hpp>
#include <J3ML/Geometry/AABB2D.hpp>
namespace J3ML::Geometry
{
/// A specialized type of 2D AABB structure, which represents a box from it's top-left point, and a size as width and height.
/// This is more natural for manipulation in 2D games, for bounding-boxes, sprite-quads, etc.
struct Rect2D {
/// The top-left origin point of this Rect2D.
Vector2 position;
/// The width and height of this Rect2D.
Vector2 size;
/// Constructs a Rect2D from a given Vector2 position, and size.
Rect2D(const Vector2& pos, const Vector2& size);
/// Constructs a Rect2D from a given position {x, y}, and size {w, h}
/// @param x The X-axis of the position.
/// @param y The Y-axis of the position.
/// @param w The width of the Rect2D.
/// @param h The height of the Rect2D.
Rect2D(float x, float y, float w, float h);
/// Constructs a Rect2D from a desired centroid, and a Vector2 specifying half-width, and half-height.
static Rect2D FromCentroidAndRadii(const Vector2& centroid, const Vector2& radii);
[[nodiscard]] float HorizontalRadius() const;
[[nodiscard]] float VerticalRadius() const;
[[nodiscard]] float HalfWidth() const;
[[nodiscard]] float HalfHeight() const;
[[nodiscard]] Vector2 Centroid() const;
[[nodiscard]] float Width() const;
[[nodiscard]] float Height() const;
[[nodiscard]] Vector2 MinPoint() const;
[[nodiscard]] Vector2 MaxPoint() const;
[[nodiscard]] AABB2D GetAsAABB() const;
float Area() const { return size.x * size.y;}
float Perimeter() const { return 2.f * (size.x + size.y); }
bool Intersects(const Rect2D& rhs) const;
bool Intersects(const AABB2D& rhs) const;
bool Contains(const Vector2& rhs) const;
bool Contains(int x, int y) const;
Vector2 PosInside(const Vector2 &normalizedPos) const;
Vector2 ToNormalizedLocalSpace(const Vector2 &pt) const;
Vector2 CornerPoint(int cornerIndex);
bool IsDegenerate() const;
bool HasNegativeVolume() const;
bool IsFinite() const;
Rect2D operator + (const Vector2& pt) const;
Rect2D& operator + (const Vector2& pt);
Rect2D operator - (const Vector2& pt) const;
};
struct Rect2Di {
Vector2i position;
Vector2i size;
};
}

View File

@@ -8,7 +8,6 @@ namespace J3ML::LinearAlgebra {
protected:
Matrix3x3 transformation;
public:
const static Transform2D Identity;
const static Transform2D FlipX;
const static Transform2D FlipY;

View File

@@ -78,7 +78,7 @@ namespace J3ML::Geometry
return a;
}
Vector2 AABB2D::Centroid() {
Vector2 AABB2D::Centroid() const {
return (minPoint + (maxPoint / 2.f));
}

View File

@@ -0,0 +1,50 @@
#include <J3ML/Geometry/Rect2D.hpp>
namespace J3ML::Geometry
{
Rect2D Rect2D::operator+(const J3ML::LinearAlgebra::Vector2 &pt) const {
return {position+pt, size};
}
Rect2D &Rect2D::operator+(const Vector2 &pt) {
position += pt;
return *this;
}
AABB2D Rect2D::GetAsAABB() const { return {MinPoint(), MaxPoint()};}
Rect2D Rect2D::FromCentroidAndRadii(const Vector2 &centroid, const Vector2 &radii) {
return Rect2D(centroid.x - (radii.x), centroid.y - (radii.y),
radii.x*2.f, radii.y*2.f);
}
Rect2D::Rect2D(float x, float y, float w, float h) {
this->position = {x,y};
this->size = {w,h};
}
Rect2D::Rect2D(const Vector2 &pos, const Vector2 &size) {
this->position = pos;
this->size = size;
}
float Rect2D::HorizontalRadius() const { return Width()/2.f;}
float Rect2D::VerticalRadius() const { return Height()/2.f;}
float Rect2D::HalfWidth() const { return HorizontalRadius();}
float Rect2D::HalfHeight() const { return VerticalRadius();}
Vector2 Rect2D::Centroid() const { return position + (size / 2.f);}
float Rect2D::Width() const { return size.x;}
float Rect2D::Height() const { return size.y;}
Vector2 Rect2D::MinPoint() const { return position; }
Vector2 Rect2D::MaxPoint() const { return position + size;}
}