# Drawing using Catmull and Rom cubic spline

**URL:** https://forum.kirupa.com/t/drawing-using-catmull-and-rom-cubic-spline/225623
**Category:** open source
**Created:** [May 14, 2007, 12:35pm UTC](https://forum.kirupa.com/t/drawing-using-catmull-and-rom-cubic-spline/225623 "2007-05-14T12:35:40Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![puppy](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@puppy](https://forum.kirupa.com/u/puppy)
#### Post date: [May 14, 2007, 12:35pm UTC](https://forum.kirupa.com/t/drawing-using-catmull-and-rom-cubic-spline/225623/1 "2007-05-14T12:35:40Z")

</div>

This came up on [other forum. I was just dorking around [url=http://www.mvps.org/directx/articles/catmull/]this](http://flashmove.com/forum/showthread.php?t=30560&page=2) article.

```auto
var px = [], py = [];
function spline (t, p) {
 // this maps t = 0..1 to P1..P2
 var P0 = p[p.length -4], P1 = p[p.length -3], P2 = p[p.length -2], P3 = p[p.length -1];
 return 0.5 * ((2 * P1) + (-P0 + P2) * t + (2*P0 - 5*P1 + 4*P2 - P3) * t * t + (-P0 + 3*P1 - 3*P2 + P3) * t * t * t);
}
function onMouseUp () {
 do { px.push (_xmouse); py.push (_ymouse); } while (px.length < 4);
 var x0 = px[px.length -2], y0 = py[py.length -2], x1 = px[px.length -1], y1 = py[py.length -1];
 // polyline
 _root.lineStyle(1, 0xFF0000, 100); _root.moveTo(x0, y0); _root.lineTo(x1, y1);
 // employ spline
 x0 = px[px.length -3]; y0 = py[py.length -3]; x1 = spline (1, px); y1 = spline (1, py);
 var n = Math.round(2 + Math.sqrt((x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1)) / 5);
 _root.lineStyle(1, 0x0000FF, 100); _root.moveTo(x0, y0);
 for (var i=1; i<=n; i++) {
  x1 = spline (i/n, px); y1 = spline (i/n, py); _root.lineTo(x1, y1);
 }
}

```

:block:
