MathLib - SOHCAHTOA [Update 1/8/11]

These are tools and utilities that make it easier to run games. This includes Lib: macro tokens dropped into MapTool to manage the game, a conversion file for CharacterTool to allow use in MapTool, or just about anything else you can think of -- except graphics with macros and anything specific to a particular campaign framework. Those are already covered by the Tilesets subforum and the Links and External Resources forum.

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

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

MathLib - SOHCAHTOA [Update 1/8/11]

Post by aliasmask »

This was inspired and some code gleaned from biodude's submission on this thread here.

Other sources include: Lib:Math Functions
  • 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
MathLib 2.2.rptok
MT 1.3.b80
(39.26 KiB) Downloaded 205 times
Last edited by aliasmask on Sun Jan 16, 2011 8:20 pm, edited 3 times in total.

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

Re: MathLib - SOACAHTOA

Post by aliasmask »

[RESERVED]

These are the built in mathematical functions.

Suggestions for new functions or bug reports are welcomed.
Last edited by aliasmask on Mon Jul 12, 2010 12:19 pm, edited 1 time in total.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: MathLib - SOACAHTOA

Post by wolph42 »

nice!!

You could easily add power(x,y):number thats just a for loop with multiplier (there is a much more efficient way to do that but i don't think people will want x^1000)

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

Re: MathLib - SOACAHTOA

Post by aliasmask »

Actually, that's already a built in function. I just added the link to the built functions above for reference.

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

Re: MathLib - SOACAHTOA

Post by aliasmask »

I just found a bug/limitation with the arcTan formula. I'm going to try a different formula to see if I can get a better significance with the value. The first formula just doesn't work very well and requires too many iterations to get a good value. There are 2 other formulas to calculate the arcTan and I'll test those next.

But sine, cosine and tangent work great. I'm not sure about arcSine and arcCosine though, but from looking at the formula I think they're good and wouldn't have trouble with significance.

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

Re: MathLib - SOACAHTOA

Post by aliasmask »

Yep, sin and cos are broken for certain values. My test values used just happen to work, but values like 45 and 90 blow up and are completely wrong. I'll look in to changing it using a chart which will work for whole numbers only. The problem lies within the summation formula and significant digits.

edit: Nevermind. I was being dumb. Make sure when you use sin or cos to pass it radians.

Code: Select all

[H: degrees = 45]
[H: result = math.sin(math.toRadians(degrees))]

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

Re: MathLib - SOACAHTOA [Update 1/8/11]

Post by aliasmask »

Okay, I updated the arcTan and arcSin to be more accurate. Let me know if you get weird values because there are a couple of options to calculate those differently.

The function slopeToAngle is the same thing as using toDegrees(arcTan(x)) where it returns x in degrees and is nearly the same as toaAngle(rise,run) where you give it 2 values.

User avatar
akuma
Kobold
Posts: 12
Joined: Sun Jan 16, 2011 5:13 pm
Location: Oulu, Finland

Re: MathLib - SOHCAHTOA [Update 1/8/11]

Post by akuma »

aliasmask wrote:Okay, I updated the arcTan and arcSin to be more accurate. Let me know if you get weird values because there are a couple of options to calculate those differently.
Okay, I downloaded the Mathlib2.2 from the download link at the top of this page and it started working fine. Just to be sure, this is the arcTan macro i got with the Mathlib2.2:

Code: Select all

[H: radians = arg(0)]
[H: arcTangent = radians]
[H: sign = 1]
[H, for(i,3,150,2), code: {
   [H: sign = if(sign == -1,1,-1)]
   [H: arcTangent = arcTangent + (sign * power(radians,i) / i)]
}]
[H: macro.return = arcTangent]
Then I created the following macro to get atan(2)...

Code: Select all

/self 
[r,MACRO('arcTan@Lib:Math'):2]<br>
[r: myNumber = macro.return]
I got the result 3821168523081063463477134266326310553728605.688206467618381063500077081915933 - whereas it should be 1.10714871779409, right? :) Am I doing something wrong?

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

Re: MathLib - SOHCAHTOA [Update 1/8/11]

