Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 
Share Thread:
Reddit Facebook Twitter
Request for automatic handle
02-04-2016, 02:49 PM (This post was last modified: 02-04-2016 03:27 PM by ggaliens.)
Post: #5
RE: Request for automatic handle
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)).





Reply


Messages In This Thread
Request for automatic handle - cyseal - 02-01-2016, 08:57 PM
RE: Request for automatic handle - ggaliens - 02-04-2016 02:49 PM
RE: Request for automatic handle - cyseal - 02-04-2016, 04:01 PM
RE: Request for automatic handle - micheus - 02-04-2016, 04:37 PM
RE: Request for automatic handle - cyseal - 02-05-2016, 10:50 AM
RE: Request for automatic handle - cyseal - 02-05-2016, 07:33 PM

Forum Jump:


User(s) browsing this thread: 1 Guest(s)