Mathematics

template<typename TType>
struct IVector2

Represents a 2D vector with templated type.

Provides various operations like addition, subtraction, multiplication, and division, as well as utility methods for vector normalization, dot product, and cross product.

Template Parameters:

TType – The type of the vector’s components (e.g., int, float, double).

Public Functions

inline IVector2(const TType &x, const TType &y)

Vector2 constructor.

Parameters:
  • x – TType value representing x

  • y – TType value representing y

inline IVector2 operator+(const IVector2 &other) const

Adds this vector to another vector.

Parameters:

other – The other vector to add.

Returns:

A new vector representing the result of the addition.

inline IVector2 operator-(const IVector2 &other) const

Subtracts another vector from this vector.

Parameters:

other – The other vector to subtract.

Returns:

A new vector representing the result of the subtraction.

inline IVector2 operator*(const TType &scalar) const

Multiplies this vector by a scalar value.

Parameters:

scalar – The scalar to multiply by.

Returns:

A new vector representing the result of the multiplication.

inline IVector2 operator*(const IVector2 &other) const

Multiplies this vector by another vector.

Parameters:

other – The other vector to multiply.

Returns:

A new vector representing the result of the multiplication.

inline IVector2 operator/(const TType &scalar) const

Divides this vector by a scalar value.

Parameters:

scalar – The scalar to divide by.

Returns:

A new vector representing the result of the division.

inline IVector2 operator/(const IVector2 &other) const

Divides this vector by another vector.

Parameters:

scalar – The scalar to divide by.

Returns:

A new vector representing the result of the division.

inline bool operator==(const IVector2 &other) const

Compares this vector with another vector for equality.

Parameters:

other – The other vector to compare with.

Returns:

True if the vectors are equal, false otherwise.

inline bool operator!=(const IVector2 &other) const

Compares this vector with another vector for inequality.

Parameters:

other – The other vector to compare with.

Returns:

True if the vectors are not equal, false otherwise.

inline float length() const

Computes the length (norm) of the vector.

Returns:

The length of the vector as a float.

inline IVector2<float> normalize() const

Normalizes the vector (makes its length equal to 1).

Returns:

A new vector representing the normalized vector.

inline float dot(const IVector2 &other) const

Computes the dot product of this vector with another vector.

Parameters:

other – The other vector to compute the dot product with.

Returns:

The dot product as a float.

inline IVector2 cross() const

Computes the cross product of this vector itself.

Note

Two parallel vectors will always return zero cross product.

Returns:

Zero

inline float cross(const IVector2 &other) const

Computes the cross product of this vector with another vector.

Parameters:

other – The other vector to compute the cross product with.

Returns:

The cross product as a float.

Public Members

const TType x

First coordinate of the vector.

const TType y

Second coordinate of the vector.

template<typename TType>
struct IVector3

Represents a 3D vector with templated type.

Provides various operations like addition, subtraction, multiplication, and division, as well as utility methods for vector normalization, dot product, and cross product.

Template Parameters:

TType – The type of the vector’s components (e.g., int, float, double).

Public Functions

inline IVector3(const TType &x, const TType &y, const TType &z)

Vector2 constructor.

Parameters:
  • x – TType value representing x

  • y – TType value representing y

  • z – TType value representing z

inline IVector3 operator+(const IVector3 &other) const

Adds this vector to another vector.

Parameters:

other – The other vector to add.

Returns:

A new vector representing the result of the addition.

inline IVector3 operator-(const IVector3 &other) const

Subtracts another vector from this vector.

Parameters:

other – The other vector to subtract.

Returns:

A new vector representing the result of the subtraction.

inline IVector3 operator*(const TType &scalar) const

Multiplies this vector by a scalar value.

Parameters:

scalar – The scalar to multiply by.

Returns:

A new vector representing the result of the multiplication.

inline IVector3 operator*(const IVector3 &other) const

Multiplies this vector by another vector.

Parameters:

other – The vector to multiply by.

Returns:

A new vector representing the result of the multiplication.

inline IVector3 operator/(const TType &scalar) const

Divides this vector by a scalar value.

Parameters:

scalar – The scalar to divide by.

Returns:

A new vector representing the result of the division.

inline IVector3 operator/(const IVector3 &other) const

Divides this vector by anoter vector.

Parameters:

other – The vector to divide by.

Returns:

A new vector representing the result of the division.

inline bool operator==(const IVector3 &other) const

Compares this vector with another vector for equality.

Parameters:

other – The other vector to compare with.

Returns:

True if the vectors are equal, false otherwise.

inline bool operator!=(const IVector3 &other) const

Compares this vector with another vector for inequality.

Parameters:

other – The other vector to compare with.

Returns:

True if the vectors are not equal, false otherwise.

inline float length() const

Computes the length (norm) of the vector.

Returns:

The length of the vector as a float.

inline IVector3<float> normalize() const

Normalizes the vector (makes its length equal to 1).

Returns:

A new vector representing the normalized vector.

inline float dot(const IVector3 &other) const

Computes the dot product of this vector with another vector.

Parameters:

other – The other vector to compute the dot product with.

Returns:

The dot product as a float.

inline IVector3 cross() const

Computes the cross product of this vector with itself.

Note

Two parallel vectors will always return zero cross product.

Returns:

A new vector representing the cross product.

inline IVector3 cross(const IVector3 &other) const

Computes the cross product of this vector with another vector.

Parameters:

other – The other vector to compute the cross product with.

Returns:

A new vector representing the cross product.

Public Members

TType x

First coordinate of the vector.

TType y

Second coordinate of the vector.

TType z

Third coordinate of the vector.

class Random2DCoordinateGenerator

A pseudo-random number generator using a 2D coordinate and seed.

Generates deterministic pseudo-random numbers based on a seed and 2D coordinates.

Public Functions

Random2DCoordinateGenerator(const long long &seed = 42LL)

Constructs a Random2DCoordinateGenerator with a given seed.

Parameters:

seed – The seed for the generator.

long long seed() const

Returns the current seed of the generator.

Returns:

The seed as an long long.

long long operator()(const long long &x, const long long &y) const

Generates a pseudo-random number using the given coordinates.

Parameters:
  • x – The x-coordinate.

  • y – The y-coordinate.

Returns:

A pseudo-random number as long long.

Private Members

const long long seed_value

The seed value for the generator.

class PerlinNoise2D

A class for generating 2D Perlin noise values.

Provides methods to sample Perlin noise at given 2D coordinates.

Public Functions

PerlinNoise2D(int seed = 42)

Constructs a PerlinNoise2D generator with an optional seed.

Parameters:

seed – The seed for initializing the noise generator (default: 42).

float sample(float x, float y) const

Samples Perlin noise at the given 2D coordinates.

Parameters:
  • x – The x-coordinate.

  • y – The y-coordinate.

Returns:

A float value representing the Perlin noise at the coordinates in range [-1, 1].

Private Functions

float fade(float t) const

Computes a fade function for smoothing.

Parameters:

t – The input value.

Returns:

The smoothed value.

float lerp(float t, float a, float b) const

Computes a linear interpolation.

Parameters:
  • t – The interpolation factor.

  • a – The start value.

  • b – The end value.

Returns:

The interpolated value.

float grad(int hash, float x, float y) const

Computes a gradient for a hash and coordinates.

Parameters:
  • hash – The gradient hash value.

  • x – The x-offset.

  • y – The y-offset.

Returns:

The gradient dot product.

Private Members

std::vector<int> permutation_

Permutation table for gradient hashing.