Post by aliasmask »

akuma wrote:
aliasmask wrote:Okay, I updated the arcTan and arcSin to be more accurate. Let me know if you get weird values because there are a couple of options to calculate those differently.
Okay, I downloaded the Mathlib2.2 from the download link at the top of this page and it started working fine. Just to be sure, this is the arcTan macro i got with the Mathlib2.2:

Code: Select all

[H: radians = arg(0)]
[H: arcTangent = radians]
[H: sign = 1]
[H, for(i,3,150,2), code: {
   [H: sign = if(sign == -1,1,-1)]
   [H: arcTangent = arcTangent + (sign * power(radians,i) / i)]
}]
[H: macro.return = arcTangent]
Then I created the following macro to get atan(2)...

Code: Select all

/self 
[r,MACRO('arcTan@Lib:Math'):2]<br>
[r: myNumber = macro.return]
I got the result 3821168523081063463477134266326310553728605.688206467618381063500077081915933 - whereas it should be 1.10714871779409, right? :) Am I doing something wrong?
Yeah, there is a problem with using the power() function in MT and the formula loses significant digits. I was testing it with values below 1 before. I did see another method results from one of the other functions, but I'll have to look that up. I was afraid arcTan was still messed up.

edit: I'm going to see how this formula works. The numbers don't get too large too fast.

Image

User avatar
akuma
Kobold
Posts: 12
Joined: Sun Jan 16, 2011 5:13 pm
Location: Oulu, Finland

Re: MathLib - SOHCAHTOA [Update 1/8/11]

Post by akuma »

Great, thanks! I will gladly test out the new version as it becomes available. :)

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

Re: MathLib - SOHCAHTOA [Update 1/8/11]

Post by aliasmask »

Hmm.. that's not working so well. I just tested 41! which is when n=20 and MT has 53 digit number and the calculator has 50. I think I saw a conversion using arcSin or something like that and I'll try that one out. No factorials in that formula.

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

Re: MathLib - SOHCAHTOA [Update 1/8/11]

Post by aliasmask »

Okay, try think for the arcTan macro:

Code: Select all

[H: radians = arg(0)]
[H: arcTangent = radians]
[H: macro.return = math.arcSin(radians/sqrt(sqr(radians)+1))]
Seems to be only 9 or 10 sig figs, but that's better than it was. I'll update the library later. I've been up all night and I'm dying for a nap.

User avatar
akuma
Kobold
Posts: 12
Joined: Sun Jan 16, 2011 5:13 pm
Location: Oulu, Finland

Re: MathLib - SOHCAHTOA [Update 1/8/11]

Post by akuma »

*drooling over the forthcoming mathlib update* :D

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

Re: MathLib - SOHCAHTOA [Update 1/8/11]

Post by aliasmask »

I think I'll have to do a value check using one formula for values for 1 and below and another formula for values above 1(-1) because of "i" values.

User avatar
Bone White
Great Wyrm
Posts: 1124
Joined: Tue Aug 23, 2011 11:41 am
Location: Cornwall, UK

Re: MathLib - SOHCAHTOA [Update 1/8/11]

Post by Bone White »

Trying to calculate two angles, one, the angle between north, through the initiative token, and ending on the selected token (so I can face the initiative token to the target token when shooting). The output should be in degrees, but where am I going wrong, it doesn't work properly:

[code=php][h: selected = getSelected()]
[h: targetX = getTokenX(0, selected)]
[h: targetY = getTokenY(0, selected)]
[h: initiative = getInitiativeToken()]
[h: initiativeX = getTokenX(0, initiative)]
[h: initiativeY = getTokenY(0, initiative)]
[r: xDiff = targetX - initiativeX] 
[r: yDiff = 0 - (targetY - initiativeY)]
[r: tanAngle = arcTan(xDiff/yDiff)]
[r: angle = toDegrees(tanAngle))] [/code]


Second one is a little more complicated, which is getting the angle from the target initiative token, to the selected token, to the selected token's facing, but as long as I can use the first correctly, I should be able to do the second.

Post Reply

Return to “Drop-In Macro Resources”