Micheus ... this is a test REPLY.
Not working correct yet. Padding at ends logic needs help. And Polykind parameter must get used.
Code:
%% http://stackoverflow.com/questions/7054272/how-to-draw-smooth-curve-through-n-points-using-javascript-html5-canvas
%% TEST : cardinal_spline([0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,0.0], 12)
cardinal_spline(PTS, PolyKind, NumOfSegments) when is_integer(NumOfSegments)
when PolyKind == polyline orelse PolyKind == polygon ->
Tension = 0.7,
Len = length(PTS),
_Pts = fun(I0) ->
I = I0 + Len,
lists:nth((I rem Len) + 1,PTS)
end,
Fun = fun
(I, Acc) ->
Fun1 = fun(T, Acc1) ->
%% calc tension vectors
T1X = (element(1,_Pts(I+1)) - element(1,_Pts(I-1))) * Tension,
T2X = (element(1,_Pts(I+2)) - element(1,_Pts(I))) * Tension,
T1Y = (element(2,_Pts(I+1)) - element(2,_Pts(I-1))) * Tension,
T2Y = (element(2,_Pts(I+2)) - element(2,_Pts(I))) * Tension,
T1Z = (element(3,_Pts(I+1)) - element(3,_Pts(I-1))) * Tension,
T2Z = (element(3,_Pts(I+2)) - element(3,_Pts(I))) * Tension,
%% calc step
ST = 1.0 * T / NumOfSegments,
%% calc cardinals
C1 = 2.0 * pow(ST,3.0) - 3.0 * pow(ST,2.0) + 1.0,
C2 = -(2.0 * pow(ST,3.0)) + 3.0 * pow(ST,2.0),
C3 = pow(ST,3.0) - 2.0 * pow(ST,2.0) + ST,
C4 = pow(ST,3.0) - pow(ST,2.0),
%% calc x and y cords with common control vectors
X = C1 * element(1,_Pts(I)) + C2 * element(1,_Pts(I+1)) + C3 * T1X + C4 * T2X,
Y = C1 * element(2,_Pts(I)) + C2 * element(2,_Pts(I+1)) + C3 * T1Y + C4 * T2Y,
Z = C1 * element(3,_Pts(I)) + C2 * element(3,_Pts(I+1)) + C3 * T1Z + C4 * T2Z,
[{X,Y,Z}|Acc1]
end,
lists:foldl(Fun1, Acc, lists:seq(0,NumOfSegments-1))
end,
lists:foldl(Fun, [], lists:seq(1,length(PTS)-0)).