Page 1 of 2

MathLib - SOHCAHTOA [Update 1/8/11]

Posted: Mon Jul 12, 2010 5:27 am
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 ""

Re: MathLib - SOACAHTOA

Posted: Mon Jul 12, 2010 5:29 am
by aliasmask
[RESERVED]

These are the built in mathematical functions.

Suggestions for new functions or bug reports are welcomed.

Re: MathLib - SOACAHTOA

Posted: Mon Jul 12, 2010 7:59 am
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)

Re: MathLib - SOACAHTOA

Posted: Mon Jul 12, 2010 12:21 pm
by aliasmask
Actually, that's already a built in function. I just added the link to the built functions above for reference.

Re: MathLib - SOACAHTOA

Posted: Mon Sep 20, 2010 8:10 am
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.

Re: MathLib - SOACAHTOA

Posted: Thu Jan 06, 2011 12:48 pm
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))]

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

Posted: Sat Jan 08, 2011 7:01 am
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.

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

Posted: Mon Jan 17, 2011 10:59 am
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?

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

Posted: Mon Jan 17, 2011 11:28 am
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

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

Posted: Mon Jan 17, 2011 11:45 am
by akuma
Great, thanks! I will gladly test out the new version as it becomes available. :)

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

Posted: Mon Jan 17, 2011 12:16 pm
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.

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

Posted: Mon Jan 17, 2011 12:45 pm
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.

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

Posted: Thu Jan 20, 2011 5:33 am
by akuma
*drooling over the forthcoming mathlib update* :D

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

Posted: Thu Jan 20, 2011 9:35 am
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.

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

Posted: Sun Aug 19, 2012 12:18 pm
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.