author Sam Moore Wed, 15 Jan 2014 15:31:16 +0000 (23:31 +0800) committer Sam Moore Wed, 15 Jan 2014 15:31:16 +0000 (23:31 +0800)
Probably doing it wrong.
It makes a curve, but it looks like it won't converge to the same curve as the basic algorithm.

index 7e3dbd3..976a6b6 100644 (file)
"cell_type": "markdown",
"source": [
"cell_type": "markdown",
"source": [
-      "Urgh"
+      "$L P = Q$ and $M P = R$"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "def L(n):\n",
+      "    try:\n",
+      "        return L.dynamic[n]\n",
+      "    except KeyError:\n",
+      "        result = asarray([asarray([ nCk(j,k) / (2.**j) for k in xrange(n+1)]) for j in xrange(n+1)])\n",
+      "        L.dynamic.update({n : result})\n",
+      "        return result\n",
+      "L.dynamic = {}                        \n",
+      "\n",
+      "def M(n):\n",
+      "    try:\n",
+      "        return M.dynamic[n]\n",
+      "    except KeyError:\n",
+      "        result = asarray([asarray([ nCk(n-j,n-k) / (2.**(n-j)) for k in xrange(n+1)]) for j in xrange(n+1)])\n",
+      "        M.dynamic.update({n : result})\n",
+      "        return result       \n",
+      "M.dynamic = {}\n",
+      "\n"
+     ],
+     "language": "python",
+     "outputs": [],
+     "prompt_number": 296
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "points = [(0,0), (0.5,1), (1,0)]"
+     ],
+     "language": "python",
+     "outputs": [],
+     "prompt_number": 298
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "def Casteljau(P, nPoints):\n",
+      "    while len(P) < nPoints:\n",
+      "        Q = dot(L(len(P)-1), P)\n",
+      "        R = dot(M(len(P)-1), P)\n",
+      "        P = list(Q) + list(R)[1:]\n",
+      "    return P\n",
+      "    "
+     ],
+     "language": "python",
+     "outputs": [],
+     "prompt_number": 329
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "points = [(0,0), (0.5,1), (1,0)]\n",
+      "c = Casteljau(points,50)\n",
+      "PlotBezier(points)\n",
+      "plot([p[0] for p in c], [p[1] for p in c], 'go-')"
+     ],
+     "language": "python",
+     "outputs": [
+      {
+       "output_type": "pyout",
+       "prompt_number": 332,
+       "text": [
+        "[<matplotlib.lines.Line2D at 0x5a3a6d0>]"
+       ]
+      },
+      {
+       "output_type": "display_data",
+       "text": [
+        "<matplotlib.figure.Figure at 0x5942790>"
+       ]
+      }
+     ],
+     "prompt_number": 332
+    },
+    {
+     "cell_type": "markdown",