⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vectorutils.as

📁 flex3d 的源码
💻 AS
字号:
/**
* project3D Engine
* @author John Sword
* @version 2 - AS3
*/

package engine.math
{
	
	import engine.geom.Vector;

	public class VectorUtils 
	{
				
		private static const sqrt:Function = Math.sqrt;
		
		// calculate the dot product of 2 vectors
		public static function dot (v1:*, v2:*) : Number
		{
			return ( v1.x * v2.x ) + ( v1.y * v2.y ) + ( v1.z * v2.z );
		}
		
		// calculate the components of the cross product
		public static function cross ( v1:*, v2:* ) : Vector
		{
			// cross product vector that will be returned
			// cross[x] = d1[y] * d2[z]   -   d1[z] * d2[y]
			// cross[y] = d1[z] * d2[x]   -   d1[x] * d2[z]
			// cross[z] = d1[x] * d2[y]   -   d1[y] * d2[x]
			var v:Vector = new Vector();
			v.x = (v1.y * v2.z) - (v1.z * v2.y)
			v.y = (v1.z * v2.x) - (v1.x * v2.z)
			v.z = (v1.x * v2.y) - (v1.y * v2.x)
			return v;
		}

		public static function negate ( v:Vector ) : Vector
		{
			return new Vector( -v.x, -v.y, - v.z);
		}
		
		public static function add ( v1:Vector, v2:Vector ) : Vector
		{
			return new Vector( 	v1.x + v2.x ,
								v1.y + v2.y ,
								v1.z + v2.z );
		}
		
		// calculate the difference between 2 vectors (vector subtraction)
		public static function difference ( v1:*, v2:* ) : Vector
		{
			var v:Vector = new Vector();
			v.x = v1.x-v2.x;
			v.y = v1.y-v2.y;
			v.z = v1.z-v2.z;
			return v;
		}
		
		// returns the normal of this vector
		public static function norm ( v:Vector ) : Number
		{
			return (sqrt ((v.x * v.x) + (v.y * v.y) + (v.z * v.z)));
		}
	
		// returns the unit vector of this vector
		public static function Normalize ( v:Vector ) : Vector
		{
		   var n:Number = norm ( v );
		   v.x /= n;
		   v.y /= n;
		   v.z /= n;
		   return v;
		}
		
		public static function toRadian ( n:Number ) : Number
		{
			return n * (Math.PI / 180);
		}
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -