In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Bezier - Closed Loop

Get Adobe Flash player
by PESakaTFM 23 Feb 2010
package
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class Bezier extends Sprite
	{
		public const NUM_POINTS:int = 4;
		public var pts:Vector.<Point> = new Vector.<Point>();
		protected var mc:Sprite;
		
		public function Bezier()
		{
			var c:Cross;
			for(var i:int=0; i<NUM_POINTS; i++)
			{
				pts[i] = new Point(10+400*Math.random() >> 0, 10+400*Math.random() >> 0);
				c = new Cross(pts[i]);
				addChild(c);
				
				c.buttonMode = true;
				c.addEventListener(MouseEvent.MOUSE_DOWN, drag);
			}
			
			draw();
		}
		
		public function drawClosedLoop(pnts:Vector.<Point>, accuracy:int=20):void
		{
			var p:Vector.<Point> = new Vector.<Point>(3,true);
			for(var i:int=0; i<pnts.length-2; i++)
			{
				p[0] = Point.interpolate(pnts[i], pnts[i+1], .5);
				p[1] = pnts[i+1];
				p[2] = Point.interpolate(pnts[i+1], pnts[i+2], .5);
				drawCurve(p, accuracy);
			}
			p[0] = Point.interpolate(pnts[i], pnts[i+1], .5);
			p[1] = pnts[i+1];
			p[2] = Point.interpolate(pnts[i+1], pnts[0], .5);
			drawCurve(p, accuracy);
			i++;
			p[0] = Point.interpolate(pnts[i], pnts[0], .5);
			p[1] = pnts[0];
			p[2] = Point.interpolate(pnts[0], pnts[1], .5);
			drawCurve(p, accuracy);
		}
		
		public function drawCurve(pnts:Vector.<Point>, accuracy:int=20):void
		{
			var g:Graphics = this.graphics;
			var a:int;
			var p:Point;
			
			g.moveTo(pnts[0].x, pnts[0].y);
			for(a=0; a <= accuracy; a++)
			{
				p = getPointAt(pnts, a/accuracy);
				g.lineTo(p.x, p.y);
			}
		}
		
		protected function getPointAt(pnts:Vector.<Point>, t:Number):Point
		{
			var n:int = pnts.length-1;
			var m:Number = Math.pow((1-t), n);
			var p:Point = new Point(pnts[0].x * m, pnts[0].y * m);
			for(var i:int=1; i<=n; i++)
			{
				m = Math.pow((1-t), n-i)*bc(n, i)*Math.pow(t, i);
				p = p.add(new Point(pnts[i].x * m, pnts[i].y * m));
			}
			return p;
		}
		
		protected function bc(n:int, k:int):int // Binomial Coefficient
		{
			return (f(n)/(f(k)*f(n-k)));
		}
		
		protected function f(value:int):int // Factorial
		{
			var i:int = value;
			var result:int = 1;
			
			for(; i>0; i--)
			{
				result *= i;
			}
			
			return result;
		}
		
		protected function draw():void
		{
			graphics.clear();
			graphics.lineStyle(1,0xFF0000);
			drawClosedLoop(pts);
			graphics.endFill();
		}
		
		protected function drag(event:MouseEvent):void
		{
			mc = event.currentTarget as Sprite;
			stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, move);
			move();
		}
		
		protected function endDrag(event:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, move);
			move();
		}
		
		protected function move(event:MouseEvent=null):void
		{
			mc.x = mouseX;
			mc.y = mouseY;
			draw();
		}
	}
}
	import flash.display.Sprite;
	import flash.geom.Point;
	

class Cross extends Sprite
{
	protected var pnt:Point;
	public function Cross(p:Point)
	{
		graphics.beginFill(0xFF0000, 0);
		graphics.drawCircle(0,0,6);
		graphics.endFill();
		graphics.lineStyle(1, 0x0000FF);
		graphics.moveTo(-3,-3);
		graphics.lineTo(3, 3);
		graphics.moveTo(-3, 3);
		graphics.lineTo(3,-3);
		graphics.endFill();
		
		pnt = p;
		super.x = pnt.x;
		super.y = pnt.y;
	}
	
	override public function set x(value:Number):void
	{
		super.x = pnt.x = value;
	}
	
	override public function set y(value:Number):void
	{
		super.y = pnt.y = value;
	}
}