package Interpolation import Wurstunit // Credits to BlinkyBoy /**Look at linear*/ public function linear(vec2 start, vec2 stop, real p) returns vec2 return vec2(linear(start.x, stop.x, p), linear(start.y, stop.y, p)) public function bezier_3(vec2 start, vec2 mid, vec2 stop, real p) returns vec2 return vec2(bezier_3(start.x, mid.x, stop.x, p), bezier_3(start.y, mid.y, stop.y, p)) /** Linear Interpolation Sample Usage: AddSpecialEffect(TEST_SFX,linear(P1X,P2X,p),linear(P1Y,P2Y,p)) */ public function linear( real start, real stop, real p ) returns real return start + (stop-start)*p /** 3-Point-Bezier Interpolation Sample Usage: AddSpecialEffect(TEST_SFX,bezier_3(P1X,P3X,P2X,p),bezier_3(P1Y,P3Y,P2Y,p)) */ public function bezier_3( real start, real mid, real stop, real p ) returns real return start + 2.*(mid-start)*p + (stop -2.*mid + start)*p*p /** This function returns the derivate of the 3-Point-Bezier curve on certain point. (d(bezier_3())/dt) */ public function derivBezier_3( real start, real mid, real stop, real p ) returns real return 2.*(mid-start) + 2.*(stop-2.*mid+start)*p /** 4-Point-Bezier Interpolation Sample Usage: AddSpecialEffect(TEST_SFX,bezier_4(P1X,P3X,P4X,P2X,t),bezier_4(P1Y,P3Y,P4Y,P2Y,t)) */ function bezier_4( real start, real mid1, real mid2, real stop, real p ) returns real real m = p*p return start + 3.*p*(mid1 - start) + 3.*m*(mid2 - 2.*mid1 + start) + m*p*(3.*(mid1-mid2) + stop - start) /** This function returns the derivate of the 4-Point-Bezier curve on certain point. (d(bezier_4())/dt) */ public function derivBezier_4( real start, real mid1, real mid2, real stop, real p ) returns real return 3.*(mid1-start) + 6.*(mid2-2.*mid1+start)*p + 3.*(3.*(mid1-mid2) + stop -start)*p*p //You can use Bezier 3 to graphic perfect parabolas //Bezier_4(, , , ) /** Hermite Interpolation */ public function hermite( real start, real stop, real tangent1, real tangent2, real s ) returns real real s2 = s*s real h1 = 8.0*s*s2 - 9.0*s2 real h2 = h1*(-1) real h3 = s2*(s - 2.0) + s real h4 = s2*(s - 1.0) h1 = h1 + 1 return h1*start + h2*stop + h3*tangent1 + h4*tangent2 // TCB functions for finding the tangents among 3 points // Most modellers may know TCB from 3dsmax, in which they are used to make rotations // look very smooth. // p = tension, c = continuity, b = bias // tension is how sharply the curve makes turns. // continuity specifies the rate of change between speed and direction. // bias specifies the direction of the curve. // sidenote: specify p1 as 0, when working with 2 points. /** Returns the intan of p2 */ public function inTan( real p1, real p2, real p3, real p, real c, real b ) returns real return (1.0-p)*(1.0-c)*(1.0+b)*(p2-p1)/2. + (1.0-p)*(1.0+c)*(1.0-b)*(p3-p2)/2. /** Returns the outtan of p2 */ public function outTan( real p1, real p2, real p3, real p, real c, real b ) returns real return (1.0-p)*(1.0+c)*(1.0+b)*(p2-p1)/2. + (1.0-p)*(1.0-c)*(1.0-b)*(p3-p2)/2. public function axiAngle( real dy, real dx ) returns real return Atan2(dy,dx) public function getCardTan( real p1, real p3 ) returns real return 0.5*(p3-p1) @test function linearVecTest() let v = linear(vec2(3,4), vec2(6,2), 0.5) v.x.assertEquals(4.5, .001) v.y.assertEquals(3, .001)