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.
  • 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 The Helper:
    I think we need to add something to the bottom of the front page that shows the Headline News forum that has a link to go to the News Forum Index so people can see there is more news. Do you guys see what I am saying, lets say you read all the articles on the front page and you get to the end and it just ends, no kind of link for MOAR!
  • The Helper The Helper:
    Happy Wednesday!
    +1
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    Sticking with the desserts for now the latest recipe is Fried Apple Pies - https://www.thehelper.net/threads/recipe-fried-apple-pies.194297/
  • The Helper The Helper:
    Finally finding about some of the bots that are flooding the users online - bytespider apparently is a huge offender here - ignores robots.txt and comes in from a ton of different IPs
  • Monovertex Monovertex:
    @The Helper I'm really not seeing the "Signature" link in the sidebar on that page. Here's a screenshot:
  • The Helper The Helper:
    I have reported it - I was wondering why nobody I have given sigs to over the last few years have used them
  • The Helper The Helper:
    Ghan has said he has fixed this. Monovertex please confirm this fix. This was only a problem with people that had signatures in the upper levels like not the special members but the respected members.
  • The Helper The Helper:
    Here is a better place to manage this stuff https://www.thehelper.net/account/account-details which I think should be way more visible
  • The Helper The Helper:
    I am hoping that online user count drop is finally that TikTok bot banned
  • Ghan Ghan:
    I added the filter last night.
  • The Helper The Helper:
    They are still there

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top