Cubic Bezier Curve

Bezier Curves implemented into ActionScript, De Casteljau-Algorythmus

BezierLine Class

Actionscript:
  1. /*    @package:        at.hm.draw
  2. *     @class:            BezierLine
  3. *     @version:        0.1
  4. *     @date:            12.08.2006
  5. *     @author:        Hannes Moser
  6. *     @description:    draw a cubic bezier curve(n=3), de Casteljau Algorythmus
  7. * */
  8.  
  9. import at.hm.geom.g2d.Point2D;
  10.  
  11. class at.hm.draw.BezierLine {
  12. // attributes
  13. private var s:Point2D;
  14. private var t:Point2D;
  15. private var a1:Point2D;
  16. private var a2:Point2D;
  17. private var m:MovieClip;
  18. private var th:Number;
  19. private var c:Number;
  20. private var tp:Number;
  21. private var it:Number;
  22. // constructor
  23. 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) {
  24.  
  25. s = new Point2D(sX, sY);
  26. t = new Point2D(tX, tY);
  27. a1 = new Point2D(a1X, a1Y);
  28. a2 = new Point2D(a2X, a2Y);
  29.  
  30. if(arguments.length <10) {
  31. th = 0.25;
  32. c = 0x000000;
  33. tp = 100;
  34. it = 4;
  35. } else if (arguments.length == 10) {
  36. th = thickness;
  37. c = 0x000000;
  38. tp = 100;
  39. it = 4;
  40. } else if (arguments.length == 11) {
  41. th = thickness;
  42. c = color;
  43. tp = 100;
  44. it = 4;
  45. } else if (arguments.length == 12) {
  46. th = thickness;
  47. c = color;
  48. tp = transperancy;
  49. it = 4;
  50. } else if (arguments.length == 13) {
  51. th = thickness;
  52. c = color;
  53. tp = transperancy;
  54. it = iterations;
  55. }
  56.  
  57. m = mc;
  58. }
  59. // draw
  60. public function draw():Void {
  61. renderCurve();
  62. }
  63. // render curve
  64. private function renderCurve():Void {
  65. m.lineStyle(th, c, tp);
  66. m.moveTo(s.x, s.y);
  67. for(var i:Number = 1; i <= it; i++) {
  68. drawCurveSegment((1 / it) * i);
  69. }
  70. }
  71. // drawCurveSegment
  72. private function drawCurveSegment(iterationStep:Number):Void {
  73. var tX:Number;
  74. var tY:Number;
  75. var iS:Number;
  76.  
  77. iS = iterationStep;
  78.  
  79. 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);
  80. 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);
  81.  
  82. m.lineTo(tX, tY);
  83. }
  84. }

Demo

Actionscript:
  1. /*    @package:
  2. *     @class:            Main
  3. *     @version:        0.1
  4. *     @date:            12.08.2006
  5. *     @author:        Hannes Moser
  6. *     @description:    main class for libtest project
  7. * */
  8.  
  9. import at.hm.draw.BezierLine;
  10.  
  11. class Main implements at.hm.core.Runnable {
  12. // attributes
  13. private var bez:BezierLine;
  14. // constructor
  15. public function Main() {
  16. }
  17. // methods
  18. public function run():Void {
  19. // trace init state of the application
  20. trace("libtest application");
  21. // init bezier line
  22. bez = new BezierLine(50, Stage.height - 10, Stage.width - 50, Stage.height - 10, 100, 50, Stage.width - 100, 50, _root, 2, 0x333333, 100, 25);
  23. bez.draw();
  24. }
  25. }


About this entry