Trig and Maptools

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

User avatar
RPTroll
TheBard
Posts: 3159
Joined: Tue Mar 21, 2006 7:26 pm
Location: Austin, Tx
Contact:

Re: Trig and Maptools

Post by RPTroll »

Someone, or perhaps a lot of someones, should develop a Lib:Math token to share.
ImageImage ImageImageImageImage
Support RPTools by shopping
Image
Image

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Trig and Maptools

Post by Azhrei »

And copy it to the wiki with the appropriate links from the Math category. ;)

Akodo Makama
Giant
Posts: 249
Joined: Mon Apr 20, 2009 9:31 pm

Re: Trig and Maptools

Post by Akodo Makama »

zEal wrote:
Akodo Makama wrote:I just copypasted what MatLab spit out at me.
Is there any way of having it spit out more? :)
1/2*Pi - x -1/6*x^3
-3/40*x^5
-5/112*x^7
-35/1152*x^9
-63/2816*x^11
-231/13312*x^13
-143/10240*x^15
-6435/557056*x^17
-12155/1245184*x^19
-46189/5505024*x^21
-88179/12058624*x^23
-676039/104857600*x^25
-1300075/226492416*x^27
-5014575/973078528*x^29
-9694845/2080374784*x^31
-100180065/23622320128*x^33
-116680311/30064771072*x^35
-2268783825/635655159808*x^37
-1472719325/446676598784*x^39
-34461632205/11269994184704*x^41
-67282234305/23639499997184*x^43
-17534158031/6597069766656*x^45
-514589420475/206708186021888*x^47
-8061900920775/3448068464705536*x^49
-5267108601573/2392537302040576*x^51
-61989816618513/29836347531329536*x^53

e < 10^-30 good enough (10^-20 only needs up to term 37)? The recurrence formula is:

Code: Select all

N^2 * u(N) + (-n^2 – 3n – 2) * u(n+2) = 0
U(0)=1/2*pi(), U(1) = -1, n>=0[code], should you require a higher order equation

User avatar
zEal
Dragon
Posts: 944
Joined: Sun Mar 22, 2009 2:25 am

Re: Trig and Maptools

Post by zEal »

More than enough, thanks a lot. :)

Akodo Makama
Giant
Posts: 249
Joined: Mon Apr 20, 2009 9:31 pm

Re: Trig and Maptools

Post by Akodo Makama »

zEal wrote:More than enough, thanks a lot. :)
I missed something earlier. My error values were only for values 'close' to (within 0.25 of) the origin (0 in this case).

The error at 0 is 0 after only the pi()/2 term 8)
But the error at 0.5 needs 37 terms to drop the error to 7.8e-15 :(
To get e-15 errors at 0.9, we need 131 terms :shock:
Even then (131 terms) the error at 1 would still be 5e-2. That's 0.05, or 1/20.

I should have looked closer, as this is a well known problem with Taylor polynomial expansions. All the coefficients have the same sign. That's bad news for extreme input values (close to 1 or -1). There is a fix (use multiple expansions from different central points with weighted averaging or piecewise definition), but it's a bit ugly (as acos(x) is undefined at x>1), and I'll need time to figure out how what we'll need for e-15 errors from [-1..1]

All the other expansions likely suffer from the same problem.

Akodo Makama
Giant
Posts: 249
Joined: Mon Apr 20, 2009 9:31 pm

Re: Trig and Maptools

Post by Akodo Makama »

Ok, given asin, acos is simple

acos(x) = 2 * asin( sqrt( (1-x)/2 ) )

You'd think I'd remember little things like that. It was only 10 years ago I used these things on a regular basis.

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Trig and Maptools

Post by Azhrei »

I remember a proof on an exam that was worth extra credit. I didn't even know where to start:

e * i^pi + 1 = 0

I think that was calc3, IIRC. Fortunately, I don't use that kind of higher math at all on a regular basis. ;)

Akodo Makama
Giant
Posts: 249
Joined: Mon Apr 20, 2009 9:31 pm

Re: Trig and Maptools

Post by Akodo Makama »

Azhrei wrote:I remember a proof on an exam that was worth extra credit. I didn't even know where to start:

e * i^pi + 1 = 0

I think that was calc3, IIRC. Fortunately, I don't use that kind of higher math at all on a regular basis. ;)
You mean e^(i*pi()) +1 =0 , right?

seanb
Kobold
Posts: 2
Joined: Mon Oct 05, 2009 12:29 pm

Re: Trig and Maptools

Post by seanb »

This will pick a random angle between 1 and 90, and give you a good approximation of sine and cosine for that angle:

Code: Select all

[h: myAngle = 1d90]
[h: myRadians = divide(myAngle, 57.2958)]
[h: sinAngle = myRadians - divide(power(myRadians, 3),6)]
[h: sinAngle = sinAngle + divide(power(myRadians, 5),120)]
[h: sinAngle = sinAngle - divide(power(myRadians, 7),5040)]
[h: cosAngle = 1 - divide(power(myRadians, 2),2)]
[h: cosAngle = cosAngle + divide(power(myRadians, 4),24)]
[h: cosAngle = cosAngle - divide(power(myRadians, 6),720)]
Angle: [myAngle]<br>
SIN(Angle): [sinAngle]<br>
COS(Angle): [cosAngle]<br>
And to answer the previous poster's question about why you would want this: I put together a Macro for doing a scatter function for Warhammer 40K. By those rules, you get an angle and a distance to move the token. So I needed to create a macro that would determine a random angle and distance, and then move a selected token by that amount in the right direction.

User avatar
biodude
Dragon
Posts: 444
Joined: Sun Jun 15, 2008 2:40 pm
Location: Montréal, QC

Re: Trig and Maptools

Post by biodude »

RPTroll wrote:Someone, or perhaps a lot of someones, should develop a Lib:Math token to share.
Well, since you asked ...
Lib-Math-1.3b56.rptok
Misc. Math functions for MapTool
(40.86 KiB) Downloaded 196 times
I included all the trig & math functions in this thread:
  • pi
  • degrees
  • radians
  • sine
  • cosine
  • factorial
  • modulo
and a few extras:
  • robust eval()
  • my custom wrapper for number(), which returns 0 (or specified default value) if the argument is not a number.
  • my custom math.list(). Requires 2 arguments: function name and a string list, or json array, of values. It lets you perform math functions like max, sum, etc. on a list of values. ex. math.list( 'sum', valueList ).
[EDIT: aliasMask has kindly posted a larger version with even more functions, without the custom 'extras' included here. His versions are probably more accurate.
Newer version (v2.1).
]
Last edited by biodude on Tue Jun 29, 2010 8:43 am, edited 1 time in total.
"The trouble with communicating is believing you have achieved it"
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]

