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

Vector Intersection Experiment

Get Adobe Flash player
by Johannes 10 May 2010
package {
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import fl.controls.Button;

	public class VectorTesting extends Sprite 
	{
		private var randomvector:Button = new Button();
		private var colors:Array = new Array(0xff0000, 0x00ff00, 0x0000ff, 0xff9900, 0x9933ff, 0x00ffff);
		private var v1:Object, v2:Object, v3:Object, v4:Object, v5:Object, v6:Object;
		
		public function VectorTesting() : void
		{
			this.addChild(randomvector);
			randomvector.x = 5;
			randomvector.y = 5;
			randomvector.label = 'Randomize Vectors';
			
			v1 =
			{
				p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight),
				p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)
			};
			
			v2 =
			{
				p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight),
				p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)
			};
			
			v3 =
			{
				p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight),
				p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)
			};
			
			v4 =
			{
				p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight),
				p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)
			};
			
			v5 =
			{
				p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight),
				p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)
			};
			
			v6 =
			{
				p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight),
				p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)
			};
			
			v1 = svector(v1);
			v2 = svector(v2);
			v3 = svector(v3);
			v4 = svector(v4);
			v5 = svector(v5);
			v6 = svector(v6);
			
			stage.addEventListener(Event.ENTER_FRAME, update);
			randomvector.addEventListener(MouseEvent.CLICK, rv);
		}
		
		protected function rv(e:MouseEvent = null) : void
		{
			v1 = svector({p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight), p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)});
			v2 = svector({p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight), p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)});
			v3 = svector({p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight), p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)});
			v4 = svector({p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight), p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)});
			v5 = svector({p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight), p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)});
			v6 = svector({p0: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight), p1: new Point(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight)});
		}
		
		protected function svector(v:Object) : Object
		{
			if(!v.p1)
			{
				v.p1 = new Point
				(
					v.p0.x + v.vx,
					v.p0.y + v.vy
				);
			}
			
			if(!v.vx || !v.vy)
			{
				v.vx = v.p1.x - v.p0.x;
				v.vy = v.p1.y - v.p0.y;
			}
			
			v.length = Math.sqrt(v.vx * v.vx + v.vy * v.vy);
			v.dx = v.vx / v.length;
			v.dy = v.vy / v.length;
			
			return v;
		}
		
		protected function uvector(v:Object) : Object
		{
			v.vx = v.p1.x - v.p0.x;
			v.vy = v.p1.y - v.p0.y;
			v.length = Math.sqrt(v.vx * v.vx + v.vy * v.vy);
			v.dx = v.vx / v.length;
			v.dy = v.vy / v.length;
			
			return v;
		}
		
		protected function dotProduct(a:Object, b:Object) : Number
		{
			return a.vx * b.vx + a.vy * b.vy;
		}
		
		protected function perpProduct(a:Object, b:Object) : Number
		{
			return a.vx * b.vy - a.vy * b.vx;
		}
		
		protected function intersection(a:Object, b:Object) : Intersection
		{
			var ca = {vx: b.p0.x - a.p0.x, vy: b.p0.y - a.p0.y};
			var cb = {vx: a.p0.x - b.p0.x, vy: a.p0.y - b.p0.y};
			
			var t:Number, t1:Number = 2, t2:Number = 2;
			
			if((a.dx == b.dx && a.dy == b.dy) || (a.dx == -b.dx && a.dy == -b.dy))
			{
				t = 1000000;
			} else {
				t1 = perpProduct(ca, b) / perpProduct(a, b);
				t2 = perpProduct(cb, a) / perpProduct(b, a);
				t = t1;
			}
			
			return new Intersection
			(
				new Point
				(
					a.p0.x + a.vx * t,
					a.p0.y + a.vy * t
				),
				t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1
			);
		}

		protected function update(e:Event = null) : void
		{
			this.graphics.clear();
			
			v1.p0.x = mouseX;
			v1.p0.y = mouseY;
			v1 = uvector(v1);
			
			var vectors:Array = new Array(v1, v2, v3, v4, v5, v6);
			
			for(var index in vectors)
			{
				this.graphics.lineStyle(2, colors[index]);
				this.graphics.moveTo(vectors[index].p0.x, vectors[index].p0.y);
				this.graphics.lineTo(vectors[index].p1.x, vectors[index].p1.y);
				
				for(var i:int = 0; i <= 1; ++i)
				{
					this.graphics.lineStyle(1, 0);
					this.graphics.drawCircle(vectors[index]['p' + i].x, vectors[index]['p' + i].y, 4);
				}
			}
			
			var intersections:Array = new Array(v1, v2, v3, v4, v5, v6);
			var _intersection:Intersection;
			
			for(var j:int = 0; j < intersections.length; j++)
			{
				for(var k:int = 0; k < intersections.length; k++)
				{
					_intersection = intersection(intersections[j], intersections[k]);
					
					if(_intersection.t)
					{
						this.graphics.lineStyle(3, 0xff00ff);
						this.graphics.drawCircle(_intersection.x, _intersection.y, 6);
					}
				}
			}
		}
	}
}

import flash.geom.Point;

class Intersection
{
	public var x:Number;
	public var y:Number;
	public var t:Boolean; // hello.
	
	public function Intersection(p:Point, _t:Boolean) : void
	{
		x = p.x;
		y = p.y;
		t = _t;
	}
}