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

Draw Grid

...
@author ...
Get Adobe Flash player
by mtok 25 Jan 2009
package
{
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Tategaki extends Sprite
	{
		
		public function Tategaki() 
		{
			graphics.lineStyle(1, 0xA0EBF3);
			GraphicsUtils.drawGrid(graphics, new Rectangle(5, 5, 460, 460), 50);
			GraphicsUtils.drawGrid(graphics, new Rectangle(5, 5, 460, 460), 25, Vector.<Number>([2.5,2.5]));
		}
		
	}
	
}
import flash.display.Graphics;
import flash.geom.Point;
import flash.geom.Rectangle;

class GraphicsUtils {
	public function GraphicsUtils() {
		;
	}
	public static function drawGrid(g:Graphics, rect:Rectangle, size:Number, pattern:Vector.<Number> = null):void {
		var i:Number;
		var limitW:Number;
		var limitH:Number;
		
		limitW = rect.x + rect.width;
		limitH = rect.y + rect.height;
		if (pattern == null) {
			i = rect.y;
			while (i < limitH) {
				g.moveTo(rect.x, i);
				g.lineTo(limitW, i);
				i += size;
			}
			i = rect.x;
			while ( i < limitW) {
				g.moveTo(i, rect.y);
				g.lineTo(i, limitH);
				i += size;
			}
		}else {
			i = rect.y;
			while (i < limitH) {
				drawDotLine(g, rect.x, i, limitW, i, pattern);
				i += size;
			}

			i = rect.x;
			while (i < limitW) {
				drawDotLine(g, i, rect.y, i, limitH, pattern);
				i += size;
			}
		}
	}
	public static function drawDotLine(g:Graphics, fromX:Number, fromY:Number, toX:Number, toY:Number, pattern:Vector.<Number>):void {
		var i:int;
		var n:Number;
		var v:Point = new Point(toX - fromX, toY - fromY);
		var tmp:Point;
		var l:Number = v.length;
		var sl:SimpleList = new SimpleList();
		var s:Boolean = true;
		v.normalize(1);
		
		for (i = 0; i < pattern.length; i++) {
			n = pattern[i];
			tmp = v.clone();
			tmp.normalize(n);
			sl.append({v:tmp, length:n});
		}
		
		
		var pos:Point = new Point(fromX, fromY);
		var obj:Object;
		n = 0;
		g.moveTo(pos.x, pos.y);
		while (true) {
			obj = sl.next();
			n += obj.length;
			if (n > l) {
				pos.x = toX;
				pos.y = toY;
				s = step(g, pos, s);
				break;
			}else {
				pos.offset(obj.v.x, obj.v.y);
				s = step(g, pos, s);
			}
		}
	}
	private static function step(g:Graphics, p:Point, s:Boolean):Boolean {
		if (s) {
			g.lineTo(p.x, p.y);
		}else {
			g.moveTo(p.x, p.y);
		}
		return !s;
	}
}

class SimpleList {
	private var currentElement:ListElement;
	private var appendFunc:Function;
	public function SimpleList(data:* = null) {
		if (data != null) {
			_initialize(data);
		}else{
			appendFunc = _initialize;
		}
	}
	public function append(data:*):void {
		appendFunc.call(null, data);
	}
	public function _initialize(data:*):void {
		currentElement = new ListElement(data);
		currentElement.next = currentElement;
		currentElement.prev = currentElement;
		appendFunc = _append;
	}
	public function _append(data:*):void {
		var elem:ListElement = new ListElement(data);
		var tmp:ListElement;
		tmp = currentElement.next;
		currentElement.next = elem;
		elem.prev = currentElement;
		elem.next = tmp;
		tmp.prev = elem;
		
		currentElement = elem;
	}
	public function current():*{
		return currentElement.data;
	}
	public function next():* {
		currentElement = currentElement.next;
		return currentElement.data;
	}
	public function prev():*{
		currentElement = currentElement.prev;
		return currentElement.data;
	}
}
class ListElement {
	public var next:ListElement;
	public var prev:ListElement;
	public var data:*;
	public function ListElement(data:*) {
		this.data = data;
	}
}