ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


functions:math

This is an old revision of the document!


Math functions

This is a list of all simple functions in ACSUtils.

Constants

  • fixed MATH_E – the number E.
  • fixed PI – the number π (pi).
  • fixed SQRT_2 – square root of 2

Generic math functions

abs

num abs(num x)

Returns the absolute value of x.

Example

abs(2.0) == 2.0;
abs(-123) == 123;

clamp

num clamp(num x, num a, num b)

Limits x to the range [a; b]. If x falls in the range, returns x. If x is less than a, returns a. If x is greater than b, returns b.

Example

clamp(7, 6, 8) == 7;
clamp(0.1, 0.2, 0.3) == 0.2;

cmp

int cmp(num a, num b)

Compares two numbers and returns the result:

  • 1 – a is greater than b
  • -1 – a is less than b
  • 0 – a and b are equal

cond

any cond(bool condition, any a, any b)

Returns a if the condition is true, b otherwise.

It's a simulation of the ternary operator ?: in ACS.

Both a and b are evaluated. Do not do this:
int y = cond(x, DoSomething(), DoSomethingElse)

as both DoSomething() and DoSomethingElse() will be called.

max

num max(num a, num b)

Returns the greater of two numbers.

Example

max(3, 5) == 5;
max(8.0, 7.5) == 8.0;

min

num min(num a, num b)

Returns the lesser of two numbers.

Example

min(3, 5) == 3;
min(8.0, 7.5) == 7.5;

sgn

int sgn(num x)

Returns the sign of x.

  • 1 – x is positive
  • -1 – x is negative
  • 0 – x is zero

Example

sgn(12.3) == 1;
sgn(0) == 0;
sgn(-7) == -1;

Rounding

There are four rounding modes:

  • trunc – zeroes the fractional part of the number
  • floor – rounds the number down
  • ceil – round the number up
  • round – rounds the number to the closest integer

Rounding table:

   x  round(x) floor(x)  ceil(x) trunc(x)
----- -------- -------- -------- --------
 2.3         2        2        3        2
 3.8         4        3        4        3
 5.5         6        5        6        5
-2.3        -2       -3       -2       -2
-3.8        -4       -4       -3       -3
-5.5        -5       -6       -5       -5

Every rounding mode is available as two functions. The ones with the “i” in the name return an integer, while the ones without the “i” return a fixed.

ceil

fixed ceil(fixed x)

int iceil(fixed x)

Returns x rounded up.

floor

fixed floor(fixed x)

int ifloor(fixed x)

Returns x rounded down.

round

fixed round(fixed x)

int iround(fixed x)

Returns x rounded to the closest integer.

trunc

fixed trunc(fixed x)

int itrunc(fixed x)

Returns x with fractional part removed.

Numerical algorithms

IntDiv

fixed IntDiv(int a, int b)

Divides two integers and returns a fixed-point result. Allows division of any 2 integers whose ratio is not higher than 32767.

lerp

fixed lerp(fixed a, fixed b, fixed alpha)

Performs linear interpolation (or extrapolation) between two numbers and returns the result.

Examples

lerp(a, b, 0.0) == a;
lerp(a, b, 1.0) == b;
lerp(a, b, 0.5) == (a + b) / 2; // Average of a and b.
// Simple animation.
for (int time = 0; time < 1.0; time += 0.05)
{
    int x = lerp(x1, x2, time); 
    int y = lerp(y1, y2, time);
    DrawSomething(x, y);
    Delay(1);
}

gcf

num gcf(num a, num b)

Returns the greatest common factor of two numbers. The returned factor will be negative if one or both numbers are negative.

Example

gcf(18,   24)   == 6;
gcf(18.0, 24.0) == 6.0;
gcf(-18,  -24)  == -6;

mod

num mod(num a, num b)

Returns a % b, but the result is always positive. Only positive b values are supported.

Example

mod(2, 5) == 3;
mod(-3, 5) == 3;

npo2

num npo2(num x)

Rounds the number up to the Next Power Of 2. The input must be positive.

Example

npo2(17) == 32;
npo2(6.5) == 8.0;

sqrt

int zan_Sqrt(int number)

fixed zan_FixedSqrt(fixed number)

Zandronum implementations of corresponding ZDoom 2.7.0-only functions.

Trigonometry

Sin and cos are already in ZDoom.

tan

fixed tan(angle x)

Returns the tangent of x.

cot

fixed cot(angle x)

Returns the cotangent of x.

sec

fixed sec(angle x)

Returns the secant of x.

cosec

fixed cosec(angle x)

Returns the cosecant of x.

asin

angle asin(fixed x)

Returns the arcsine of x.

acos

angle acos(fixed x)

Returns the arccosine of x.

atan

angle atan(fixed x)

Returns the arctangent of x.

acot

angle acot(fixed x)

Returns the arccotangent of x.

asec

angle asec(fixed x)

Returns the arcsecant of x.

acosec

angle acosec(fixed x)

Returns the arccosecant of x.

Vectors

dot2

fixed dot2(fixed x1, fixed y1, fixed x2, fixed y2)

Returns the dot product of two 2D vectors.

dot3

fixed dot3(fixed x1, fixed y1, fixed z1, fixed x2, fixed y2, fixed z2)

Returns the dot product of two 3D vectors.

length2d

fixed length2d(fixed x, fixed y)

Returns the length of the given 2D vector. This is a Zandronum replacement for VectorLength.

length2d2

fixed length2d2(int x, int y)

Returns the squared length of the given 2D vector.

length3d

fixed length3d(fixed x, fixed y, fixed z)

Returns the length of the given 3D vector.

length3d2

fixed length3d2(int x, int y, int z)

Returns the squared length of the given 3D vector.

normalize2d

fixed, fixed normalize2d(fixed x, fixed y)

Normalizes the given 2D vector and returns the result.

This function returns multiple values.

normalize3d

fixed, fixed, fixed normalize3d(fixed x, fixed y, fixed z)

Normalizes the given 3D vector and returns the result.

This function returns multiple values.

RotatePoint

fixed, fixed RotatePoint(fixed x, fixed y, fixed originX, fixed originY, angle angle)

Rotates the given point (x; y) around the given origin by the given angle and returns the resulting coordinates of the point.

This function returns multiple values.

RotateVector

fixed, fixed RotateVector(fixed x, fixed y, angle angle)

Rotates the vector by the given angle and returns its new coordinates.

This function returns multiple values.

Example

RotateVector(x, y, angle);
int newX = r1;
int newY = r2;

VectorToAngles

angle, angle VectorToAngles(fixed x, fixed y, fixed z)

Converts a vector to a pair of angles (yaw and pitch) that point to the same direction.

This function returns multiple values.

Example

// Make the player look along the vector
VectorToAngles(1.0, 2.0, 3.0);
int angle = r1;
int pitch = r2;
SetActorAngle(tid, angle);
SetActorPitch(tid, -pitch); // Note that actor pitch is inverted in ZDoom.
Use LookAtPoint instead of the example code above.
functions/math.1470865270.txt.gz · Last modified: 2016/08/10 21:41 by korshun