================ BEZIER1 ================ Mathematicaを用いた練習を行います。 適当にプログラムの中の数字などを変更して試してみてください。 以下の説明の中で ***** ではさまれた部分を Mathematica に入力して下さい。自分で入力するか、コピー&ペーストして下さい。 ペーストしたものをShift+Enter(Return) キーで実行します。 上から順に,すべて実行するようにしてください。 上で定義した関数を後で利用すること があります。 関数のグラフを描くにはPlotを用います。 ****** Plot[Sin[x], {x, 0, 2 Pi}] ****** ベジエ曲線を描くには、ParametricPlotを用います。 これは、y=f(x)の形になるもの以外も描く必要があるためです。 ParametricPlotを用いて放物線を描いてみましょう。 ****** ParametricPlot[{-1 + 2*t, (-1 + 2*t)^2}, {t, 0,1}, AspectRatio->Automatic,Axes -> True, PlotRange->All] ****** 平面上にベジエ曲線を描くための準備として、まず次ぎを入力してください。 ***** lerp[p_List, q_List, r_, s_, t_] := (s - t)/(s - r)*p + (t - r)/(s - r)*q; ***** ***** badecas[cpoly__, r_, s_, t_] := Block[ {bb = cpoly, b = {}, m, i, j}, (m = Length[bb] - 1; Do[ Do[ b = Append[b, lerp[bb[[i]], bb[[i + 1]], r, s, t]], {i, 1, m - j + 1}]; bb = b; b = {}, {j, 1, m} ]; bb[[1]] ) ]; ***** decas[cpoly__, r_, s_, t_] := Block[ {bb = cpoly, b = {}, m, i, j, lseg = {}, res}, (m = Length[bb] - 1; Do[ Do[ b = Append[b, lerp[bb[[i]], bb[[i + 1]], r, s, t]]; If[i > 1, lseg = Append[lseg, { b[[ i - 1]], b[[i]]}]], {i, 1, m - j + 1}]; bb = b; b = {}, {j, 1, m} ]; res := Append[lseg, bb[[1]]]; res ) ]; ***** ***** < {Blue, Thickness[0.006]}, AxesLabel -> {"x", "y"}, AspectRatio -> Automatic, options] ***** ***** pdecas[cpoly__, r_, s_, t_]:= Block[ {bb = cpoly, pt, ll, res, i,l1, edge}, res = decas[bb, r, s, t]; pt = Last[res]; l1 = Length[res]-1; ll={}; Do[ edge = res[[i]]; ll = Append[ll, Line[edge]], {i, 1, l1} ]; res = Append[{RGBColor[1,0,0], ll}, {RGBColor[0,0.2,0], PointSize[0.02], Point[pt]}]; res ]; ***** ***** bezieranimate1[cpoly__, frame_]:=Animate[Show[Graphics[{AbsoluteThickness[0.5],Line[cpoly]}, AspectRatio->Automatic, PlotRange->{{Min[cpoly],Max[cpoly]},{Min[cpoly],Max[cpoly]}}], Graphics[{AbsoluteThickness[1.5],pdecas[cpoly, 0, 1 , (i-1)/frame]}]], {i,1,frame+1,1}, Frames->frame] ***** ***** bezieranimate2[cpoly__, a_, b_, {x0_, x1_}, { y0_, y1_}, frame_]:= Animate[Show[Graphics[{AbsoluteThickness[0.5],Line[cpoly]}, AspectRatio->Automatic, PlotRange->{{x0, x1},{y0, y1}}], Graphics[{AbsoluteThickness[1.5], pdecas[cpoly, 0, 1 , (i-1)/frame]}], Beziercurvegraphics[cpoly, a, b]], {i,1,frame+1,1}, Frames->frame] ***** では、放物線の例から始めましょう。 例1 ***** cpoly = {{-1, 1}, {0, -1}, {1, 1}} bezieranimate1[cpoly, 64] ***** 出力結果をアニメーションで見るために、 この場合の出力セルをすべて選択して(右端の真ん中をダブルクリックするとすべて選択されます)、 Mathematicaのメニューのセルにあるグラフィックスのアニメーション化 ( キーボードでは %Y )を選びます。 ***** bezieranimate2[cpoly, 0, 1, {-1,1}, {-1,1}, 64] ***** 例2 ***** cpoly =cpoly = {{1, 0}, {1, 1}, {0, 1}} bezieranimate1[cpoly, 64] ***** ***** bezieranimate2[cpoly,-1/2,3/2,{-1,1.1},{-1,1.1}, 64] ***** 例3 ***** cpoly:={{0, -1}, {2, 0}, {0, 1}}; bezieranimate1[cpoly, 64] ***** ***** bezieranimate2[cpoly, 0, 1, {Min[cpoly], Max[cpoly]}, {Min[cpoly], Max[cpoly]}, 64] ***** 例4 ***** cpoly={{-1,0},{0,2},{1,1}} bezieranimate1[cpoly, 64] ***** ***** bezieranimate2[cpoly, 0, 1, {Min[cpoly], Max[cpoly]}, {Min[cpoly], Max[cpoly]}, 64] ***** 次に3次曲線を描きましょう。 例5 ***** cpoly = {{2, 3}, {0, 2}, {0, 0}, {2, -1}} bezieranimate1[cpoly, 64] ***** ***** bezieranimate2[cpoly, 0, 1, {Min[cpoly], Max[cpoly]}, {Min[cpoly], Max[cpoly]}, 64] ***** 少し曲線をのばしてみましょう。 ***** bezieranimate2[cpoly, -1, 2, {-4, 14}, {-8, 10}, 64] ***** もっとのばしてみましょう。 ***** bezieranimate2[cpoly, -2, 3, {-4, 24}, {-8, 10}, 64] ***** 例6 ***** cpoly = {{14, 1}, {-4, 10}, {-4, -8}, {14, 1}} bezieranimate2[cpoly, 0, 1, {Min[cpoly], Max[cpoly]+0.5}, {Min[cpoly]-0.5, -Min[cpoly] + 2.5}, 64] ***** 例7 ***** cpoly={{3,3},{-1,-3},{-1,3},{3,-3}} bezieranimate1[cpoly, 64] ***** ***** bezieranimate2[cpoly,0,1,{Min[cpoly],Max[cpoly]},{Min[cpoly],Max[cpoly]},64] ***** 例8 ***** cpoly={{3,3},{-1,-5},{-1,5},{3,-3}} bezieranimate1[cpoly, 64] ***** ***** bezieranimate2[cpoly,0,1,{Min[cpoly],Max[cpoly]},{Min[cpoly],Max[cpoly]},64] ***** 例9 ***** cpoly={{3,4},{-1,-2},{-1,2},{3,-4}} bezieranimate1[cpoly, 64] ***** ***** bezieranimate2[cpoly,0,1,{Min[cpoly],Max[cpoly]},{Min[cpoly],Max[cpoly]},64] ***** 例10 ***** cpoly1={{-1,-1},{-(1/3),1},{(1/3),-1},{1,1}} bezieranimate1[cpoly1,64] ***** ***** bezieranimate2[cpoly1,0,1, {Min[cpoly1], Max[cpoly1]}, {Min[cpoly1], Max[cpoly1]}, 64] ***** 例11 ***** cpoly2 = {{-1, 0}, {1, 2}, {-1, 2}, {0, 3}, {1, 2}, {1, 0}, {-1, 2}, {-1, 0} } bezieranimate1[cpoly2, 64] ***** ***** bezieranimate2[cpoly2, 0, 1, {Min[cpoly2] - 0.1, Max[cpoly2]}, {Min[cpoly2] - 0.1, Max[cpoly2]}, 64] ***** では、ベジエ点 cpoly={{ , }, { , }, .... , { , }} を与えて、ベジエ曲線を作って見ましょう。 何かおもしろそうなものができたら、教えて下さい。