Math module

Math related stuff.

Files

file bn_clip_line.h
file bn_fixed.h
file bn_fixed_fwd.h
file bn_fixed_point.h
file bn_fixed_point_fwd.h
file bn_fixed_rect.h
file bn_fixed_rect_fwd.h
file bn_fixed_size.h
file bn_fixed_size_fwd.h
file bn_math.h
file bn_point.h
file bn_power_of_two.h
file bn_reciprocal_lut.h
file bn_rect.h
file bn_rule_of_three_approximation.h
file bn_sin_lut.h
file bn_size.h
file bn_top_left_fixed_rect.h
file bn_top_left_fixed_rect_fwd.h
file bn_top_left_rect.h

Classes

template<int Precision>
struct bn::hash<fixed_t<Precision>>
Hash support for fixed_t.
template<int Precision>
class bn::fixed_t
Poor man's float.
template<int Precision>
struct bn::hash<fixed_point_t<Precision>>
Hash support for fixed_point_t.
template<int Precision>
class bn::fixed_point_t
Defines a two-dimensional point using fixed point precision.
template<int Precision>
struct bn::hash<fixed_rect_t<Precision>>
Hash support for fixed_rect_t.
template<int Precision>
class bn::fixed_rect_t
Defines a two-dimensional rectangle using fixed point precision and a center point as position.
template<int Precision>
struct bn::hash<fixed_size_t<Precision>>
Hash support for fixed_size_t.
template<int Precision>
class bn::fixed_size_t
Defines the size of a two-dimensional object using fixed point precision.
class bn::point
Defines a two-dimensional point using integer precision.
template<>
struct bn::hash<point>
Hash support for point.
class bn::rect
Defines a two-dimensional rectangle using integer precision and a center point as position.
template<>
struct bn::hash<rect>
Hash support for rect.
class bn::rule_of_three_approximation
Approximates a rule of three cross-multiplication (x = (value * multiplier) / divisor).
class bn::size
Defines the size of a two-dimensional object using integer precision.
template<>
struct bn::hash<size>
Hash support for size.
template<int Precision>
struct bn::hash<top_left_fixed_rect_t<Precision>>
Hash support for top_left_fixed_rect_t.
template<int Precision>
class bn::top_left_fixed_rect_t
Defines a two-dimensional rectangle using fixed point precision and a top-left point as position.
class bn::top_left_rect
Defines a two-dimensional rectangle using integer precision and a top-left point as position.
template<>
struct bn::hash<top_left_rect>
Hash support for top_left_rect.

Functions

