SINGLE POST

Cubic Bezier Curve

Bezier Curves implemented into ActionScript, De Casteljau-Algorythmus

BezierLine Class
[as]
/*    @package:        at.hm.draw
*     @class:            BezierLine
*     @version:        0.1
*     @date:            12.08.2006
*     @author:        Hannes Moser
*     @description:    draw a cubic bezier curve(n=3), de Casteljau Algorythmus
* */

import at.hm.geom.g2d.Point2D;

class at.hm.draw.BezierLine {
// attributes
private var s:Point2D;
private var t:Point2D;
private var a1:Point2D;
private var a2:Point2D;
private var m:MovieClip;
private var th:Number;
private var c:Number;
private var tp:Number;
private var it:Number;
// constructor
function BezierLine(sX:Number, sY:Number, tX:Number, tY:Number, a1X:Number, a1Y:Number, a2X:Number, a2Y:Number, mc:MovieClip, thickness:Number, color:Number, transperancy:Number, iterations:Number) {

s = new Point2D(sX, sY);
t = new Point2D(tX, tY);
a1 = new Point2D(a1X, a1Y);
a2 = new Point2D(a2X, a2Y);

if(arguments.length < 10) {
th = 0.25;
c = 0x000000;
tp = 100;
it = 4;
} else if (arguments.length == 10) {
th = thickness;
c = 0x000000;
tp = 100;
it = 4;
} else if (arguments.length == 11) {
th = thickness;
c = color;
tp = 100;
it = 4;
} else if (arguments.length == 12) {
th = thickness;
c = color;
tp = transperancy;
it = 4;
} else if (arguments.length == 13) {
th = thickness;
c = color;
tp = transperancy;
it = iterations;
}

m = mc;
}
// draw
public function draw():Void {
renderCurve();
}
// render curve
private function renderCurve():Void {
m.lineStyle(th, c, tp);
m.moveTo(s.x, s.y);
for(var i:Number = 1; i <= it; i++) {
drawCurveSegment((1 / it) * i);
}
}
// drawCurveSegment
private function drawCurveSegment(iterationStep:Number):Void {
var tX:Number;
var tY:Number;
var iS:Number;

iS = iterationStep;

tX = s.x * Math.pow((1 - iS), 3) + 3 * a1.x * iS * Math.pow((1 - iS), 2) + 3 * a2.x * Math.pow(iS, 2) * (1 - iS) + t.x * Math.pow(iS, 3);
tY = s.y * Math.pow((1 - iS), 3) + 3 * a1.y * iS * Math.pow((1 - iS), 2) + 3 * a2.y * Math.pow(iS, 2) * (1 - iS) + t.y * Math.pow(iS, 3);

m.lineTo(tX, tY);
}
}
[/as]

Demo
[as]
/*    @package:
*     @class:            Main
*     @version:        0.1
*     @date:            12.08.2006
*     @author:        Hannes Moser
*     @description:    main class for libtest project
* */

import at.hm.draw.BezierLine;

class Main implements at.hm.core.Runnable {
// attributes
private var bez:BezierLine;
// constructor
public function Main() {
}
// methods
public function run():Void {
// trace init state of the application
trace(“libtest application”);
// init bezier line
bez = new BezierLine(50, Stage.height – 10, Stage.width – 50, Stage.height – 10, 100, 50, Stage.width – 100, 50, _root, 2, 0×333333, 100, 25);
bez.draw();
}
}
[/as]


One thought on “Cubic Bezier Curve

  1. Pingback: Hannes Moser » Blog Archive » Physics