Files
j3ml/include/J3ML/Geometry/Rect2D.hpp
josh e4dd7058e1
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
Implement Geometry::Rect2D class, which will work in tandem with 2D geometry tools such as Transform2D, AABB2D, OBB2D, Polygon2D
2025-04-18 15:31:22 -04:00

86 lines
3.0 KiB
C++

/// 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;
};
}