Sine interpolation

Share your advanced PureBasic knowledge/code with the community.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Sine interpolation

Post by Trond »

An example of how to use Sin() to interpolate between two points. When the second point is used again with a third point, we can even make a curvy graph plotting.

Code: Select all


tX = 5
Dim Y(tX)

Y(0) = 100
Y(1) = 200
Y(2) = 30
Y(3) = 120
Y(4) = 300
Y(5) = 100

Procedure.d DegToRad(D.d)
  ProcedureReturn D * #PI/180
EndProcedure

Procedure.f Interpolate(A.f, B.f, cX.f)
  ; a  = 1st Number
  ; b  = 2nd Number
  ; cX = Relationship (0 - 1)
  ProcedureReturn b*cX + a*(1-cX)
EndProcedure

Procedure.f InterpolateSine(A.f, B.f, cX.f)
  ; a = 1st Number
  ; b = 2nd Number
  ; v = Relationship (0 - 1)

  ; "Stretch" cx to the correct range for Sin()
  cX = cX * (180*#PI/180) + DegToRad(90)
  ; Pass cX to Sin() and compress the result to the
  ; correct range for Interpolate()
  cX = 1 - ( Sin(cX) + 1 ) / 2
  ProcedureReturn Interpolate(A, B, cX)
EndProcedure

Global pX, pY
Procedure LinePlotStart(X, Y)
  pX = X
  pY = Y
EndProcedure

Procedure LinePlot(X, Y, Col)
  LineXY(pX, pY, X, Y, Col)
  pX = X
  pY = Y
EndProcedure

CreateImage(0, 512, 420)
StartDrawing(ImageOutput(0))
  Box(0, 0, 512, 420, #White)
  For X = 0 To tX
    Circle(X*100, Y(X), 2, #Blue)
  Next
  
  LinePlotStart(0, Y(0))
  
  For X = 0 To tX-1
    For cX = 0 To 100
      
      ; Linear interpolation
      Y = Interpolate(Y(X), Y(X+1), cX/100)
      Plot(X*100+cX, Y, RGB(192, 192, 192))
      
      ; Sine interpolation with line plotting
      Y = InterpolateSine(Y(X), Y(X+1), cX/100)
      LinePlot(X*100+cX, Y, #Red)
    Next
  Next
  
StopDrawing()

OpenWindow(0, 0, 0, 512, 420, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
ImageGadget(0, 0, 0, 0, 0, ImageID(0))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post by flaith »

Thanks for the tip :D
“Fear is a reaction. Courage is a decision.” - WC
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

Nice!
Tusen takk Trond.
dige
Addict
Addict
Posts: 1411
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Great!
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

I like that idea. :wink:

EDIT: By the way, this is what must be applied to this problem asked by merendo:
http://www.purebasic.fr/english/viewtopic.php?p=224808
much better than my solution there :roll:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
idle
Always Here
Always Here
Posts: 5918
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

could be handy thanks
Post Reply