May 04, 2024, 06:25:35 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Cubic Spline for points

Started by ckoehn, February 22, 2010, 02:06:45 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ckoehn

February 22, 2010, 02:06:45 PM Last Edit: February 22, 2010, 06:41:26 PM by ckoehn
I don't know if this is the right place to put this, or if this is even needed.  It draws a line through points using a cubic spline.

My question.  Is it possible to define an array with a variable?  Something like h[n] in the example below, so your arrays in your sub can be any size?

Thanks,
Clint

'w - Window that the lines will be drawn in
'x - x[0]..x[n]
'y - y[0]..y[n]
'colr - line color

SUB Spline(WINDOW w, INT x BYREF, INT y BYREF, INT n BYVAL, UINT colr BYVAL)
DOUBLE h[100],f[100],u[100],v[100],z[100],a[100],b[100],c[100],d[100]   'change to max values
INT i,xx,yy

FOR i=0 TO n-1
h[i]=x[i+1]-x[i]
f[i]=(y[i+1]-y[i])/h[i]
NEXT i

u[1]=2*(h[0]+h[1])
v[1]=6*(f[1]-f[0])
FOR i=2 to (n-1)
u[i]=2*(h[i-1]+h[i])-(h[i-1]^2)/u[i-1]
v[i]=6*(f[i]-f[i-1])-(h[i-1]*v[i-1])/u[i-1]
NEXT i

z[n]=0
FOR i=(n-1) to 1 STEP -1
z[i]=(v[i]-(h[i]*z[i+1]))/u[i]
NEXT i
z[0]=0

FOR i=0 TO n-1
a[i]=y[i]
b[i]=((y[i+1]-y[i])/h[i])-((h[i]/6)*z[i+1])-((h[i]/3)*z[i])
c[i]=z[i]/2
d[i]=(z[i+1]-z[i])/(6*h[i])
NEXT i

MOVE w,x[0],y[0]
SETLINESTYLE w,@LSSOLID,2
FRONTPEN w,ColorBlend(colr,0xffffff,11,6)
FOR i=0 to n-1
FOR xx=x[i] to x[i+1] step 2
yy=a[i]+(xx-x[i])*(b[i]+(xx-x[i])*(c[i]+(xx-x[i])*d[i]))
LINETO w,xx,yy
NEXT xx
NEXT i

MOVE w,x[0],y[0]
SETLINESTYLE w,@LSSOLID,1
FRONTPEN w,colr
FOR i=0 to n-1
FOR xx=x[i] to x[i+1] step 2
yy=a[i]+(xx-x[i])*(b[i]+(xx-x[i])*(c[i]+(xx-x[i])*d[i]))
LINETO w,xx,yy
NEXT xx
NEXT i

ENDSUB

'colr - first color
'sideColr - second color
'cStep - number of steps between colors
'cStepNum - the color to return

SUB ColorBlend(colr:UINT BYVAL,sideColr:UINT BYVAL,cStep:INT BYVAL,cStepNum:INT BYVAL),UINT
DEF retColor:UINT
DEF rRed,rGreen,rBlue:INT
DEF r,g,b,sr,sg,sb:INT

b=Hex2Dec(MID$(right$("000000"+Hex$(colr),6),1,2))
g=Hex2Dec(MID$(right$("000000"+Hex$(colr),6),3,2))
r=Hex2Dec(MID$(right$("000000"+Hex$(colr),6),5,2))

sb=Hex2Dec(MID$(right$("000000"+Hex$(sidecolr),6),1,2))
sg=Hex2Dec(MID$(right$("000000"+Hex$(sidecolr),6),3,2))
sr=Hex2Dec(MID$(right$("000000"+Hex$(sidecolr),6),5,2))

IF cStepNum<1 THEN cStepNum=1
IF cStepNum>cStep THEN cStepNum=cStep

IF r<0 THEN r=0
IF r>255 THEN r=255
IF g<0 THEN g=0
IF g>255 THEN g=255
IF b<0 THEN b=0
IF b>255 THEN b=255

IF sr<0 THEN sr=0
IF sr>255 THEN sr=255
IF sg<0 THEN sg=0
IF sg>255 THEN sg=255
IF sb<0 THEN sb=0
IF sb>255 THEN sb=255

rRed=r+(((sr-r)*cStepNum)/(cStep))
rGreen=g+(((sg-g)*cStepNum)/(cStep))
rBlue=b+(((sb-b)*cStepNum)/(cStep))
retColor=RGB(rRed,rGreen,rBlue)
RETURN retColor
ENDSUB

SUB Hex2Dec(txt:STRING),UINT
DEF i,pow:INT
DEF res:UINT

res=0
FOR i=1 to LEN(txt)
pow=16^(LEN(txt)-i)
res+=(INSTR("0123456789ABCDEF",MID$(txt,i,1),1)-1)*pow
NEXT i
RETURN res
ENDSUB

ZeroDog

I believe the NEW function is what you are looking for. 

ckoehn

Thanks,  will check it out.

Later,
Clint