ToyGine2 26.2.0
Game Engine for retro consoles
Loading...
Searching...
No Matches
toy::math::Point Class Reference

2D integer point for UI and input coordinates. More...

Public Member Functions

constexpr Point () noexcept=default
 Default constructor.
constexpr Point (const int32_t &x, const int32_t &y) noexcept
 Constructs a point with the given coordinates.
constexpr Point (const int32_t *values) noexcept
 Constructs a point from an array.
constexpr int32_t * c_arr () noexcept
 Pointer to the coordinate array (x, then y).
constexpr const int32_t * c_arr () const noexcept
 Read-only pointer to the coordinate array (x, then y).
constexpr Pointoperator+= (const Point &point) noexcept
 Adds point to this point.
constexpr Pointoperator-= (const Point &point) noexcept
 Subtracts point from this point.
template<PointScalar T>
constexpr Pointoperator*= (const T &scalar) noexcept
 Multiplies both coordinates by scalar and truncates to integer.
template<PointScalar T>
constexpr Pointoperator/= (const T &scalar) noexcept
 Divides both coordinates by scalar and truncates to integer.
constexpr void setZero () noexcept
 Sets x and y to zero.
constexpr bool isZero () const noexcept
 Returns whether both coordinates are zero.
constexpr bool isEqual (const Point &point, int32_t epsilon=0) const noexcept
 Equality within epsilon.

Public Attributes

int32_t x
 X coordinate.
int32_t y
 Y coordinate.

Detailed Description

2D integer point for UI and input coordinates.

Value type with x and y for pixel positions, mouse coordinates, and layout. Prefer toy::math::Vector2 for floating-point or world coordinates.

Key Features

  • Constexpr support: Most operations are constexpr.
  • Exception safety: All operations are noexcept.
  • UI-oriented: Integer coordinates for pixel-perfect layout.
  • Type safety: Distinct type; coordinates are named x and y.

Usage Example

#include "math.hpp"
toy::math::Point mousePos(100, 200);
toy::math::Point windowPos(50, 75);
toy::math::Point buttonPos(10, 10);
buttonPos += Point(5, 5);
constexpr auto origin = toy::math::Point(0, 0);
constexpr auto center = toy::math::Point(640, 480);
2D integer point for UI and input coordinates.
Definition point.hpp:95
constexpr Point() noexcept=default
Default constructor.
Umbrella header for the math module.

Performance Characteristics

  • Construction, assignment, arithmetic, comparison: O(1).
  • Memory: 8 bytes; stack-friendly.

Safety Guarantees

  • Contracts: Division by zero and null values are asserted in debug.
  • Exception safety: All operations are noexcept.
Note
For approximate equality use isEqual() with a tolerance.
See also
toy::math::Vector2

Constructor & Destructor Documentation

◆ Point() [1/3]

toy::math::Point::Point ( )
constexprdefaultnoexcept

Default constructor.

◆ Point() [2/3]

toy::math::Point::Point ( const int32_t & x,
const int32_t & y )
constexprnoexcept

Constructs a point with the given coordinates.

Parameters
xX coordinate.
yY coordinate.

◆ Point() [3/3]

toy::math::Point::Point ( const int32_t * values)
explicitconstexprnoexcept

Constructs a point from an array.

Parameters
valuesPointer to at least two elements; values[0] is x, values[1] is y.
Precondition
values != nullptr; array has at least two elements.
Postcondition
x == values[0], y == values[1].

Member Function Documentation

◆ c_arr() [1/2]

const int32_t * toy::math::Point::c_arr ( ) const
nodiscardconstexprnoexcept

Read-only pointer to the coordinate array (x, then y).

Returns
Pointer to x; y follows contiguously.

◆ c_arr() [2/2]

int32_t * toy::math::Point::c_arr ( )
nodiscardconstexprnoexcept

Pointer to the coordinate array (x, then y).

Returns
Pointer to x; y follows contiguously.

◆ isEqual()

bool toy::math::Point::isEqual ( const Point & point,
int32_t epsilon = 0 ) const
nodiscardconstexprnoexcept

Equality within epsilon.

Parameters
pointOther point.
epsilonMaximum allowed difference per coordinate (default: 0).
Returns
true if |x − point.x| ≤ epsilon and |y − point.y| ≤ epsilon.
Precondition
epsilon0.

◆ isZero()

bool toy::math::Point::isZero ( ) const
nodiscardconstexprnoexcept

Returns whether both coordinates are zero.

Returns
true if x and y are zero, false otherwise.

◆ operator*=()

template<PointScalar T>
Point & toy::math::Point::operator*= ( const T & scalar)
constexprnoexcept

Multiplies both coordinates by scalar and truncates to integer.

Template Parameters
TType satisfying toy::math::PointScalar (signed integral, floating-point, or fixed-point).
Parameters
scalarScale factor.
Returns
Reference to *this.

◆ operator+=()

Point & toy::math::Point::operator+= ( const Point & point)
constexprnoexcept

Adds point to this point.

Parameters
pointPoint to add.
Returns
Reference to *this.
Postcondition
Coordinates are the sum of the previous values and point.

◆ operator-=()

Point & toy::math::Point::operator-= ( const Point & point)
constexprnoexcept

Subtracts point from this point.

Parameters
pointPoint to subtract.
Returns
Reference to *this.
Postcondition
Coordinates are the difference of the previous values and point.

◆ operator/=()

template<PointScalar T>
Point & toy::math::Point::operator/= ( const T & scalar)
constexprnoexcept

Divides both coordinates by scalar and truncates to integer.

Template Parameters
TType satisfying toy::math::PointScalar (signed integral, floating-point, or fixed-point).
Parameters
scalarDivisor.
Returns
Reference to *this.
Precondition
scalar != 0.

◆ setZero()

void toy::math::Point::setZero ( )
constexprnoexcept

Sets x and y to zero.

Postcondition
Point is at the origin.

Member Data Documentation

◆ x

int32_t toy::math::Point::x

X coordinate.

◆ y

int32_t toy::math::Point::y

Y coordinate.


The documentation for this class was generated from the following files: