November 25, 2020, 05:04:34 AM

Author Topic: [bmx] Interpolation by Pineapple [ 1+ years ago ]  (Read 616 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Interpolation by Pineapple [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Interpolation
Author : Pineapple
Posted : 1+ years ago

Description : Linear, cosine, cubic, and hermite interpolation functions

Code :
Code: BlitzMax
  1. '       --+-----------------------------------------------------------------------------------------+--
  2. '         |   This code was originally written by Sophie Kirschner (sophiek@pineapplemachine.com)   |  
  3. '         | It is released as public domain. Please don't interpret that as liberty to claim credit |  
  4. '         |   that isn't yours, or to sell this code when it could otherwise be obtained for free   |  
  5. '         |                because that would be a really shitty thing of you to do.                |
  6. '       --+-----------------------------------------------------------------------------------------+--
  7.  
  8. ' these functions were adapted from the interpolation algorithms described at
  9. ' http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
  10. ' http://www.blitzbasic.com/codearcs/codearcs.php?code=781
  11.  
  12. SuperStrict
  13.  
  14. Import brl.math ' for interpolate_cosine
  15.  
  16. ' example code
  17. Rem
  18. Graphics 512,512
  19.  
  20. Local pix_linear:TPixmap=CreatePixmap(128,256,PF_RGB888)
  21. Local pix_cosine:TPixmap=pix_linear.copy()
  22. Local pix_cubic:TPixmap=pix_linear.copy()
  23. Local pix_hermite:TPixmap=pix_linear.copy()
  24.  
  25. Local begin_r%=0
  26. Local begin_g%=10
  27. Local begin_b%=32
  28.  
  29. Local end_r%=255
  30. Local end_g%=70
  31. Local end_b%=4
  32.  
  33. Local before_r%=0
  34. Local before_g%=0
  35. Local before_b%=0
  36.  
  37. Local after_r%=255
  38. Local after_g%=255
  39. Local after_b%=255
  40.  
  41. For Local y%=0 Until pix_linear.height
  42.         ' linear
  43.         Local value!=y/Double(pix_linear.height)
  44.         Local red%=  interpolate_linear(begin_r,end_r,value)
  45.         Local green%=interpolate_linear(begin_g,end_g,value)
  46.         Local blue%= interpolate_linear(begin_b,end_b,value)
  47.         red=Min(255,Max(0,red));green=Min(255,Max(0,green));blue=Min(255,Max(0,blue))
  48.         Local rgb_linear%=red Shl 16 | green Shl 8 | blue
  49.         ' cosine
  50.         red%=  interpolate_cosine(begin_r,end_r,value)
  51.         green%=interpolate_cosine(begin_g,end_g,value)
  52.         blue%= interpolate_cosine(begin_b,end_b,value)
  53.         red=Min(255,Max(0,red));green=Min(255,Max(0,green));blue=Min(255,Max(0,blue))
  54.         Local rgb_cosine%=red Shl 16 | green Shl 8 | blue
  55.         ' cubic
  56.         red%=  interpolate_cubic(before_r,begin_r,end_r,after_r,value)
  57.         green%=interpolate_cubic(before_g,begin_g,end_g,after_g,value)
  58.         blue%= interpolate_cubic(before_b,begin_b,end_b,after_b,value)
  59.         red=Min(255,Max(0,red));green=Min(255,Max(0,green));blue=Min(255,Max(0,blue))
  60.         Local rgb_cubic%=red Shl 16 | green Shl 8 | blue
  61.         ' hermite
  62.         red%=  interpolate_hermite(before_r,begin_r,end_r,after_r,value,0.5,0.5)
  63.         green%=interpolate_hermite(before_g,begin_g,end_g,after_g,value,0.5,0.5)
  64.         blue%= interpolate_hermite(before_b,begin_b,end_b,after_b,value,0.5,0.5)
  65.         red=Min(255,Max(0,red));green=Min(255,Max(0,green));blue=Min(255,Max(0,blue))
  66.         Local rgb_hermite%=red Shl 16 | green Shl 8 | blue
  67.         For Local x%=0 Until pix_linear.width
  68.                 pix_linear.WritePixel(x,y,rgb_linear)
  69.                 pix_cosine.WritePixel(x,y,rgb_cosine)
  70.                 pix_cubic.WritePixel(x,y,rgb_cubic)
  71.                 pix_hermite.WritePixel(x,y,rgb_hermite)
  72.         Next
  73. Next
  74.  
  75. Local start_y%=0
  76. Local end_y%=0
  77. Local before_y%=0
  78. Local after_y%=0
  79. Local editpoint%=0
  80.  
  81. Repeat
  82.         Cls
  83.         DrawPixmap pix_linear,0,0
  84.         DrawPixmap pix_cosine,128,0
  85.         DrawPixmap pix_cubic,256,0
  86.         DrawPixmap pix_hermite,384,0
  87.         lines before_y,start_y,end_y,after_y,128,382,256
  88.         SetColor 255,255,255
  89.         DrawText "Linear",2,2
  90.         DrawText "Cosine",130,2
  91.         DrawText "Cubic",258,2
  92.         DrawText "Hermite",386,2
  93.         SetColor 128,128,128
  94.         DrawText "Linear",2,260
  95.         SetColor 255,0,0
  96.         DrawText "Cosine",2,280
  97.         SetColor 0,255,0
  98.         DrawText "Cubic",2,300
  99.         SetColor 255,255,0
  100.         DrawText "Hermite",2,320
  101.         Local modifier%=KeyDown(key_down)-KeyDown(key_up)
  102.         If KeyHit(key_left) Or KeyHit(key_right) Then editpoint=Not editpoint
  103.         If editpoint Then start_y:+modifier Else end_y:+modifier
  104.         start_y=Min(100,Max(-100,start_y))
  105.         end_y=Min(100,Max(-100,end_y))
  106.         Flip
  107. Until KeyDown(key_escape) Or AppTerminate()
  108.  
  109. Function lines(a%,b%,c%,d%,x%,y%,w%)
  110.         For Local i%=0 Until w
  111.                 Local value!=i/Double(w)
  112.                 SetColor 128,128,128
  113.                 Plot x+i,y+interpolate_linear(b,c,value)
  114.                 SetColor 255,0,0
  115.                 Plot x+i,y+interpolate_cosine(b,c,value)
  116.                 SetColor 0,255,0
  117.                 Plot x+i,y+interpolate_cubic(a,b,c,d,value)
  118.                 SetColor 255,255,0
  119.                 Plot x+i,y+interpolate_hermite(a,b,c,d,value,0.5,0.5)
  120.         Next
  121. End Function
  122.  
  123. EndRem
  124.  
  125. ' a --- b
  126. Function interpolate_linear!(a#,b#,x!)
  127.         Return a*(1-x)+b*x
  128. End Function
  129. ' a --- b
  130. Function interpolate_cosine!(a#,b#,x!)
  131.         Local f!=(1-Cos(x*180))*.5
  132.         Return a*(1-f)+b*f
  133. End Function
  134. ' a     b --- c     d
  135. Function interpolate_cubic!(a#,b#,c#,d#,x!)
  136.         Local p#=(d-c)-(a-b)
  137.         Return p*x*x*x+((a-b)-p)*x*x+(c-a)*x+b
  138. End Function
  139. ' a     b --- c     d
  140. Function interpolate_hermite!(a#,b#,c#,d#,x!,tension#,bias#)
  141.         Local x2!=x*x
  142.         Local x3!=x*x2
  143.         Local xb!=(1+bias)*(1-tension)/2!
  144.         Return (2*x3-3*x2+1)*b+(((x3-2*x2+x)*(a+c))+(x3-x2)*(b+d))*xb+(3*x2-2*x3)*c
  145. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal