January 26, 2021, 12:29:25 PM

### Author Topic: [bb] Interpolation by Klaas [ 1+ years ago ]  (Read 741 times)

#### BlitzBot

• Jr. Member
•  • Posts: 1 ##### [bb] Interpolation by Klaas [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Interpolation
Author : Klaas
Posted : 1+ years ago

Description : nothing new but all in one code to see the difference between the interpolation methodes.

Took the Framework from Vertex's Cosin Interpolation and added Cubic and Hermite Interpolation.
THX to Vertex

Take a closer look at the tension and bias control for hermite interpolation ... pretty cool

remember to use this function in 3d space just do the interpolation to z
like this (linear):
Y = Cosine_Interpolate(y0,y1,MU#)
X = Cosine_Interpolate(x0,x1,MU#)
Z = Cosine_Interpolate(z0,z1,MU#)

Cubic and Hermite Interpolation do need some extra information to do the interpolation. Both methodes need
4 points to make the curves smooth. PLay around with the controlpoints and you see why.

Code :
Code: BlitzBasic
1. ; Set the screensize
2. Graphics 640,480,32,2
3. SetBuffer BackBuffer()
4.
5. ; Dimension the pointarray and set startvalues
6. Dim DataPoint(6,1)
7. DataPoint(0,0) = 50 : DataPoint(0,1) = 090
8. DataPoint(1,0) = 150 : DataPoint(1,1) = 160
9. DataPoint(2,0) = 250 : DataPoint(2,1) = 300
10. DataPoint(3,0) = 350 : DataPoint(3,1) = 210
11. DataPoint(4,0) = 450 : DataPoint(4,1) = 100
12. DataPoint(5,0) = 550 : DataPoint(5,1) = 300
13.
14. ; Set the clearcolor to red, and select point 0
15. ClsColor 0,0,0 : Global Selected = 0
16. Global linear_draw,cosin_draw,cubic_draw,hermite_draw,tension#,bias#
17. ; Mainloop
18. While Not KeyDown(1)
19.    Cls ; Clear screen
20.
21.    ; Userinput (Use [Q] and [W] to select a point,
22.    ; and Arrowkeys to position the selected point)
23.    If KeyHit(16) And Selected > 0 Then Selected = Selected - 1
24.    If KeyHit(17) And Selected < 5 Then Selected = Selected + 1
25.
26.    If KeyHit(30) And tension > -1 Then tension = tension - 0.1
27.    If KeyHit(31) And tension < 1 Then tension = tension + 0.1
28.
29.    If KeyHit(44) And bias > -1 Then bias = bias - 0.1
30.    If KeyHit(45) And bias < 1 Then bias = bias + 0.1
31.
32.    If KeyDown(203) Then DataPoint(Selected,0) = DataPoint(Selected,0) - 1
33.    If KeyDown(205) Then DataPoint(Selected,0) = DataPoint(Selected,0) + 1
34.    If KeyDown(200) Then DataPoint(Selected,1) = DataPoint(Selected,1) - 1
35.    If KeyDown(208) Then DataPoint(Selected,1) = DataPoint(Selected,1) + 1
36.
37.         If KeyHit(59) Then linear_draw = Not linear_draw
38.         If KeyHit(60) Then cosin_draw = Not cosin_draw
39.         If KeyHit(61) Then cubic_draw = Not cubic_draw
40.         If KeyHit(62) Then hermite_draw = Not hermite_draw
41.    DrawLine() ; Draw the interpolated line between point 0 and 4
42.
43.         Color 250,250,250
44.         Text 10,10,"F1 - Linear Interpolation"
45.         Text 10,25,"F2 - Cosine Interpolation"
46.         Text 10,40,"F3 - Cubic Interpolation"
47.         Text 10,55,"F4 - Hermite Interpolation"
48.
49.         Text 300,10,"Tension(+A,-S) - "+tension
50.         Text 300,25,"Bias(+Z,-X) - "+Bias
51.
52.         Text 10,460,"use W,Q and Cursor to steer the points"
53.    Flip ; Flip backbuffer to frontbuffer
54. Wend
55.
56. End ; End of programm
57.
58. Function DrawLine()
59.    ; Startpoint for the first Line
60.    X = DataPoint(0,0) : Y = DataPoint(0,1) :
61.
62.    ; Draw the interpolated line between point 1 and 4
63.         If cosin_draw
64.                 f = False
65.                 Color 255,50,50
66.                 For I = 1 To 5
67.                         MX = (DataPoint(I - 1,0) - DataPoint(I,0)) * (-1)
68.                         For MU# = 0 To 1.0 Step 0.08
69.                                 OX = X : OY = Y
70.                                 Y = Cosine_Interpolate(DataPoint(I - 1,1),DataPoint(I,1),MU#)
71.                                 X = Cosine_Interpolate(DataPoint(I - 1,0),DataPoint(I,0),MU#)
72.                                 If Not f
73.                                         ox = x
74.                                         oy = y
75.                                         f = True
76.                                 EndIf
77.                                 Line OX,OY,X,Y
78.                         Next
79.                 Next
80.         EndIf
81.         If linear_draw
82.                 f = False
83.                 Color 50,255,50
84.                 For I = 1 To 5
85.                         MX = (DataPoint(I - 1,0) - DataPoint(I,0)) * (-1)
86.                         For MU# = 0 To 1.0 Step 0.08
87.                                 OX = X : OY = Y
88.                                 Y = Linear_Interpolate(DataPoint(I - 1,1),DataPoint(I,1),MU#)
89.                                 X = Linear_Interpolate(DataPoint(I - 1,0),DataPoint(I,0),MU#)
90.                                 If Not f
91.                                         ox = x
92.                                         oy = y
93.                                         f = True
94.                                 EndIf
95.                                 Line OX,OY,X,Y
96.                         Next
97.                 Next
98.         EndIf
99.         If cubic_draw
100.                 f = False
101.                 Color 50,50,255
102.                 For I = 2 To 4
103.                         MX = (DataPoint(I - 1,0) - DataPoint(I,0)) * (-1)
104.                         For MU# = 0 To 1.0 Step 0.08
105.                                 OX = X : OY = Y
106.                                 Y = Cubic_Interpolate(DataPoint(I - 2,1),DataPoint(I-1,1),DataPoint(I,1),DataPoint(I+1,1),MU#)
107.                                 X = Cubic_Interpolate(DataPoint(I - 2,0),DataPoint(I-1,0),DataPoint(I,0),DataPoint(I+1,0),MU#)
108.                                 If Not f
109.                                         ox = x
110.                                         oy = y
111.                                         f = True
112.                                 EndIf
113.                                 Line OX,OY,X,Y
114.                         Next
115.                 Next
116.         EndIf
117.         If hermite_draw
118.                 f = False
119.                 Color 255,50,255
120.                 For I = 2 To 4
121.                         MX = (DataPoint(I - 1,0) - DataPoint(I,0)) * (-1)
122.                         For MU# = 0 To 1.0 Step 0.08
123.                                 OX = X : OY = Y
124.                                 Y = Hermite_Interpolate(DataPoint(I - 2,1),DataPoint(I-1,1),DataPoint(I,1),DataPoint(I+1,1),MU#,tension,bias)
125.                                 X = Hermite_Interpolate(DataPoint(I - 2,0),DataPoint(I-1,0),DataPoint(I,0),DataPoint(I+1,0),MU#,tension,bias)
126.                                 If Not f
127.                                         ox = x
128.                                         oy = y
129.                                         f = True
130.                                 EndIf
131.                                 Line OX,OY,X,Y
132.                         Next
133.                 Next
134.         EndIf
135.    ; Dra the anchor points
136.    Color 255,0,0
137.    For I = 0 To 5
138.       If Selected = I Then
139.          Color 0,0,255
140.       Else
141.          Color 255,0,0
142.       EndIf
143.       Oval DataPoint(I,0) - 2,DataPoint(I,1) - 2,5,5,1
144.    Next
145. End Function
146.
147. Function Cubic_Interpolate(v0#, v1#, v2#, v3#, x#)
148.         P# = (v3-v2) - (v0-v1)
149.         Q# = (v0-v1) - P
150.         R# = v2 - v0
151.         S# = v1
152.         Return P * x^3 + Q * x^2 + R * x + S
153. End Function
154.
155. ;Function For cosineinterpolated Line
156. Function Cosine_Interpolate(Y1#,Y2#,MU#)
157.         Local   MU2#
158.         MU2 = (1.0 - Cos(MU * 180))/2.0;
159.         Return (Y1 * (1.0 - MU2) + Y2 * MU2)
160. End Function
161.
162. ;Function For normal Line
163. Function Linear_Interpolate(Y1#,Y2#,MU#)
164.         Return Y1 * (1 - MU) + Y2 * MU
165. End Function
166.
167.
168. ;Tension: 1 is high, 0 normal, -1 is low
169. ;Bias: 0 is even,
170. ;positive is towards First segment,
171. ;negative towards the other
172.
173. Function Hermite_Interpolate(y0#,y1#,y2#,y3#,mu#,tension#,bias#)
174.         Local m0#,m1#,mu2#,mu3#
175.         Local a0#,a1#,a2#,a3#
176.
177.         mu2 = mu * mu
178.         mu3 = mu2 * mu
179.         m0  = (y1-y0)*(1+bias)*(1-tension)/2
180.         m0  = m0 + (y2-y1)*(1-bias)*(1-tension)/2
181.         m1  = (y2-y1)*(1+bias)*(1-tension)/2
182.         m1 = m1 + (y3-y2)*(1-bias)*(1-tension)/2
183.         a0 =  2*mu3 - 3*mu2 + 1
184.         a1 =    mu3 - 2*mu2 + mu
185.         a2 =    mu3 -   mu2
186.         a3 = -2*mu3 + 3*mu2
187.
188.         Return(a0*y1+a1*m0+a2*m1+a3*y2)
189. End Function

Bot Builder(Posted 1+ years ago)

Nice set of functions there. I optimized and made hermite better though:
Code: [Select]
`Function Hermite_Interpolate#(y0#,y1#,y2#,y3#,t#,tension#,bias#) t2# = t * t t3# = t2 * t tb# = (1+bias)*(1-tension)/2 Return (2*t3-3*t2+1)*y1+(((t3-2*t2+t)*(y1-y0+y2-y1))+(t3-t2)*(y2-y1+y3-y2))*tb#+(3*t2-2*t3)*y2End Function`Yours didn't return a float, and I optimized it since I need it to run fast.

DJWoodgate(Posted 1+ years ago)

Seems to me the cosine interpolation is wrong.  I was expecting to see something similar to changing the line   Return (Y1 * (1 - MU2) + Y2 * MU2)to   Return (Y1 * (1 - MU2) + Y2 * MU)although that is not right either Something with a bit more curve anyway. Well Ok the function is OK. Maybe the trick is to mix cosine interpolation with linear interpolation.         Y = Cosine_Interpolate(DataPoint(I - 1,1),DataPoint(I,1),MU#)              X = Linear_Interpolate(DataPoint(I - 1,0),DataPoint(I,0),MU#)Rats.  Yes. I just found Vertex's code.  <a href="http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=652" target="_blank">http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=652[/url]

Beaker(Posted 1+ years ago)

It makes more sense if you change line 79:Line OX,OY,X,Yto:Plot X,Y [/i]