This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| functions:math [2016/09/12 16:20] – [swap] korshun | functions:math [2017/06/12 15:36] (current) – removed korshun | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Math functions ====== | ||
| - | ===== Constants ===== | ||
| - | |||
| - | * '' | ||
| - | * '' | ||
| - | * '' | ||
| - | * '' | ||
| - | * '' | ||
| - | |||
| - | ===== Generic math functions ===== | ||
| - | |||
| - | ==== abs ==== | ||
| - | '' | ||
| - | |||
| - | Returns the absolute value of x. | ||
| - | |||
| - | **Example** | ||
| - | |||
| - | < | ||
| - | abs(2.0) == 2.0; | ||
| - | abs(-123) == 123; | ||
| - | </ | ||
| - | |||
| - | ==== clamp ==== | ||
| - | '' | ||
| - | |||
| - | 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 ==== | ||
| - | '' | ||
| - | |||
| - | Compares two numbers and returns the result: | ||
| - | * '' | ||
| - | * '' | ||
| - | * '' | ||
| - | |||
| - | ==== cond ==== | ||
| - | '' | ||
| - | |||
| - | Returns a if the condition is true, b otherwise. | ||
| - | |||
| - | It's a simulation of the ternary operator ?: in ACS. | ||
| - | |||
| - | <note important> | ||
| - | < | ||
| - | int y = cond(x, DoSomething(), | ||
| - | </ | ||
| - | as both '' | ||
| - | </ | ||
| - | |||
| - | ==== max ==== | ||
| - | '' | ||
| - | |||
| - | Returns the greater of two numbers. | ||
| - | |||
| - | **Example** | ||
| - | |||
| - | < | ||
| - | max(3, 5) == 5; | ||
| - | max(8.0, 7.5) == 8.0; | ||
| - | </ | ||
| - | |||
| - | ==== min ==== | ||
| - | '' | ||
| - | |||
| - | Returns the lesser of two numbers. | ||
| - | |||
| - | **Example** | ||
| - | |||
| - | < | ||
| - | min(3, 5) == 3; | ||
| - | min(8.0, 7.5) == 7.5; | ||
| - | </ | ||
| - | |||
| - | ==== sgn ==== | ||
| - | '' | ||
| - | |||
| - | Returns the sign of x. | ||
| - | * '' | ||
| - | * '' | ||
| - | * '' | ||
| - | |||
| - | **Example** | ||
| - | |||
| - | < | ||
| - | sgn(12.3) == 1; | ||
| - | sgn(0) == 0; | ||
| - | sgn(-7) == -1; | ||
| - | </ | ||
| - | |||
| - | ==== swap ==== | ||
| - | '' | ||
| - | |||
| - | Swaps two variables. Is a function to improve readability. | ||
| - | |||
| - | < | ||
| - | swap(x, y); | ||
| - | x = r1; | ||
| - | y = r2; | ||
| - | </ | ||
| - | |||
| - | instead of | ||
| - | |||
| - | < | ||
| - | tmp = x; | ||
| - | x = y; | ||
| - | y = tmp; | ||
| - | </ | ||
| - | |||
| - | [[..mulretval|This function returns multiple values.]] | ||
| - | |||
| - | ===== 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: | ||
| - | |||
| - | < | ||
| - | | ||
| - | ----- -------- -------- -------- -------- | ||
| - | | ||
| - | | ||
| - | | ||
| - | -2.3 -2 | ||
| - | -3.8 -4 | ||
| - | -5.5 -5 | ||
| - | </ | ||
| - | |||
| - | Every rounding mode is available as two functions. The ones with the " | ||
| - | |||
| - | ==== ceil ==== | ||
| - | '' | ||
| - | |||
| - | '' | ||
| - | |||
| - | Returns x rounded up. | ||
| - | |||
| - | ==== floor ==== | ||
| - | '' | ||
| - | |||
| - | '' | ||
| - | |||
| - | Returns x rounded down. | ||
| - | |||
| - | ==== round ==== | ||
| - | '' | ||
| - | |||
| - | '' | ||
| - | |||
| - | Returns x rounded to the closest integer. | ||
| - | |||
| - | ==== trunc ==== | ||
| - | '' | ||
| - | |||
| - | '' | ||
| - | |||
| - | Returns x with fractional part removed. | ||
| - | |||
| - | ===== Numerical algorithms ===== | ||
| - | |||
| - | ==== IntDiv ==== | ||
| - | '' | ||
| - | |||
| - | Divides two integers and returns a fixed-point result. Allows division of any 2 integers whose ratio is not higher than 32767. | ||
| - | |||
| - | ==== lerp ==== | ||
| - | '' | ||
| - | |||
| - | Performs [[wp> | ||
| - | |||
| - | **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, | ||
| - | Delay(1); | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | ==== gcf ==== | ||
| - | '' | ||
| - | |||
| - | Returns the greatest common factor of two numbers. The returned factor will be negative if one or both numbers are negative. | ||
| - | |||
| - | **Example** | ||
| - | |||
| - | < | ||
| - | gcf(18, | ||
| - | gcf(18.0, 24.0) == 6.0; | ||
| - | gcf(-18, | ||
| - | </ | ||
| - | |||
| - | ==== mod ==== | ||
| - | '' | ||
| - | |||
| - | Returns '' | ||
| - | |||
| - | **Example** | ||
| - | |||
| - | < | ||
| - | mod(2, 5) == 3; | ||
| - | mod(-3, 5) == 3; | ||
| - | </ | ||
| - | |||
| - | ==== npo2 ==== | ||
| - | '' | ||
| - | |||
| - | 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 ==== | ||
| - | '' | ||
| - | |||
| - | '' | ||
| - | |||
| - | Zandronum implementations of corresponding [[zdoom> | ||
| - | |||
| - | ===== Logarithms ===== | ||
| - | |||
| - | All logarithm functions come in two versions: fixed-point and integer. | ||
| - | |||
| - | All logarithm functions return fixed-point values. | ||
| - | |||
| - | <note tip>The integer versions are all prefixed with '' | ||
| - | |||
| - | ==== ln ==== | ||
| - | '' | ||
| - | |||
| - | '' | ||
| - | |||
| - | Returns the natual logaritm (base e) of the given number. | ||
| - | |||
| - | < | ||
| - | |||
| - | ==== log2 ==== | ||
| - | '' | ||
| - | |||
| - | '' | ||
| - | |||
| - | Returns the logarithm in base 2 of the given number. | ||
| - | |||
| - | ==== log10 ==== | ||
| - | '' | ||
| - | |||
| - | '' | ||
| - | |||
| - | Returns the logarithm in base 10 of the given number. | ||
| - | |||
| - | ==== logb ==== | ||
| - | '' | ||
| - | |||
| - | '' | ||
| - | |||
| - | Returns the logarithm in an arbitrary fixed-point base. Base must be a fixed number greater than 1.0. | ||
| - | |||
| - | |||
| - | ===== Trigonometry ===== | ||
| - | |||
| - | Sin and cos are already in ZDoom. | ||
| - | |||
| - | ==== tan ==== | ||
| - | '' | ||
| - | |||
| - | Returns the tangent of x. | ||
| - | |||
| - | ==== cot ==== | ||
| - | '' | ||
| - | |||
| - | Returns the cotangent of x. | ||
| - | |||
| - | ==== sec ==== | ||
| - | '' | ||
| - | |||
| - | Returns the secant of x. | ||
| - | |||
| - | ==== cosec ==== | ||
| - | '' | ||
| - | |||
| - | Returns the cosecant of x. | ||
| - | |||
| - | ==== asin ==== | ||
| - | '' | ||
| - | |||
| - | Returns the arcsine of x. | ||
| - | ==== acos ==== | ||
| - | '' | ||
| - | |||
| - | Returns the arccosine of x. | ||
| - | |||
| - | ==== atan ==== | ||
| - | '' | ||
| - | |||
| - | Returns the arctangent of x. | ||
| - | |||
| - | ==== acot ==== | ||
| - | '' | ||
| - | |||
| - | Returns the arccotangent of x. | ||
| - | |||
| - | ==== asec ==== | ||
| - | '' | ||
| - | |||
| - | Returns the arcsecant of x. | ||
| - | |||
| - | ==== acosec ==== | ||
| - | '' | ||
| - | |||
| - | Returns the arccosecant of x. | ||
| - | |||
| - | ===== Vectors ===== | ||
| - | |||
| - | ==== dot2 ==== | ||
| - | '' | ||
| - | |||
| - | Returns the dot product of two 2D vectors. | ||
| - | |||
| - | ==== dot3 ==== | ||
| - | '' | ||
| - | |||
| - | Returns the dot product of two 3D vectors. | ||
| - | |||
| - | ==== length2d ==== | ||
| - | '' | ||
| - | |||
| - | Returns the length of the given 2D vector. This is a Zandronum replacement for | ||
| - | [[zdoom> | ||
| - | ==== length2d2 ==== | ||
| - | '' | ||
| - | |||
| - | Returns the **squared** length of the given 2D vector. | ||
| - | |||
| - | ==== length3d ==== | ||
| - | '' | ||
| - | |||
| - | Returns the length of the given 3D vector. | ||
| - | |||
| - | ==== length3d2 ==== | ||
| - | '' | ||
| - | |||
| - | Returns the **squared** length of the given 3D vector. | ||
| - | |||
| - | ==== normalize2d ==== | ||
| - | '' | ||
| - | |||
| - | Normalizes the given 2D vector and returns the result. | ||
| - | |||
| - | [[..mulretval|This function returns multiple values.]] | ||
| - | |||
| - | ==== normalize3d ==== | ||
| - | '' | ||
| - | |||
| - | Normalizes the given 3D vector and returns the result. | ||
| - | |||
| - | [[..mulretval|This function returns multiple values.]] | ||
| - | |||
| - | ==== RotatePoint ==== | ||
| - | '' | ||
| - | |||
| - | Rotates the given point (x; y) around the given origin by the given angle and returns the resulting coordinates of the point. | ||
| - | |||
| - | [[..mulretval|This function returns multiple values.]] | ||
| - | |||
| - | ==== RotateVector ==== | ||
| - | '' | ||
| - | |||
| - | Rotates the vector by the given angle and returns its new coordinates. | ||
| - | |||
| - | [[..mulretval|This function returns multiple values.]] | ||
| - | |||
| - | **Example** | ||
| - | |||
| - | < | ||
| - | RotateVector(x, | ||
| - | int newX = r1; | ||
| - | int newY = r2; | ||
| - | </ | ||
| - | |||
| - | ==== VectorToAngles ==== | ||
| - | '' | ||
| - | |||
| - | Converts a vector to a pair of angles (yaw and pitch) that point to the same direction. | ||
| - | |||
| - | [[..mulretval|This function returns multiple values.]] | ||
| - | |||
| - | **Example** | ||
| - | |||
| - | < | ||
| - | // Make the player look along the vector | ||
| - | VectorToAngles(1.0, | ||
| - | int angle = r1; | ||
| - | int pitch = r2; | ||
| - | SetActorAngle(tid, | ||
| - | SetActorPitch(tid, | ||
| - | </ | ||
| - | |||
| - | <note tip>Use '' | ||