template<typename Type>
auto clip_line(Type x1, Type y1, Type x2, Type y2, Type clip_x, Type clip_y, Type clip_width, Type clip_height) -> optional<array<Type, 4>> constexpr
Clips a line to a rectangular area.
template<typename Type>
auto abs(Type value) -> Type constexpr
Returns the absolute value of the given value.
auto sqrt(int value) -> BN_CONST constexpr int constexpr
Returns the integer square root of the given value.
template<int Precision>
auto sqrt(fixed_t<Precision> value) -> BN_CONST constexpr fixed_t<(Precision+1)/2> constexpr
Returns the fixed point square root of the given value.
template<int Precision>
auto unsafe_sqrt(fixed_t<Precision> value) -> BN_CONST constexpr fixed_t<Precision> constexpr
Returns the fixed point square root of the given value without trying to avoid overflow.
auto safe_degrees_angle(fixed degrees_angle) -> fixed constexpr
Returns an angle in degrees in the range [0..360) from the given angle in degrees in any range.
auto sin(fixed_t<16> angle) -> fixed constexpr
Calculates the sine value of an angle.
auto degrees_sin(fixed degrees_angle) -> fixed constexpr
Calculates the sine value of an angle in degrees.
auto lut_sin(int lut_angle) -> fixed constexpr
Calculates the sine value of an angle using a LUT.
auto degrees_lut_sin(fixed degrees_angle) -> fixed constexpr
Calculates the sine value of an angle in degrees using a LUT.
auto degrees_lut_sin_safe(fixed degrees_angle) -> fixed constexpr
Calculates the sine value of an angle in degrees in any range using a LUT.
auto cos(fixed_t<16> angle) -> fixed constexpr
Calculates the cosine value of an angle.
auto degrees_cos(fixed degrees_angle) -> fixed constexpr
Calculates the cosine value of an angle in degrees.
auto lut_cos(int lut_angle) -> fixed constexpr
Calculates the cosine value of an angle using a LUT.
auto degrees_lut_cos(fixed degrees_angle) -> fixed constexpr
Calculates the cosine value of an angle in degrees using a LUT.
auto degrees_lut_cos_safe(fixed degrees_angle) -> fixed constexpr
Calculates the cosine value of an angle in degrees in any range using a LUT.
auto sin_and_cos(fixed_t<16> angle) -> pair<fixed, fixed> constexpr
Calculates the sine and the cosine values of an angle.
auto degrees_sin_and_cos(fixed degrees_angle) -> pair<fixed, fixed> constexpr
Calculates the sine and the cosine values of an angle in degrees.
auto lut_sin_and_cos(int lut_angle) -> pair<fixed, fixed> constexpr
Calculates the sine and the cosine values value of an angle using a LUT.
auto degrees_lut_sin_and_cos(fixed degrees_angle) -> pair<fixed, fixed> constexpr
Calculates the sine and the cosine values of an angle in degrees using a LUT.
auto degrees_lut_sin_and_cos_safe(fixed degrees_angle) -> pair<fixed, fixed> constexpr
Calculates the sine and the cosine values of an angle in degrees in any range using a LUT.
auto atan2(int y, int x) -> BN_CONST fixed_t<16>
Computes the arc tangent of y/x using the signs of arguments to determine the correct quadrant.
auto degrees_atan2(int y, int x) -> BN_CONST fixed
Computes the arc tangent of y/x using the signs of arguments to determine the correct quadrant.
auto diamond_angle(int y, int x) -> fixed_t<16> constexpr
Computes an approximation of the arc tangent of y/x using the signs of arguments to determine the correct quadrant.
auto degrees_diamond_angle(int y, int x) -> fixed constexpr
Computes an approximation of the arc tangent of y/x using the signs of arguments to determine the correct quadrant.
auto lut_reciprocal(int lut_value) -> fixed_t<20> constexpr
Calculates the reciprocal of a value using a LUT.
template<typename Type>
auto power_of_two(Type value) -> bool constexpr
Indicates if the given value is a power of two or not.
template<int Precision = 20>
auto calculate_reciprocal_lut_value(int lut_value) -> fixed_t<Precision> constexpr
Calculates the value to store in a reciprocal LUT for the given value.
auto calculate_sin_lut_value(int lut_angle) -> int constexpr
Calculates the value to store in sin_lut for the given angle.

Variables

int reciprocal_lut_size constexpr
Reciprocal 20bit LUT size.
const array<fixed_t<20>, reciprocal_lut_size>& reciprocal_lut
Reciprocal 20bit LUT.
int reciprocal_16_lut_size constexpr
Reciprocal 16bit LUT size.
const array<uint16_t, reciprocal_16_lut_size>& reciprocal_16_lut
Reciprocal 16bit LUT.
int sin_lut_size constexpr
Size of the sine LUT of 16bit values in 4.12 format.
const array<int16_t, sin_lut_size>& sin_lut
Sine LUT of 16bit values in 4.12 format.

Function documentation

template<typename Type>
optional<array<Type, 4>> clip_line(Type x1, Type y1, Type x2, Type y2, Type clip_x, Type clip_y, Type clip_width, Type clip_height) constexpr

Clips a line to a rectangular area.

Parameters
x1 Horizontal position of the start point of the line.
y1 Vertical position of the start point of the line.
x2 Horizontal position of the end point of the line.
y2 Vertical position of the end point of the line.
clip_x Horizontal position of the top-left point of the clipping rectangle.
clip_y Vertical position of the top-left point of the clipping rectangle.
clip_width Width of the clipping rectangle (> 0).
clip_height Height of the clipping rectangle (> 0).
Returns An array with the clipped line coordinates in the form { x1, y1, x2, y2 } if at least part of the line is inside the rectangular clipping area; bn::nullopt otherwise.

