# RPTools.net

Discussion and Support

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

 Page 1 of 2 [ 22 posts ] Go to page 1, 2  Next
Author Message
 aliasmask   Deity Joined: Tue Nov 10, 2009 6:11 pmPosts: 8012Location: Bay Area
 Post subject: MathLib - SOHCAHTOA [Update 1/8/11]Posted: 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.
• factorial(start[,end,step]) - Does factors from start to end and allows skips with step parameter (defaults, end = 0, step = -1)
• 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 ""

_________________

Last edited by aliasmask on Sun Jan 16, 2011 8:20 pm, edited 3 times in total.
 aliasmask   Deity Joined: Tue Nov 10, 2009 6:11 pmPosts: 8012Location: Bay Area
 Post subject: Re: MathLib - SOACAHTOAPosted: Mon Jul 12, 2010 4:29 am
[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 11:19 am, edited 1 time in total.

 wolph42   Deity Joined: Fri Mar 20, 2009 4:40 amPosts: 9485Location: Netherlands
 Post subject: Re: MathLib - SOACAHTOAPosted: 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

 aliasmask   Deity Joined: Tue Nov 10, 2009 6:11 pmPosts: 8012Location: Bay Area
 Post subject: Re: MathLib - SOACAHTOAPosted: 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.

_________________

 aliasmask   Deity Joined: Tue Nov 10, 2009 6:11 pmPosts: 8012Location: Bay Area
 Post subject: Re: MathLib - SOACAHTOAPosted: 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.

_________________

 aliasmask   Deity Joined: Tue Nov 10, 2009 6:11 pmPosts: 8012Location: Bay Area
 Post subject: Re: MathLib - SOACAHTOAPosted: 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]

_________________

 aliasmask   Deity Joined: Tue Nov 10, 2009 6:11 pmPosts: 8012Location: Bay Area
 Post subject: Re: MathLib - SOACAHTOA [Update 1/8/11]Posted: 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.

_________________

 akuma   Kobold Joined: Sun Jan 16, 2011 5:13 pmPosts: 12Location: Oulu, Finland
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]Posted: Mon Jan 17, 2011 10:59 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.

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

 aliasmask   Deity Joined: Tue Nov 10, 2009 6:11 pmPosts: 8012Location: Bay Area
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]Posted: Mon Jan 17, 2011 11:28 am
akuma 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.

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

_________________

 akuma   Kobold Joined: Sun Jan 16, 2011 5:13 pmPosts: 12Location: Oulu, Finland
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]Posted: Mon Jan 17, 2011 11:45 am
Great, thanks! I will gladly test out the new version as it becomes available.

 aliasmask   Deity Joined: Tue Nov 10, 2009 6:11 pmPosts: 8012Location: Bay Area
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]Posted: 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.

_________________

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

Code:

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.

_________________

 akuma   Kobold Joined: Sun Jan 16, 2011 5:13 pmPosts: 12Location: Oulu, Finland
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]Posted: Thu Jan 20, 2011 5:33 am
*drooling over the forthcoming mathlib update*

 aliasmask   Deity Joined: Tue Nov 10, 2009 6:11 pmPosts: 8012Location: Bay Area
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]Posted: 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.

_________________

 Bone White   Great Wyrm Joined: Tue Aug 23, 2011 10:41 amPosts: 1134Location: Cornwall, UK
 Post subject: Re: MathLib - SOHCAHTOA [Update 1/8/11]Posted: 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)

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 2 [ 22 posts ] Go to page 1, 2  Next

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

 Search for: Jump to: Select a forum ------------------ General    Announcements    Ideas for New Applications    General Discussion    Developer Notes       Java Programming Info    User Creations       Requests for HELLLLP!       Tilesets, Token Libraries, and Complete Maps          Token Libraries          Tilesets/Graphics for Map-making          Complete Maps       Campaign Frameworks          D&D 5e Frameworks          D&D 4e Frameworks          [D&D3.5 + Pathfinder] Campaign Macros          Shadowrun 4e Frameworks          Savage Worlds Frameworks       Published Module Support       Tutorials          Graphics, Images, Tokens, Maps          Drop-In Macros and Scripts          Campaign Frameworks       Drop-In Macro Resources    Links & External Resources    Looking For Group       "Living" Campaigns       Dungeons and Dragons       Savage Worlds       WoD       Wargaming    Documentation Requests/Discussion    Session Report-Back Tools    MapTool       How To       Macros       Feature Requests          Resolved       Bug Reports          Resolved       WIP - User FAQ       Testing    MapTool 1.4    TokenTool    DiceTool    InitiativeTool    CharacterTool       Support: d20 D&D       Support: D&D 4e       Support: Hero System       Support: Savage Worlds       Support: GURPS Other    RPTools Sightings    Running Campaigns       Thiera's College of Adventurers       Age of Might       Scales of War       Renwold       Dead Men Walking       Dark Sun - 4th Edition       Warhammer Fantasy Roleplay       Return of Uchly Namen       Insert Campaign Here       Antium       The Mystic Moon       World of Lagal       GangerRun       FB Scales of War       Sellswords of Eberron          Archive       Orbs of Chaos       World of Omegus       Keep on the Shadowfell - 4E       Rise of the Runelords       Aurith In The Age Of Steam       Widdershins       Nos's Pathfinder Campaign       Falcons Hollow       Verwirrte Welten       With it or on it       Starter Set - Jeff

### 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