Snippet Progression

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:
///////////////////////////////////////////////////////////
//
//  Progression Object      
//      by kingking
//
//  Progression Object provides 2 types of progression.
//  ~ Arithmetric
//  ~ Geometric
//
//  What is progression?
//  <a href="http://en.wikipedia.org/wiki/Arithmetic_progression" target="_blank" class="link link--external" rel="nofollow ugc noopener">http://en.wikipedia.org/wiki/Arithmetic_progression</a>
//  <a href="http://en.wikipedia.org/wiki/Geometric_Progression" target="_blank" class="link link--external" rel="nofollow ugc noopener">http://en.wikipedia.org/wiki/Geometric_Progression</a>
//
//  Progression is useful in Jump systems, knockback systems,
//  projectile systems for calculating distance travelled, 
//  acceleration, height....
//
//  Instead of storing the progression data by yourself,
//  the library provides storage solution for you.
//
//  PO_Arithmetric.create() -&gt; return a new arithmetric progression
//  &lt;vars&gt;.start -&gt; set the starting number.
//  &lt;vars&gt;.difference -&gt; set the difference.
//  &lt;vars&gt;.getNumber(n) -&gt; returns T(n)
//  &lt;vars&gt;.getSum(n) -&gt; returns S(n)
//  &lt;vars&gt;.destroy() -&gt; destroy the progression.
//
//  PO_Geometric.create() -&gt; return a new geometric progression
//  &lt;vars&gt;.start -&gt; set the starting number.
//  &lt;vars&gt;.ratio -&gt; set the ratio
//  &lt;vars&gt;.getNumber(n) -&gt; returns T(n)
//  &lt;vars&gt;.getSum(n) -&gt; return S(n)
//  &lt;vars&gt;.getInfinite() -&gt; return S(infinite)
//  &lt;vars&gt;.destroy() -&gt; destroy the progression.
//
//  GetDifferenceByNumber(starting number, n, T(n) )
//  GetDifferenceBySum(starting number, n, S(n) )
//  GetStartingNumberByNumber(difference, n, T(n) )
//  GetStartingNumberBySum(difference, n, S(n) )
//
//  GetRatioByNumber(starting number, n, T(n) )
//  GetStartingNumberGeoByNumber(ratio, n, T(n) )
//  GetStartingNumberGeoBySum(ratio, n, S(n) )
//
//  Log10(number)
//
//  Requires : Jasshelper 0.A.2.A of later
/////////////////////////////////////////////////////
library PO

    public struct Arithmetric
        real start
        real difference
        
        method getNumber takes integer index returns real
            //T(n) = a + (n-1)d
            return .start - (index - 1.) * .difference
        endmethod
        
        method getSum takes integer index returns real
            //        n 
            //S(n) = --- ( 2a + (n-1)d )
            //        2 
            return (index / 2.) * ((2. * .start) + (index - 1.) * .difference)
        endmethod  
        
        method onDestroy takes nothing returns nothing
            set .start = 0.
            set .different = 0.
        endmethod
    endstruct
    
    public struct Geometric
        real start
        real ratio
        
        method getNumber takes integer index returns real
            //T(n) = ar^(n-1)
            return .start * Pow(.ratio,index - 1.)
        endmethod
        
        method getSum takes integer index returns real
            if ratio &lt; 1. then
                //         a (1 - r^n)
                //S(n) = ------------
                //            1 - r
                return .start * (1. - Pow(.ratio,index)) / (1. - .ratio)
            endif
            //          a (r^n - 1)
            //  S(n) = -------------
            //              r - 1
            return .start * (Pow(.ratio,index) - 1.) / (.ratio - 1.)
        endmethod
        
        method getInfinite takes nothing returns real
            //                   a
            // S(infinite) =  -------
            //                 1 - r
            return .start / (1. - .ratio)
        endmethod
        
        method onDestroy takes nothing returns nothing
            set .start = 0.
            set .ratio = 0.
        endmethod
    endstruct
    
    function GetDifferenceByNumber takes real start, integer index, real number returns real
        // T(n) = a + (n - 1)d
        // T(n) - a = (n - 1)d
        
        //  T(n) - a
        // ---------- = d
        //   n - 1
        return (number - start) / (index - 1)
    endfunction
    
    function GetStartingNumberByNumber takes real difference, integer index, real number returns real
        //      T(n) - (n - 1)d = a
        return number - ((index - 1) * difference)
    endfunction
    
    function GetDifferenceBySum takes real start, integer index, real total returns real
        //         n 
        // S(n) = --- ( 2a + (n-1)d )
        //         2 
            
        //                n
        // S(n) = n(a) + --- (n-1)d
        //                2
            
        //                n
        // S(n) - n(a) = --- (n-1)d
        //                2
            
        //       S(n) - n(a)
        // d =  -------------
        //      (n/2) * (n-1)
        return (total - (index * start)) / ((index / 2) * (index - 1))
    endfunction
    
    function GetStartingNumberBySum takes real difference, integer index, real total returns real
        //         n 
        // S(n) = --- ( 2a + (n-1)d )
        //         2 
            
        //                n
        // S(n) = n(a) + --- (n-1)d
        //                2
            
        //         n
        // S(n) - --- (n-1)d = n(a)
        //         2
            
        //        S(n) - (n / 2) * n-1(d)
        // a =  --------------------------
        //                n
        return (total - ((index / 2) * ((index - 1) * difference))) / index
    endfunction
    
    function Log10 takes real a returns real //Thanks for BlinkBoy for making this. =D
        local real sum = 0.0
        local real e = 1.284025417
        loop
        exitwhen a &lt; bj_E
            set a = a/bj_E
            set sum = sum + 1.
        endloop
        loop
        exitwhen a &lt; e
            set a = a/e
            set sum = sum + .25
        endloop
        return sum + (a-1.)*(1. + 8./(1.+ a) + 1/a)/6.
    endfunction
    
    function GetStartingNumberGeoByNumber takes real ratio, integer index, real number returns real
        //    n-1
        // ar       = T(n)
        
        //        T(n)
        //  a = -------
        //       r^n-1
        return number / Pow(ratio,index-1)
    endfunction
    
    function GetStartingNumberGeoBySum takes real ratio, integer index, real total returns real
        //        a( 1 - r^n)
        // S(n) =  ---------
        //           1 - r
            
        //  S(n) * (1 - r)
        // --------------- = a
        //      1 - r^n
        if ratio &lt; 1 then
            return (total * (1 - ratio)) / (1 - Pow(ratio,index))
        endif
        //        a( r^n - 1)
        // S(n) =  ---------
        //           r - 1
            
        //  S(n) * (r - 1)
        // --------------- = a
        //     r^n - 1
        return (total * (ratio - 1)) / (Pow(ratio,index) - 1)
    endfunction
    
    function GetRatioByNumber takes real start, integer index, real number returns real
        //     n-1
        //  ar     = T(n)
        
        //    n-1    T(n)
        //  r     = ------
        //            a
        
        //                 (  T(n) )
        //  (n-1)lg r = lg ( ----- )
        //                 (   a   )
        
        //            (  T(n) )
        //  lg r = lg ( ----- ) / (n - 1)
        //            (   a   )
        //
        //            (  T(n) )     
        //         lg ( ----- ) / (n - 1)
        //            (   a   )
        //    r = 10^
        return Pow(10,Log10(number / start) / (index-1))
    endfunction
