RPTools.net

Discussion and Support

Skip to content

It is currently Fri Feb 23, 2018 11:26 pm 






Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

Previous topic | Next topic 

  Print view

Author Message
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 8012
Location: Bay Area
 Post subject: MathLib - SOHCAHTOA [Update 1/8/11]
PostPosted: Mon Jul 12, 2010 4:27 am 
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 ""


File comment: MT 1.3.b80
MathLib 2.2.rptok [39.26 KiB]
Downloaded 143 times

_________________
Downloads:


Last edited by aliasmask on Sun Jan 16, 2011 8:20 pm, edited 3 times in total.
Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 8012
Location: Bay Area
 Post subject: Re: MathLib - SOACAHTOA
PostPosted: Mon Jul 12, 2010 4:29 am 
[RESERVED]

These are the built in mathematical functions.

Suggestions for new functions or bug reports are welcomed.

_________________
Downloads:


Last edited by aliasmask on Mon Jul 12, 2010 11:19 am, edited 1 time in total.

Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Fri Mar 20, 2009 4:40 am
Posts: 9485
Location: Netherlands
 Post subject: Re: MathLib - SOACAHTOA
PostPosted: Mon Jul 12, 2010 6:59 am 
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)

_________________
GETTING STARTED WITH MAPTOOLS - TUTORIALS, DOCS, VIDEOS, TOOLS, ETC

My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent and
DPI.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 8012
Location: Bay Area
 Post subject: Re: MathLib - SOACAHTOA
PostPosted: Mon Jul 12, 2010 11:21 am 
Actually, that's already a built in function. I just added the link to the built functions above for reference.

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 8012
Location: Bay Area
 Post subject: Re: MathLib - SOACAHTOA
PostPosted: Mon Sep 20, 2010 7:10 am 
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.

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 8012
Location: Bay Area
 Post subject: Re: MathLib - SOACAHTOA
PostPosted: Thu Jan 06, 2011 12:48 pm 
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:
[H: degrees = 45]
[H: result = math.sin(math.toRadians(degrees))]

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 8012
Location: Bay Area
 Post subject: Re: MathLib - SOACAHTOA [Update 1/8/11]
PostPosted: Sat Jan 08, 2011 7:01 am 
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.

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Kobold
 
Joined: Sun Jan 16, 2011 5:13 pm
Posts: 12
Location: Oulu, Finland
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]
PostPosted: Mon Jan 17, 2011 10:59 am 
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:
[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:
/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?


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 8012
Location: Bay Area
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]
PostPosted: Mon Jan 17, 2011 11:28 am 
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:
[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:
/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

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Kobold
 
Joined: Sun Jan 16, 2011 5:13 pm
Posts: 12
Location: Oulu, Finland
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]
PostPosted: Mon Jan 17, 2011 11:45 am 
Great, thanks! I will gladly test out the new version as it becomes available. :)


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 8012
Location: Bay Area
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]
PostPosted: Mon Jan 17, 2011 12:16 pm 
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.

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 8012
Location: Bay Area
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]
PostPosted: Mon Jan 17, 2011 12:45 pm 
Okay, try think for the arcTan macro:

Code:
[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.

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Kobold
 
Joined: Sun Jan 16, 2011 5:13 pm
Posts: 12
Location: Oulu, Finland
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]
PostPosted: Thu Jan 20, 2011 5:33 am 
*drooling over the forthcoming mathlib update* :D


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 8012
Location: Bay Area
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]
PostPosted: Thu Jan 20, 2011 9:35 am 
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.

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Great Wyrm
 
Joined: Tue Aug 23, 2011 10:41 am
Posts: 1134
Location: Cornwall, UK
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]
PostPosted: Sun Aug 19, 2012 11:18 am 
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:
[hselected getSelected()]
[
htargetX getTokenX(0selected)]
[
htargetY getTokenY(0selected)]
[
hinitiative getInitiativeToken()]
[
hinitiativeX getTokenX(0initiative)]
[
hinitiativeY getTokenY(0initiative)]
[
rxDiff targetX initiativeX
[
ryDiff - (targetY initiativeY)]
[
rtanAngle arcTan(xDiff/yDiff)]
[
rangle toDegrees(tanAngle))] 


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.

_________________
How to get around the two code nest limit in MapTool (and MOTE)


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 5 minutes)
Most users ever online was 243 on Sun Nov 04, 2012 6:14 am

Users browsing this forum: No registered users and 1 guest





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

Style based on Andreas08 by Andreas Viklund

Style by Elizabeth Shulman