ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


ru:functions:math

This is an old revision of the document!


Математические функции

Список всех математических функций ACSUtils.

Константы

  • fixed MATH_E – число е
  • fixed PI – число π (пи)
  • fixed SQRT_2 – квадратный корень из 2

Общие математические функции

abs

num abs(num x)

Возвращает модуль (|x|) числа x.

Пример

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

clamp

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

Заключает число x в промежуток [a; b]. Если число входит в промежуток, то возвращается x. Если x меньше a, возвращается a. Если число x больше b, возвращается b.

Пример

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

cmp

int cmp(num a, num b)

Сравнивает два числа и возвращает результат:

  • 1 – a > b
  • -1 – a < b
  • 0 – a = b

dist

num dist(num a, num b)

Возвращает абсолютное расстояние между числами.

Пример

dist(5.0, 8.0) == 3.0;
dist(8.0, 5.0) == 3.0;
dist(3.0, -2.0) == 5.0;

max

num max(num a, num b)

Возвращает большее из двух чисел.

Пример

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

min

num min(num a, num b)

Возвращает меньшее из двух чисел.

Пример

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

sign

int sign(num x)

Возвращает знак числа x.

  • 1 – x > 0 (положительное)
  • -1 – x < 0 (отрицательное)
  • 0 – x = 0

Пример

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

Округление

Есть четыре вида округления:

  • trunc – вообще отбрасывается дробная часть
  • floor – округление вниз (до наименьшего ближайшего целого числа)
  • ceil – округление вверх (до наибольшего ближайшего целого числа)
  • round – округляет до ближайшего целого числа

Для каждого вида округления существует две функции. Те, что начинаются с “i” возвращают целое число. Другие, без “i” возвращают значение с типом данных fixed.

ceil

fixed ceil(fixed x)

int iceil(fixed x)

Возвращает число x, округленное вверх (до наибольшего ближайшего целого числа).

floor

fixed floor(fixed x)

int ifloor(fixed x)

Возвращает число x, округленное вниз (до наименьшего ближайшего целого числа).

round

fixed round(fixed x)

int iround(fixed x)

Округляет число x до ближайшего целого числа.

trunc

fixed trunc(fixed x)

int itrunc(fixed x)

Возвращает число x без дробной части.

Численные алгоритмы

lerp

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

Выполняет линейную интерполяцию (или экстраполяцию) между двумя числами и возвращает результат.

Примеры

lerp(a, b, 0.0) == a;
lerp(a, b, 1.0) == b;
lerp(a, b, 0.5) == (a + b) / 2; // Среднее арифметическое от a и b
// Простая анимация.
for (int time = 0; time < 1.0; time += 0.05)
{
    int x = lerp(x1, x2, time); 
    int y = lerp(y1, y2, time);
    НарисоватьЧтоНибудь(x, y);
    Delay(1);
}

sqrt

int zan_Sqrt(int number)

fixed zan_FixedSqrt(fixed number)

Реализация квадратного корня для Zandronum, соответствует ZDoom 2.7.0 функциям.

Тригонометрия

Sin и cos уже присутствуют в Zdoom.

tan

fixed tan(angle x)

Возвращает tg x (тангенс).

cot

fixed cot(angle x)

Возвращает ctg x (котангенс).

sec

fixed sec(angle x)

Возвращает sec x (секанс).

cosec

fixed cosec(angle x)

Возвращает cosec x (косеканс).

asin

angle asin(fixed x)

Возвращает arcsin x (арксинус).

acos

angle acos(fixed x)

Возвращает arccos x (арккосинус).

atan

angle atan(fixed x)

Возвращает arctg x (арктангенс).

acot

angle acot(fixed x)

Возвращает arcctg x (арккотангнес).

asec

angle asec(fixed x)

Возвращает arcsec x (арксеканс).

acosec

angle acosec(fixed x)

Возвращает arccosec 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(angle);
SetActorPitch(pitch);
ru/functions/math.1461694879.txt.gz · Last modified: 2016/04/26 21:21 by djskaarj