/**
* Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/79RD
*/
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.geom.Point;
public class ch35ex6 extends Sprite {
protected var pts:Vector.<Point>;
protected var curve:Shape;
protected var points:Shape;
public function ch35ex6() {
points = new Shape();
addChild(points);
curve = new Shape();
addChild(curve);
//click to add a point
stage.addEventListener(MouseEvent.CLICK, onClick);
//press any key to clear the screen
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
onKeyDown(null);
}
protected function onClick(event:MouseEvent):void {
var p:Point = new Point(stage.mouseX, stage.mouseY);
//first add in the midpoint between the last point and this point
if (pts.length > 0)
{
var midpoint:Point = Point.interpolate(pts[pts.length-1], p, 0.5);
pts.push(midpoint);
points.graphics.lineStyle(1, 0, 0.2);
points.graphics.lineTo(p.x, p.y);
points.graphics.drawCircle(midpoint.x, midpoint.y, 5);
}
pts.push(p);
points.graphics.lineStyle(1, 0, 0.5);
points.graphics.drawCircle(p.x, p.y, 2);
drawCurve();
}
protected function onKeyDown(event:KeyboardEvent):void {
pts = new Vector.<Point>();
curve.graphics.clear();
points.graphics.clear();
}
protected function drawCurve():void {
if (pts.length < 4) return;
curve.graphics.clear();
curve.graphics.lineStyle(3);
curve.graphics.moveTo(pts[1].x, pts[1].y);
//throw out the first point (i = 1) and the last (i < pts.length - 3)
//since we're only drawing between midpoints
for (var i:int = 1; i < pts.length - 3; i += 2) {
curve.graphics.curveTo(pts[i+1].x, pts[i+1].y, pts[i+2].x, pts[i+2].y);
}
}
}
}