math: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(17 intermediate revisions by 8 users not shown)
Line 2: Line 2:
|name=math
|name=math
|version=1.4.0.5
|version=1.4.0.5
|description=this is NOT an MT function but a collection of math functions in MT.  
|description=
'''Important Note''': ALL these functions return a floating-point number (e.g.: 3.0)
This is NOT a single MapTool function but a collection of math functions in MapTool.
<p />
'''Important Note''': Some of these functions have similar versions that don't have the <code>math.</code> prefix. These functions may differ slightly from those in implementation and output. For instance, most of these functions return a floating-point number (e.g.: <code>3.0</code>), so you may find it helpful to surround them with {{func|round}}, {{func|floor}}, or {{func|ceiling}}.


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
Numbers:
Numbers:
[r:val = math.pi()]
[r:val = math.pi()]
[r:val = math.e()]
[r:val = math.e()]


trigonomotry:
Trigonomotry:
[r:val = math.acos(degrees)]
[r:val = math.acos(degrees)]
[r:val = math.acos_r(radians)]
[r:val = math.acos_r(radians)]
Line 17: Line 19:
[r:val = math.asin_r(radians)]
[r:val = math.asin_r(radians)]
[r:val = math.atan(degrees)]
[r:val = math.atan(degrees)]
[r:val = math.atan_r(radians)]
[r:val = math.atan_r(num)] <!-- radians -->
[r:val = math.atan2(degrees)]
[r:val = math.atan2(y,x)] <!-- degrees -->
[r:val = math.atan2_r(radians)]
[r:val = math.atan2_r(y,x)] <!-- radians -->
[r:val = math.cos(degrees)]
[r:val = math.cos(degrees)]
[r:val = math.cos_r(num)]
[r:val = math.cos_r(num)]
Line 36: Line 38:
[r:val = math.pow(num1,num2)]
[r:val = math.pow(num1,num2)]


logarithmic
Logarithmic
[r:val = math.log(num)] (this is loge())
[r:val = math.log(num)] (this is the log to base e)
[r:val = math.log10(num)]
[r:val = math.log10(num)]


Pythagorean:
Pythagorean:
[r:val = math.hypot(num1,num2)]
[r:val = math.hypot(num1, num2)]
[r:val = math.hypotenuse(num1,num2)]
[r:val = math.hypotenuse(num1, num2)]


Simple operations
Simple operations
Line 53: Line 55:
[r:val = math.max(num1, num2, num2, etc.)]
[r:val = math.max(num1, num2, num2, etc.)]
[r:val = math.min(num1, num2, num2, etc.)]
[r:val = math.min(num1, num2, num2, etc.)]
[r:val = math.mod(num1,num2)]  
[r:val = math.mod(dividend, divisor)]  


</source>
</syntaxhighlight>


|examples=
|examples=<nowiki></nowiki>
====abs====
====abs====
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[r:val =  math.abs(-3)]
[r:val =  math.abs(-3)]
</source>
</syntaxhighlight>
Returns:<source lang="mtmacro" line start=2>3.0</source>
Returns: 3.0
 
====atan2_r====
<syntaxhighlight lang="mtmacro" line>
<!-- getFacingToTarget(sourceId,targetId,degreeRound): facing
  sourceId - tokenId of source
  targetId - tokenId of target
  degreeRound - (opt) round to the nearest degree increment, defaults to 1
 
  Return the facing from a source token to a target token, center to center
-->
[H: sourceId = arg(0)]
[H: targetId = arg(1)]
[H, if(argCount() >= 3): degreeRound = arg(2); degreeRound = 1]
 
<!-- calculate angle from center of source to center of target -->
[H: x1 = getTokenX(1,sourceId) + round(getTokenWidth(sourceId)/2)]
[H: y1 = getTokenY(1,sourceId) + round(getTokenHeight(sourceId)/2)]
[H: x2 = getTokenX(1,targetId) + round(getTokenWidth(targetId)/2)]
[H: y2 = getTokenY(1,targetId) + round(getTokenHeight(targetId)/2)]
 
[H, if(x1 == x2): direction = 90 * min(1,max(-1,y1-y2)); direction = math.atan2_r((y1-y2),(x2-x1)) / Math.pi() * 180]
[H: facing = degreeRound * round(direction/degreeRound)]
[H: macro.return = facing]
</syntaxhighlight>
Output range from -179 to 180 degrees.


====mod====
====mod====
<source lang="mtmacro" line>
Returns the result of the modulo operation between the two numbers, which represents the remainder after a division operation.
[r:val = math.mod(6,3)]
 
