Return to Snippet

Revision: 24143
at February 22, 2010 09:04 by Flanture


Initial Code
package com.blogspot.flanture.drawing
{

// Spiderweb drawing class
// author: http://flanture.blogspot.com
// version: 1.0
// February 2010

	import flash.display.Sprite;
	import flash.geom.Point;
	
	public class spiderweb extends Sprite
	{
		private var _circles:uint; // number of concentric circles
		private var _nods:uint; // number of nods on each circle
		private var _df:Number; // circles distance factor
		private var _fr:Number; // smallest circle radius
		private var _sf:Number; // stretch factor
		
		public function spiderweb(circles:uint, nods:uint, fr:Number, df:Number, sf:Number)
		{
			_circles = circles;
			_nods = nods;
			_fr = fr;
			_df = df;
			_sf = sf;
			
			init();
		}
		
		private function init():void
		{

			var cx = 0;
			var cy = 0;
			var coords:Array = new Array();
			var controls:Array = new Array();
			var angle:Number;
			var controlAngle:Number;
			var currR:Number;
			var angleStep:Number = 360 / _nods;			
			currR = _fr;

			for(var k:uint = 0; k < _circles; k++)
			{
				currR += currR * _df;
				
				for (var i:uint = 0; i < _nods; i++)
				{
					angle = angleStep * (i + 1);
					controlAngle = angle - angleStep / 2;
					
					controlAngle = controlAngle * Math.PI / 180;
					angle = angle * Math.PI / 180;
					
					var x1:Number = trim(currR * Math.cos(angle));
					var y1:Number = trim(currR * Math.sin(angle));
					
					var tx:Number = trim(currR * Math.cos(controlAngle));
					var ty:Number = trim(currR * Math.sin(controlAngle));
					
					tx *= _sf;
					ty *= _sf;
					
					var cpoint:Point = new Point(tx, ty);
					controls.push(cpoint);
					
					var point:Point = new Point(x1, y1);
					coords.push(point);
					
				}
				
				drawStraights(coords);
				drawCurves(coords, controls);
				coords.splice(0, _nods);
				controls.splice(0, _nods);
			}
		}
		
		private function trim(num:Number):Number
		{
			var nstr:int = int(num*100);
			return Number(nstr / 100);
		}
		
		private function drawStraights(array):void
		{
			this.graphics.lineStyle(1, 0xffffff);
			this.graphics.moveTo(0,0);
			for (var i:uint = 0; i < array.length; i++)
			{
				this.graphics.lineTo(array[i].x, array[i].y);
				this.graphics.moveTo(0,0);
			}
		}
		
		private function drawCurves(arrayA, arrayB):void
		{
			
			this.graphics.lineStyle(2, 0xffffff);
			for (var j:uint = 0; j < arrayA.length-1; j++)
			{
				this.graphics.moveTo(0,0);
				this.graphics.moveTo(arrayA[j].x, arrayA[j].y);
				this.graphics.curveTo(arrayB[j+1].x, arrayB[j+1].y, arrayA[j+1].x, arrayA[j+1].y);
				this.graphics.moveTo(0,0);
			}
			this.graphics.moveTo(arrayA[arrayA.length-1].x, arrayA[arrayA.length-1].y);
			this.graphics.curveTo(arrayB[0].x, arrayB[0].y, arrayA[0].x, arrayA[0].y);			
		}
	}
}

Initial URL
http://flanture.blogspot.com/2010/02/improved-spiderweb-as30-class.html

Initial Description
Check the URL to see image examples

Initial Title
Improved Spiderweb ActionScript3.0 Class

Initial Tags
actionscript

Initial Language
ActionScript 3