Malekith
Giant
Posts: 245
Joined: Wed Jan 07, 2009 4:36 pm
Location: Gloucester, England
Contact:

Re: Trig and Maptools

Post by Malekith »

great work there biodude!
Also, just while I'm typing I was wondering whether there were any functions in MT that can assess whether a number is odd or even? I can't find anything on the list of functions.
(not meaning to de-rail but it seems that it's a fairly good match as it may be a another useful math udf)

Mal
Mal

User avatar
biodude
Dragon
Posts: 444
Joined: Sun Jun 15, 2008 2:40 pm
Location: Montréal, QC

Re: Trig and Maptools

Post by biodude »

determining if a number is even or odd is a common task, but also fairly trivial:

Code: Select all

[H: evenOrOdd = if( floor( number / 2 )==( number / 2 ) , "even", "odd" )] 
which is why I don't think anyone has ever written an entire UDF for it. Would such a thing be useful to people?
"The trouble with communicating is believing you have achieved it"
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Trig and Maptools

Post by aliasmask »

I took the lib Biodude made and added some stuff. I did remove the eval and list sum functions though. This is my own personal version, so feel free to use and modify as you will. Here's a list of functions:

Lib:Math
  • onCampaignLoad - Loads all the functions as UDFs in the form math.function
  • angleToSlope(angle): slope - Converts the angle formed by the hypotenuse and x-axis of a right triangle to a slope value.
  • cos(value): radians - Cosine
  • sin(value): radians - Sine
  • tan(value): radians - Tangent
  • toDegrees(radians): degrees - Convert radians to degrees
  • factorial(start[,end,step]) - Does factors from start to end and allows skips with step parameter (defaults, end = 0, step = -1)
  • toRadians(degrees): radians - Convert degrees to radians
  • arcCos(value): radians - Inverse Cosine
  • arcSin(value): radians - Inverse Sine
  • arcTan(value): radians - Inverse Tangent
  • cahAngle(adjacent,hypotenuse): degrees - Returns the angle opposite the Adjacent and Hypotenuse of a right triangle (SOACAHTOA)
  • slopeToAngle(slope): degrees - Returns the arcTangent of a slope
  • sohAngle(opposite,hypotenuse): degrees - Returns the angle opposite the Opposite and Hypotenuse of a right triangle (SOACAHTOA)
  • toaAngle(opposite,adjacent): degrees - Returns the angle opposite the Opposite and Adjacent of a right triangle (SOACAHTOA)
  • div(number,divisor): integer - Returns the integer of the number divided by the divisor
  • mod(number,divisor): integer - Returns the integer of the remainder of the number divided by the divisor
  • val(string): number - Forces a string value to a number where if invalid returns 0
  • pi(): number - Returns the value of PI to 20 decimal places
  • isOdd(integer): boolean - Returns 1 or 0 if integer is odd or even (respectively). If not an integer returns ""
Attachments
Lib-Math-1.3b70.rptok
(39.23 KiB) Downloaded 156 times

User avatar
Raoden
Dragon
Posts: 381
Joined: Fri Dec 18, 2009 2:33 am
Location: San Diego

Re: Trig and Maptools

Post by Raoden »

Careful, aliasmask ... you might get highschoolers taking trig stopping by RPTools and downloading that, even if they have no interest in playing actual RPGs via MapTool! :wink:
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G. K. Chesterton

Wonderful HTML/CSS reference * Color Manager * Token Manager 2.0

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Trig and Maptools

Post by aliasmask »

Ha! But they'll have to learn the MACRO language first. Doing this brought back some of my highschool/college day memories.

And for any highschoolers... go to these pages: .
That's where I got most of my formulas

Post Reply

Return to “Macros”