</source>
<syntaxhighlight lang="mtmacro" line>
Returns:<source lang="mtmacro" line start=2>0</source>
[r: math.mod(14,6)]
</syntaxhighlight>
Returns 2
 
<syntaxhighlight lang="mtmacro" line>
[r: math.mod(10,5)]
</syntaxhighlight>
Returns 0
 
<syntaxhighlight lang="mtmacro" line>
[r: math.mod(-13,4)]
</syntaxhighlight>
Returns -1


====pow====
====pow====
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[r:val =  math.pow(2,3)]
[r:val =  math.pow(2,3)]
</source>
</syntaxhighlight>
Returns:<source lang="mtmacro" line start=2>8.0</source>
Returns: 8.0
 
|also=
Some <code>math</code> functions are further documented on their own pages:
<ul><li>[[math.e]]</li><li>[[math.log]]</li></ul>
}}
}}
[[Category:Mathematical Function]]
[[Category:Mathematical Function]]

Latest revision as of 03:25, 17 April 2024

math() Function

Introduced in version 1.4.0.5
This is NOT a single MapTool function but a collection of math functions in MapTool.

Important Note: Some of these functions have similar versions that don't have the math. prefix. These functions may differ slightly from those in implementation and output. For instance, most of these functions return a floating-point number (e.g.: 3.0), so you may find it helpful to surround them with round(), floor(), or ceiling().

Usage

Numbers:
[r:val = math.pi()]
[r:val = math.e()]

Trigonomotry:
[r:val = math.acos(degrees)]
[r:val = math.acos_r(radians)]
[r:val = math.asin(degrees)]
[r:val = math.asin_r(radians)]
[r:val = math.atan(degrees)]
[r:val = math.atan_r(num)] <!-- radians -->
[r:val = math.atan2(y,x)] <!-- degrees -->
[r:val = math.atan2_r(y,x)] <!-- radians -->
[r:val = math.cos(degrees)]
[r:val = math.cos_r(num)]
[r:val = math.sin(degrees)]
[r:val = math.sin_r(num)]
[r:val = math.tan(degrees)]
[r:val = math.tan_r(num)]
[r:val = math.toDegrees(num)]
[r:val = math.toRadians(degrees)]

Power and root:
[r:val = math.sqrt(num)]
[r:val = math.squareroot(num)]
[r:val = math.cbrt(num)]
[r:val = math.cuberoot(num)]
[r:val = math.pow(num1,num2)]

Logarithmic
[r:val = math.log(num)] (this is the log to base e)
[r:val = math.log10(num)]

Pythagorean:
[r:val = math.hypot(num1, num2)]
[r:val = math.hypotenuse(num1, num2)]

Simple operations
[r:val = math.abs(num)]
[r:val = math.ceil(num)]
[r:val = math.floor(num)]
[r:val = math.isEven(num)]
[r:val = math.isInt(num)]
[r:val = math.isOdd(num)]
[r:val = math.max(num1, num2, num2, etc.)]
[r:val = math.min(num1, num2, num2, etc.)]
[r:val = math.mod(dividend, divisor)]

Examples

abs

[r:val =  math.abs(-3)]

Returns: 3.0

atan2_r

<!-- getFacingToTarget(sourceId,targetId,degreeRound): facing
   sourceId - tokenId of source
   targetId - tokenId of target
   degreeRound - (opt) round to the nearest degree increment, defaults to 1
   
   Return the facing from a source token to a target token, center to center
-->
[H: sourceId = arg(0)]
[H: targetId = arg(1)]
[H, if(argCount() >= 3): degreeRound = arg(2); degreeRound = 1]

<!-- calculate angle from center of source to center of target -->
[H: x1 = getTokenX(1,sourceId) + round(getTokenWidth(sourceId)/2)]
[H: y1 = getTokenY(1,sourceId) + round(getTokenHeight(sourceId)/2)]
[H: x2 = getTokenX(1,targetId) + round(getTokenWidth(targetId)/2)]
[H: y2 = getTokenY(1,targetId) + round(getTokenHeight(targetId)/2)]

[H, if(x1 == x2): direction = 90 * min(1,max(-1,y1-y2)); direction = math.atan2_r((y1-y2),(x2-x1)) / Math.pi() * 180]
[H: facing = degreeRound * round(direction/degreeRound)]
[H: macro.return = facing]

Output range from -179 to 180 degrees.

mod

Returns the result of the modulo operation between the two numbers, which represents the remainder after a division operation.

[r: math.mod(14,6)]

Returns 2

[r: math.mod(10,5)]

Returns 0

[r: math.mod(-13,4)]

Returns -1

pow

[r:val =  math.pow(2,3)]
Returns: 8.0

See Also

Some math functions are further documented on their own pages: