/* * Utils class by Wade Walker. * wadedwalker.com/ * wadedwalker@gmail.com * * Copyright (c) 2006 - 2010 Wade Walker * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ package com.wadedwalker { import flash.display.DisplayObject; public class Utils { //--------------------------------------------------------------------------------------------------------------------------------------\\ public function Utils() { //Do Nothing } //--------------------------------------------------------------------------------------------------------------------------------------\\ //Random Number Generator by range static public function range(startNum:Number, endNum:Number):Number { return int(Math.random() * (endNum - startNum + 1) + startNum); } //--------------------------------------------------------------------------------------------------------------------------------------\\ //Points Distance Calculation static public function getDistanceByPoints(firstPoint:Number, secondPoint:Number):Number { return Math.abs(firstPoint - secondPoint); } //--------------------------------------------------------------------------------------------------------------------------------------\\ //Object Distance Calculation static public function getDistanceByObjects(firstObject:Object, secondObject:Object):Object { var distance:Object = {"dx":(Math.abs(firstObject.x - secondObject.x)), "dy":(Math.abs(firstObject.y - secondObject.y))}; return distance; } //--------------------------------------------------------------------------------------------------------------------------------------\\ //Calculate Vector by Points static public function calculateVectorByPoints(x1:Number, y1:Number, x2:Number, y2:Number):Object { var dx:Number = x2 - x1; var dy:Number = y2 - y1; var mag:Number = Math.sqrt((dx * dx) + (dy * dy)); var rad:Number = Math.atan2(dy, dx); var ang:Number = rad * 57.3; var vx:Number = Math.cos(rad); var vy:Number = Math.sin(rad); var object:Object = {vectorX:vx, vectorY:vy, magnitude:mag, angle:ang, radian:rad}; return object; } //--------------------------------------------------------------------------------------------------------------------------------------\\ //Calculate Vector by DisplayObjects static public function calculateVectorByPoints(object1:DisplayObject, object2:DisplayObject):Object { var dx:Number = object2.x - object1.x; var dy:Number = object2.y - object1.y; var mag:Number = Math.sqrt((dx * dx) + (dy * dy)); var rad:Number = Math.atan2(dy, dx); var ang:Number = rad * 57.3; var vx:Number = Math.cos(rad); var vy:Number = Math.sin(rad); var object:Object = {vectorX:vx, vectorY:vy, magnitude:mag, angle:ang, radian:rad}; return object; } } }