Implementation provided by https://github.com/scivision/lineclipping-julia

#include <bn_math.h>
template<typename Type>
Type abs(Type value) constexpr

Returns the absolute value of the given value.

BN_CONST constexpr int sqrt(int value) constexpr
#include <bn_math.h>

Returns the integer square root of the given value.

#include <bn_math.h>
template<int Precision>
BN_CONST constexpr fixed_t<(Precision+1)/2> sqrt(fixed_t<Precision> value) constexpr

Returns the fixed point square root of the given value.

See https://github.com/JoaoBaptMG/gba-modern/blob/master/source/math/fixedmath.hpp

#include <bn_math.h>
template<int Precision>
BN_CONST constexpr fixed_t<Precision> unsafe_sqrt(fixed_t<Precision> value) constexpr

Returns the fixed point square root of the given value without trying to avoid overflow.

See https://github.com/JoaoBaptMG/gba-modern/blob/master/source/math/fixedmath.hpp

fixed safe_degrees_angle(fixed degrees_angle) constexpr
#include <bn_math.h>

Returns an angle in degrees in the range [0..360) from the given angle in degrees in any range.

fixed sin(fixed_t<16> angle) constexpr
#include <bn_math.h>

Calculates the sine value of an angle.

Parameters
angle Angle (2π = 1).
Returns Sine value in the range [-1..1].

fixed degrees_sin(fixed degrees_angle) constexpr
#include <bn_math.h>

Calculates the sine value of an angle in degrees.

Parameters
degrees_angle Angle in degrees.
Returns Sine value in the range [-1..1].

fixed lut_sin(int lut_angle) constexpr
#include <bn_math.h>

Calculates the sine value of an angle using a LUT.

Parameters
lut_angle Angle in the range [0..2048].
Returns Sine value in the range [-1..1].

fixed degrees_lut_sin(fixed degrees_angle) constexpr
#include <bn_math.h>

Calculates the sine value of an angle in degrees using a LUT.

Parameters
degrees_angle Angle in degrees in the range [0..360].
Returns Sine value in the range [-1..1].

fixed degrees_lut_sin_safe(fixed degrees_angle) constexpr
#include <bn_math.h>

Calculates the sine value of an angle in degrees in any range using a LUT.

Parameters
degrees_angle Angle in degrees in any range.
Returns Sine value in the range [-1..1].

fixed cos(fixed_t<16> angle) constexpr
#include <bn_math.h>

Calculates the cosine value of an angle.

Parameters
angle Angle (2π = 1).
Returns Cosine value in the range [-1..1].

fixed degrees_cos(fixed degrees_angle) constexpr
#include <bn_math.h>

Calculates the cosine value of an angle in degrees.

Parameters
degrees_angle Angle in degrees.
Returns Cosine value in the range [-1..1].

fixed lut_cos(int lut_angle) constexpr
#include <bn_math.h>

Calculates the cosine value of an angle using a LUT.

Parameters
lut_angle Angle in the range [0..2048].
Returns Cosine value in the range [-1..1].

fixed degrees_lut_cos(fixed degrees_angle) constexpr
#include <bn_math.h>

Calculates the cosine value of an angle in degrees using a LUT.

Parameters
degrees_angle Angle in degrees in the range [0..360].
Returns Cosine value in the range [-1..1].

fixed degrees_lut_cos_safe(fixed degrees_angle) constexpr
#include <bn_math.h>

Calculates the cosine value of an angle in degrees in any range using a LUT.

Parameters
degrees_angle Angle in degrees in any range.
Returns Cosine value in the range [-1..1].

pair<fixed, fixed> sin_and_cos(fixed_t<16> angle) constexpr
#include <bn_math.h>

Calculates the sine and the cosine values of an angle.

Parameters
angle Angle (2π = 1).
Returns Sine and cosine values in the range [-1..1].

pair<fixed, fixed> degrees_sin_and_cos(fixed degrees_angle) constexpr
#include <bn_math.h>

Calculates the sine and the cosine values of an angle in degrees.

Parameters
degrees_angle Angle in degrees.
Returns Sine and cosine values in the range [-1..1].

