ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


functions:math

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
functions:math [2016/08/10 09:59] – [VectorToAngles] korshunfunctions:math [2017/06/12 15:36] (current) – removed korshun
Line 1: Line 1:
-====== 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** 
- 
-<code> 
-abs(2.0) == 2.0; 
-abs(-123) == 123; 
-</code> 
- 
-==== 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** 
- 
-<code> 
-clamp(7, 6, 8) == 7; 
-clamp(0.1, 0.2, 0.3) == 0.2; 
-</code> 
- 
-==== 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. 
- 
-<note important>Both a and b are evaluated. Do not do this: 
-<code> 
-int y = cond(x, DoSomething(), DoSomethingElse) 
-</code> 
-as both ''DoSomething()'' and ''DoSomethingElse()'' will be called. 
-</note>  
- 
-==== max ==== 
-''num max(num a, num b)'' 
- 
-Returns the greater of two numbers. 
- 
-**Example** 
- 
-<code> 
-max(3, 5) == 5; 
-max(8.0, 7.5) == 8.0; 
-</code> 
- 
-==== min ==== 
-''num min(num a, num b)'' 
- 
-Returns the lesser of two numbers. 
- 
-**Example** 
- 
-<code> 
-min(3, 5) == 3; 
-min(8.0, 7.5) == 7.5; 
-</code> 
- 
-==== sgn ==== 
-''int sgn(num x)'' 
- 
-Returns the sign of x. 
-  * ''1'' -- x is positive 
-  * ''-1'' -- x is negative 
-  * ''0'' -- x is zero 
- 
-**Example** 
- 
-<code> 
-sgn(12.3) == 1; 
-sgn(0) == 0; 
-sgn(-7) == -1; 
-</code> 
- 
-===== 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: 
- 
-<code> 
-    round(x) floor(x)  ceil(x) trunc(x) 
------ -------- -------- -------- -------- 
- 2.3                2        3        2 
- 3.8                3        4        3 
- 5.5                5        6        5 
--2.3        -2       -3       -2       -2 
--3.8        -4       -4       -3       -3 
--5.5        -5       -6       -5       -5 
-</code> 
- 
-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. 
- 
-<note important>IntDiv doesn't work with parameters over 32767 or under -32768, it's just a shorthand for ''FixedDiv(a<<16, b<<16)'' right now. Contributions to enable IntDiv to work with any integers as long as their ratio is less than 32767 are welcome.</note> 
- 
-==== lerp ==== 
-''fixed lerp(fixed a, fixed b, fixed alpha)'' 
- 
-Performs [[wp>Linear interpolation|linear interpolation]] (or extrapolation) between two numbers and returns the result. 
- 
-**Examples** 
- 
-<code> 
-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. 
-</code> 
- 
-<code> 
-// 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); 
-} 
-</code> 
- 
-==== 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** 
- 
-<code> 
-gcf(18,   24)   == 6; 
-gcf(18.0, 24.0) == 6.0; 
-gcf(-18,  -24)  == -6; 
-</code> 
- 
-==== mod ==== 
-''num mod(num a, num b)'' 
- 
-Returns ''a % b'', but the result is always positive. Only positive b values are supported. 
- 
-**Example** 
- 
-<code> 
-mod(2, 5) == 3; 
-mod(-3, 5) == 3; 
-</code> 
- 
-==== npo2 ==== 
-''num npo2(num x)'' 
- 
-Rounds the number up to the Next Power Of 2. The input must be positive. 
- 
-**Example** 
- 
-<code> 
-npo2(17) == 32; 
-npo2(6.5) == 8.0; 
-</code> 
- 
-==== sqrt ==== 
-''int zan_Sqrt(int number)'' 
- 
-''fixed zan_FixedSqrt(fixed number)'' 
- 
-Zandronum implementations of corresponding [[zdoom>Sqrt|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  
-[[zdoom>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. 
- 
-[[..mulretval|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. 
- 
-[[..mulretval|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. 
- 
-[[..mulretval|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. 
- 
-[[..mulretval|This function returns multiple values.]] 
- 
-**Example** 
- 
-<code> 
-RotateVector(x, y, angle); 
-int newX = r1; 
-int newY = r2; 
-</code> 
- 
-==== 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. 
- 
-[[..mulretval|This function returns multiple values.]] 
- 
-**Example** 
- 
-<code> 
-// 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. 
-</code> 
- 
-<note tip>Use ''LookAtPoint'' instead of the example code above.</note> 
functions/math.1470823185.txt.gz · Last modified: 2016/08/10 09:59 by korshun