endlibrary


Some practical usage for slide system.

Deceleration = 20.
Starting Speed = 300.
Slide Tick = 20

Total Distance travelled = ?

JASS:
local PO_Arithmetric data = PO_Arithmetric.create()
local real dis
set data.start = 300.
set data.difference = -20. //for deceleration
set dis = data.getSum( 20) //total distance travelled
 

uberfoop

~=Admiral Stukov=~
Reaction score
177
JASS:

        method getSum takes integer index returns real
            if ratio &lt; 1. then
                //        1 - r^n
                //S(n) = ---------
                //         1 - r
                return start * (1. - Pow(ratio,index)) / (1. - ratio)
            endif
            //          r^n - 1
            //  S(n) = ----------
            //           r - 1
            return start * (Pow(ratio,index) - 1.) / (ratio - 1.)
        endmethod

Maybe the lack of math I've done in the last several weeks has caused some minor detail to go way over my head, but last I checked, (a-b)/(c-d)=(b-a)/(d-c). So both formulas you're using are the exact same thing in (barely) different form.

Additionally, the english used in the snippet is bad. Specifically, you're using 'different' every single time you should be using 'difference', referring to the difference between terms in an arithmetic sequence. Ex:
JASS:

GetDifferentBySum
 

Nestharus

o-o
Reaction score
84
Honestly, why I do find this interesting, I don't see any practical use for this. Yes, it does work very interesting in a generic fashion over a few common problems, but it doesn't do it as well as a standard system would do it. It doesn't do it near as well ><. Using this for sliding or projectiles movements or w/e would not be as good as just like [ljass]u.x+=20; SetUnitX(u) = u.x[/ljass]

So personally I see no practical use for it because other methods are far better ;o.

Again, I do find it interesting and it's fun to read all the math =), but interesting is it : (.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
For projectile system.

I want :
Accelerating projectile.
Max Range = 700.
Time to reach maxRange = 1.
Tick = 1 * 32 Ticks(Ex. For T32)
StartingSpeed = 0

We have starting and total now. So the acceleration will be the difference.

How do I get the acceleration?
[ljass]GetDifferenceBySum(0.,32,700.) //This is our acceleration[/ljass]

Store the value.

Then in each periodic call back.
set speed = speed + acceleration
call SetUnitX(missle,x + speed * Cos(angle))
//----

The missle will accelerate in this way.
 

saw792

Is known to say things. That is all.
Reaction score
280
JASS:
local PO_Geometric d = PO_Geometric.create()
local real r = 0
set d.start = 4
set d.ratio = 1
set r = d.getSum(10)
//Divide by zero!


EDIT: New method:
JASS:
method getSum takes integer index returns real
            if ratio != 1. then
                return start * (1. - Pow(ratio,index)) / (1. - ratio)
            endif
            return start
endmethod
 

Romek

Super Moderator
Reaction score
963
The formulae could prove somewhat useful, though I get the feeling that this is another one of your "Let's just submit anything, for the sake of submitting something" resources.

Formulae like that should simply be inlined into code. A snippet like this is useless.

GY'd.
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
We should have a tutorial with all these important formulae?
 

Nestharus

o-o
Reaction score
84
I wouldn't call them important because you don't need progressive formulas as you do calculations based on where you are at the moment, not where you will be. These formulas are slower than what you'd normally do, hence useless, which is what I sort of stated before ^_^. Nobody would ever do this, lol. Other methods are easier and faster.

This is why I labeled this interesting but useless ;p.


Amen to that.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top