pair<fixed, fixed> lut_sin_and_cos(int lut_angle) constexpr
#include <bn_math.h>

Calculates the sine and the cosine values value of an angle using a LUT.

Parameters
lut_angle Angle in the range [0..2048].
Returns Sine and cosine values in the range [-1..1].

pair<fixed, fixed> degrees_lut_sin_and_cos(fixed degrees_angle) constexpr
#include <bn_math.h>

Calculates the sine and the cosine values of an angle in degrees using a LUT.

Parameters
degrees_angle Angle in degrees in the range [0..360].
Returns Sine and cosine values in the range [-1..1].

pair<fixed, fixed> degrees_lut_sin_and_cos_safe(fixed degrees_angle) constexpr
#include <bn_math.h>

Calculates the sine and the cosine values of an angle in degrees in any range using a LUT.

Parameters
degrees_angle Angle in degrees in any range.
Returns Sine and cosine values in the range [-1..1].

BN_CONST fixed_t<16> atan2(int y, int x)
#include <bn_math.h>

Computes the arc tangent of y/x using the signs of arguments to determine the correct quadrant.

Parameters
y Vertical value in the range [-32767..32767].
x Horizontal value in the range [-32767..32767].
Returns Arc tangent of y/x in the range [-0.5..0.5] (2π = 1).

BN_CONST fixed degrees_atan2(int y, int x)
#include <bn_math.h>

Computes the arc tangent of y/x using the signs of arguments to determine the correct quadrant.

Parameters
y Vertical value in the range [-32767..32767].
x Horizontal value in the range [-32767..32767].
Returns Arc tangent of y/x in degrees in the range [-180..180].

fixed_t<16> diamond_angle(int y, int x) constexpr
#include <bn_math.h>

Computes an approximation of the arc tangent of y/x using the signs of arguments to determine the correct quadrant.

Parameters
y Vertical value in the range [-131072..131071].
x Horizontal value in the range [-131072..131071].
Returns Arc tangent of y/x in the range [-0.5..0.5] (2π = 1).

https://www.freesteel.co.uk/wpblog/2009/06/05/encoding-2d-angles-without-trigonometry/

fixed degrees_diamond_angle(int y, int x) constexpr
#include <bn_math.h>

Computes an approximation of the arc tangent of y/x using the signs of arguments to determine the correct quadrant.

Parameters
y Vertical value in the range [-131072..131071].
x Horizontal value in the range [-131072..131071].
Returns Arc tangent of y/x in degrees in the range [-180..180].

https://www.freesteel.co.uk/wpblog/2009/06/05/encoding-2d-angles-without-trigonometry/

fixed_t<20> lut_reciprocal(int lut_value) constexpr
#include <bn_math.h>

Calculates the reciprocal of a value using a LUT.

Parameters
lut_value Value in the range [1..1024].
Returns Reciprocal of the given value (1 / value).

template<typename Type>
bool power_of_two(Type value) constexpr

Indicates if the given value is a power of two or not.

template<int Precision = 20>
fixed_t<Precision> calculate_reciprocal_lut_value(int lut_value) constexpr

Calculates the value to store in a reciprocal LUT for the given value.

Template parameters
Precision Number of bits used for the fractional part of the reciprocal value.
Parameters
lut_value Value in the range (>= 1).
Returns Reciprocal of the given value (1 / value).

int calculate_sin_lut_value(int lut_angle) constexpr

Calculates the value to store in sin_lut for the given angle.

Parameters
lut_angle sin_lut angle (360 degrees = 65536).
Returns Sine value in the range [-4096, 4096].

Variable documentation

int reciprocal_lut_size constexpr

Reciprocal 20bit LUT size.

const array<fixed_t<20>, reciprocal_lut_size>& reciprocal_lut

Reciprocal 20bit LUT.

int reciprocal_16_lut_size constexpr

Reciprocal 16bit LUT size.

const array<uint16_t, reciprocal_16_lut_size>& reciprocal_16_lut

Reciprocal 16bit LUT.

int sin_lut_size constexpr

Size of the sine LUT of 16bit values in 4.12 format.

const array<int16_t, sin_lut_size>& sin_lut

Sine LUT of 16bit values in 4.12 format.