parallel_lines_featured

There was an interesting question asked on StackOverflow’s Flash category today. I thought it would be a nice challenge to solve. Have a look at the question here:
Draw 2 parallel lines between any 2 coordinates on the stage in AS3.

You can also find my detailed answer there too.

From that, I created an example to put the algorithm in use. Here’s the result running from the center of the stage to the mouse position.

This area requires
Adobe FlashPlayer version 11 or above.
iOS Devices are not currently supported.

And the Source Code all in one simple class file:

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class ParallelLines_Example extends Sprite
	{
		private var gap:Number = 100;
		
		public function ParallelLines_Example() {
			stage ? init() : addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event=null):void {
			e && removeEventListener(e.type, init);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			stage.addEventListener(Event.ENTER_FRAME, onUpdate);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
			stage.addEventListener(MouseEvent.CLICK, onClick);
			
			addText("Use the Mouse-Wheel or Click to change the gap between the parallel-lines.");
		}
		
		private function addText(pText:String):void {
			var tf:TextFormat = new TextFormat("Arial", 10, 0, true);
			var label:TextField = new TextField();
			label.defaultTextFormat = tf;
			label.width = stage.stageWidth;
			label.text = pText;
			label.mouseEnabled = false;
			
			addChild(label);
		}
		
		private function onWheel(e:MouseEvent):void {
			gap += e.delta * .25;
		}
		
		private function onClick(event:MouseEvent):void {
			gap = 1 + Math.random() * 100;
		}
		
		private function onUpdate(e:Event):void {
			var initA:Point = new Point(stage.stageWidth/2, stage.stageHeight/2);
			var initB:Point = new Point(stage.mouseX, stage.mouseY);
			
			var mouseAngle:Number = Math.atan2( initB.y - initA.y, initB.x - initA.x );
			
			var angle:Number;
			var halfGap:Number = gap * .5;
			var radians:Number = 180 / Math.PI;
			
			angle = 90 / radians + mouseAngle;
			var topOffsetX:Number = Math.cos( angle ) * halfGap;
			var topOffsetY:Number = Math.sin( angle ) * halfGap;
			
			angle = -90 / radians + mouseAngle;
			var bottomOffsetX:Number = Math.cos( angle ) * halfGap;
			var bottomOffsetY:Number = Math.sin( angle ) * halfGap;
			
			var topA:Point = new Point(initA.x + topOffsetX, initA.y + topOffsetY);
			var topB:Point = new Point(initB.x + topOffsetX, initB.y + topOffsetY);
			
			var bottomA:Point = new Point(initA.x + bottomOffsetX, initA.y + bottomOffsetY);
			var bottomB:Point = new Point(initB.x + bottomOffsetX, initB.y + bottomOffsetY);
			
			graphics.clear();
			graphics.lineStyle(1, 0);
			graphics.moveTo(topA.x, topA.y);
			graphics.lineTo(topB.x, topB.y);
			graphics.moveTo(bottomA.x, bottomA.y);
			graphics.lineTo(bottomB.x, bottomB.y);
		}
	}
}

If you have suggestions, questions or comments, submit them through the form below!

Hope this helps some of you :)