# Cubic spline  (piecewise third-order polynomial which passes through a set of control
# points and can be derived there up to the 2nd order: we have smoothness at the points;
# the second derivative is set to zero at the endpoints). Other examples here.

x <- c(1,3,4,5,7); y <- c(5,20,29,20,20)
x1 <- 0; x2 <- 8; y1 <- -1; y2 <- 30
Plane(0,8, 0,30); POINT(x,y, "blue")
lines(spline(x,y,n=1000),col="red")
# extension of tracking outside the data
lines(spline(x,y,n=1000,xmin=0,xmax=8),col="brown")



# Other example:
x <- c(20,50,80,110,140,170,200,230,260,290,320,350)
y <- c(13,13.5,12.5,10,9,7.5,6,5,6,7,11,15)
Plane(0,400, 0,16); POINT(x,y, "blue")
lines(spline(x,y,n=1000),col="red")

# Other example: the cross-sectional area of an artificial basin starting from some
# depth measurements

 
BF=4; HF=2
x = (0:10)*2
y = -c(0, 1.8, 2, 4, 4, 6, 4, 3.6, 3.4, 2.8, 0)
PLANE(0,20, -7,0); POINT(x,y, "red")
L = spline(x,y,1000); polyline(L$x,L$y,"black"); POINT(x,y, "red")
INTEGRAL = function(x,y) areaPol(c(x[length(x)],x[1],x), c(0,0,y))
INTEGRAL(L$x,L$y)                       # for integration see here
# 64.63094      I can assume that the area is 65 m^2

# Other example: hours of sunlight
# hours of sunlight - Trondheim
daylightT = c(4+45/60,7+17/60,10+13/60,13+29/60,16+41/60,19+43/60,20+21/60,17+44/60,
             14+30/60,11+23/60,8+9/60,5+20/60)
daylightT = c(daylightT,daylightT,4+45/60)
days = 0:24
BF=5; HF=3
Grid(0,25, 0,22)
gridHC(0:23, "grey60"); gridVC(0:25, "grey60")
GridHC((0:4)*6, "grey30"); GridVC((0:4)*6, "grey30")
ST = spline(days,daylightT,1000); polyline(ST$x,ST$y, "brown")
abovey("hours of sunlight")
underY("0",0); underY("6",6); underY("12",12); underY("18",18)
underX("2010",6); underX("2011",18)
# hours of sunlight - Genova
daylightG = c(8+55/60,9+50/60,11+10/60,12+46/60,14+13/60,15+18/60,15+29/60,14+39/60,
             13+14/60,11+44/60,10+13/60,9+6/60)
daylightG = c(daylightG,daylightG,8+55/60)
SG = spline(days,daylightG,1000); polyline(SG$x,SG$y, "black")
# hours of sunlight - Melburne
daylightM = c(14+44/60,14,12+56/60,11+41/60,10+33/60,9+43/60,9+35/60,10+12/60,11+16/60,
             12+28/60,13+41/60,14+34/60)
daylightM = c(daylightM,daylightM,14+44/60)
SM = spline(days,daylightM,1000); polyline(SM$x,SM$y, "blue")
text(25,15,"M",font=2,col="blue")
text(25,9,"G",font=2,col="black")
text(25,5,"T",font=2,col="brown")