Snippet A nice math function...

Discussion in 'Systems and Snippets' started by AceHart, Oct 8, 2008.

Well, "nice"... no idea what to call this.
or if there even is any kind of need...

Anyway, ever wanted something like
do 5 damage on level 1,
do 8 damage on level 2,
do 12 damage on level 3,
do 17 damage on level 4,
...

Only to sit there and try to come up with a formula instead of "if level == 1 then ..."?

So,
5, 8, 12, 17, 23
take the differences from one to the next:
3, 4, 5, 6
take the differences from one to the next:
1, 1, 1
The line is constant!
Given it was the second, the most common case,
our damage can be calculated with this formula:
damage = a * level * level + b * level + c

But... what are a, b and c?

That's where this function comes to the rescue:

JASS:
``````function solve takes real y1,real y2,real y3 returns nothing
local real a = 0.5 * (y3 - 2 * y2 + y1)
local real b = y2 - y1 - 3 * a
local real c = y1 - a - b
local string s1 = "+"
local string s2 = "+"
if b < 0 then
set s1 = "-"
set b = -b
endif
if c < 0 then
set s2 = "-"
set c = -c
endif
call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, R2S(a) + " * level * level " + s1 + " " + R2S(b) + " * level " + s2 + " " + R2S(c))
endfunction``````

Examples:
call solve(5, 8, 12) (always call with the expected values on levels 1 to 3)
Which will say
0.5 * level * level + 1.5 * level + 3

Or,
call solve(10, 18, 34):
4 * level * level - 4 * level + 10

Of course, this could be improved by telling it to simplify
4 * level * level - 4 * level + 10
to
4 * level * (level - 1) + 10
but, then again, that would be the famous "exercise for the interested reader"

Just because it's fun, here's an online version that runs in your browser:
Formula finder
• Like x 1
2. FlareStops copies me!Staff Member

Pretty cool idea (even though having to test the map just to get the values would be annoying )
3. SerraAvengerCuz I can

Exactly. Doing this in JASS is like trying to teach a squirrel fly.

Also, wouldn't this require to have the graph of the damage increase have a certain shape?
Don't make it too easy for yourself. That was just "randomly" true ( actually I believe you designed the sequence like that ).
You can't draw a 3rd degree polynomial through ALL sets of points.

So, why not ( Damien will hate me for this :evilgrin: ) do an executable of it with something easy like Python2Exe ^.-
Python comes here quite handy; You don't need no call for MAXXXXEFFICIENCYY!!!!, so you also needn't call for Perl ( oO ).
Anyway, just choose a language that
+ knows regex ( for an input like a;b;c;d )
+ can handle higher degree polynomials
+ has a lightweight + nice GUI toolkit ( like PythonQT for python )
+ dynamically allocates arrays

And code a similiar function there. Perhaps I'll do it some sleepless weekend
Best regards, Davey
• Like x 1
4. DrEvilFCRI Associate!

If i was to use this system ( which i probably wont )

How would i get [ real y1 , real y2 and real y3 ] ?

JASS:
``function solve takes real y1,real y2,real y3 returns nothing``

Sorry to ask im just trying to get used to using JASS functions taking stuff in [ Somehow , i dont know ]

Thanks for answering {[( In future post's )]}
5. FlareStops copies me!Staff Member

Let's say you wanted to get a formula for
Level 1: 5
Level 2: 6
Level 3: 8

JASS:
``call solve (5, 6, 8)``

y1/2/3 are the particular values you have for the associated level of the ability
• Like x 1
6. DrEvilFCRI Associate!

So
Code:
`call solve (5, 6, 8)`
Would call the function solve giving ?
real y1 = 5
real y2 = 6
real y3 = 8

EDIT : call solve ( 5, 6, 8) { Would be a gui custom script ? Calling frm a spell cast maybe ? }
7. FlareStops copies me!Staff Member

Ye (assuming I understand what you're asking) - you want to know how/where you get the values you pass to the function (y1, y2, y3), right?
8. DrEvilFCRI Associate!

Nice system you got there Ace :thup:

Yes , Thanks for helping me understand Flare .

Offtopic

And for other functions i could use like getting x,y coordinates and returning for a spell ?
9. DarthfettSuper ModStaff Member

Almost pointless post?

Basic Derivatives, or algebra, depending on how you look at it :

ax² + bx + c is the basic quadratic equation.

You basically wrote a guide on how to get a formula when you already know the intervals. Basically, it's finding the derivative of an equation you don't know.

The reverse derivative (being able to find the output from any level):

X is the level.
Y is the damage amount.
Y' is the difference of damage between one level and the next.
Y'' is how much Y' changes every level.

Y'' = c __ c is a constant number. Since Acehart solved the difference in difference in damage (Y'') to being 1, we can just plug in 1 for c.

Y' = cx + d __ d is an unknown constant. In this case, you can substitute 3, 4, 5, 6 for difference in damage (Y'), making d become 2.

Y = c x²/2 + dx + e __ e is an unknown constant. in this case, we can plug in 5, 8, 12, 17, 23 for damage (Y) and solve for e.

c = 1 :
Y'' = 1
Y' = x + d
Y = X²/2 + dx + e

(1) + d = 3

c = 1, d = 2:

Y'' = 1
Y' = x + 2
Y = X²/2 + 2x + e

(1)²/2 + 2(1) + e = 5

c = 1, d = 2, e = 2.5

Y'' = 1
Y' = x + 2
Y = X²/2 + 2x + 2.5

Now you can calculate the amount of damage dealt to any level using Y = X²/2 + 2x + 3

*Work in Progress Post, the formula is not quite working out as I intended. Sorry to post it incomplete, but I have a class to be at. :S*

See? Calculus is useful!

• Like x 1

• Like x 1
12. Uberplayer-

(The title of the thread fights against all those title writing rules.)

Why did you let this drop? It's awesome!

(you should return to the chat, so I could bomb you at my random questions)

Drop?
I wrote "The Amazing Formula Finder by AceHart"(tm) and put it online... what more can you ask for?

Formula Finder

Wow, that formula finder is useful :thup:
15. SiretuStarcraft 2 Editor ModeratorStaff Member

You just made my homework LOTS easier. Thanks!
16. FurbyCurrent occupation: News poster

Just an idea

What about some function like this:

Code:
```...
set value = GetValue(a,b,c,level)
...```

which takes a,b,c like in your first post and returns value for level taken in function?

I think it will be more helpfull like this because if i want to find formula I will go to some page with the Formula Finder. I think no one will use it by map if he know where the formula finder is.
17. LarcenistREP: Respect, Envy, Prosperity?

Another interesting thing would be to know the equations for the x^3 and x^4 coefficients in the formula finder (since I'm too lazy to figure them out myself).