PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 0ms

/src/away3d/tools/Align.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 132 lines | 90 code | 20 blank | 22 comment | 14 complexity | c58cbe5ff3432e196e2be1550466af73 MD5 | raw file
  1package away3d.tools
  2{
  3	import away3d.arcane;
  4
  5	use namespace arcane;
  6	
  7	/**
  8	* Class Aligns an arrays of Object3Ds, Vector3D's or Vertexes compaired to each other.<code>Align</code>
  9	*/
 10	public class Align {
 11		
 12		private static var _axis:String;
 13		private static var _condition:String;
 14		
 15		/**
 16		* Applies to array elements the alignment according to axis, x, y or z and a condition.
 17		* each element must have public x,y and z  properties
 18		* String condition:
 19		* "+" align to highest value on a given axis
 20		* "-" align to lowest value on a given axis
 21		* "" align to a given axis on 0; This is the default.
 22		* "av" align to average of all values on a given axis
 23		*
 24		* @param	 aObjs		Array. An array with elements with x,y and z public properties such as Mesh, Object3D, ObjectContainer3D,Vector3D or Vertex
 25		* @param	 axis			String. Represent the axis to align on.
 26		* @param	 condition	[optional]. String. Can be '+", "-", "av" or "", Default is "", aligns to given axis at 0.
 27		*/		
 28		public static function align(aObjs:Array, axis:String, condition:String = ""):void
 29		{
 30			checkAxis(axis);
 31			checkCondition(condition);
 32			var base:Number;			
 33			
 34			switch(_condition){
 35				case "+":
 36					base = getMax(aObjs, _axis);
 37					break;
 38				
 39				case "-":
 40					base = getMin(aObjs, _axis);
 41					break;
 42				
 43				case "av":
 44					base = getAverage(aObjs, _axis);
 45					break;
 46				
 47				case "":
 48					base = 0;
 49			}
 50			
 51			for(var i:uint = 0;i<aObjs.length;++i){
 52				aObjs[i][_axis] = base;
 53			}
 54		}
 55		
 56		/**
 57		* Applies to array elements a distributed alignment according to axis, x,y or z.
 58		* each element must have public x,y and z  properties
 59		* @param	 aObjs		Array. An array with elements with x,y and z public properties such as Mesh, Object3D, ObjectContainer3D,Vector3D or Vertex
 60		* @param	 axis			String. Represent the axis to align on.
 61		*/		
 62		public static function distribute(aObjs:Array, axis:String):void
 63		{
 64			checkAxis(axis);
 65
 66			var max:Number = getMax(aObjs, _axis);
 67			var min:Number = getMin(aObjs, _axis);
 68			var unit:Number = (max - min) / aObjs.length;
 69			aObjs.sortOn(axis, 16);
 70			
 71			var step:Number = 0;
 72			for(var i:uint = 0;i<aObjs.length;++i){
 73				aObjs[i][_axis] = min+step;
 74				step+=unit;
 75			}
 76		}
 77		
 78		private static function checkAxis(axis:String):void
 79		{
 80			axis = axis.substring(0, 1).toLowerCase();
 81			if(axis == "x" || axis == "y" || axis == "z"){
 82				_axis  = axis;
 83				return;
 84			}
 85			
 86			throw new Error("Invalid axis: string value must be 'x', 'y' or 'z'");
 87		}
 88		
 89		private static function checkCondition(condition:String):void
 90		{
 91			condition = condition.toLowerCase();
 92			var aConds:Array = ["+", "-", "", "av"];
 93			for(var i:uint = 0;i<aConds.length;++i){
 94				if(aConds[i] == condition){
 95					_condition  = condition;
 96					return;
 97				}
 98			}
 99			
100			throw new Error("Invalid condition: possible string value are '+', '-', 'av' or '' ");
101		}
102		
103		private static function getMin(a:Array, prop:String):Number
104		{
105			var min:Number = Infinity;
106			for(var i:uint = 0;i<a.length;++i){
107				min = Math.min(a[i][prop], min);
108			}
109			return min;
110		}
111		
112		private static function getMax(a:Array, prop:String):Number
113		{
114			var max:Number = -Infinity;
115			for(var i:uint = 0;i<a.length;++i){
116				max = Math.max(a[i][prop], max);
117			}
118			return max;
119		}
120		
121		private static function getAverage(a:Array, prop:String):Number
122		{
123			var av:Number = 0;
124			var loop:int = a.length;
125			for(var i:uint = 0;i<loop;++i){
126				av += a[i][prop];
127			}
128			return av/loop;
129		}
130		 
131	}
132}