0 Replies - 1045 Views - Last Post: 15 November 2006 - 10:55 AM

#1 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Bezier Curve Generation

Posted 15 November 2006 - 10:55 AM

Description: Copy and paste into the first frame of your fileThis little snippet draws Bezier Curves on the screen
var mouseListener:Object = new Object();

this.createEmptyMovieClip("scene_mc", 1);
scene_mc._x = 0;
scene_mc._y = 0;
var control1_mc:MovieClip = scene_mc.createEmptyMovieClip("P0 -> P1", 2);
var control2_mc:MovieClip = scene_mc.createEmptyMovieClip("P2 -> P3", 3);
var curve_mc:MovieClip = scene_mc.createEmptyMovieClip("b_curve", 4);

var counter:Number = 0;
var point:Array = new Array(4);

mouseListener.onmousedown = function() {
     switch(counter) {
          case 0:
          curve_mc.clear();
          control1_mc.clear();
          control2_mc.clear();
          control1_mc.moveTo(_xmouse, _ymouse);
          point[counter] = new Array(_xmouse, _ymouse);
          counter++;
          break;
          case 1:
          control1_mc.lineTo(_xmouse, _ymouse);
          point[counter] = new Array(_xmouse, _ymouse);
          counter++;
          break;
          case 2:
          control2_mc.moveTo(_xmouse, _ymouse);
          point[counter] = new Array(_xmouse, _ymouse);
          counter++;
          break;
          case 3:
          control2_mc.lineTo(_xmouse, _ymouse);
          point[counter] = new Array(_xmouse, _ymouse);
          bezier(point[3][0], point[3][1]);
          counter = 0;
          break;
     }
}

mouseListener.onmousemove = function() {
     switch(counter) {
          case 1:
          control1_mc.clear();
          control1_mc.lineStyle(1, 0x000000, 100);
          control1_mc.moveTo(point[0][0], point[0][1]);
          control1_mc.lineTo(_xmouse, _ymouse);
          break;
          case 3:
          control2_mc.clear();
          control2_mc.lineStyle(1, 0x000000, 100);
          control2_mc.moveTo(point[2][0], point[2][1]);
          control2_mc.lineTo(_xmouse, _ymouse);
          bezier(_xmouse, _ymouse);
          break;
     }
}

Mouse.addListener(mouseListener);

function bezier(point3x:Number, point3y:Number) {
     curve_mc.clear();
     curve_mc.lineStyle(2, 0xff0000, 100);
     curve_mc.moveTo(point[0][0], point[0][1]);
     var t_div:Number = 1/20;
     for(var t:Number = t_div; t <= 1 + t_div; t += t_div) {
          var xt:Number = (((point[2][0] + 3 * (point[1][0] - point3x) - point[0][0]) * t + 3 * (point3x - 2 * point[1][0] + point[0][0])) * t + 3 * (point[1][0] - point[0][0])) * t + point[0][0];
          var yt:Number = (((point[2][1] + 3 * (point[1][1] - point3y) - point[0][1]) * t + 3 * (point3y - 2 * point[1][1] + point[0][1])) * t + 3 * (point[1][1] - point[0][1])) * t + point[0][1];
          curve_mc.lineTo(xt, yt);
     }
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1