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
964
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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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