读书人

45渡坐标系数据转换工具类

发布时间: 2012-12-23 11:28:15 作者: rapoo

45度坐标系数据转换工具类

开源下自己写的一个45度坐标系数据转换工具类  /***45度坐标系数据转换工具类* code wxsr */package com.engine.data.referenceFrame45{import flash.geom.Point;public class Rectangle45 //implements IRectangle45{public static var ARI_MODE:Boolean=falsepublic static const anglex:Number=Math.asin(1/Math.sqrt(5))*(180/Math.PI)private static const angley:Number=Math.asin(2/Math.sqrt(5))*(180/Math.PI)public static const sideFirst:Point=new Point(1,1)public static const sideSecond:Point=new Point(-1,1)public static const sideThird:Point=new Point(-1,-1)public static const sideFour:Point=new Point(1,-1)/***角度转弧度 * @param angle* @return * */public static function angleToradian(angle:Number):Number{return angle*180/Math.PI*angle;}/*** 弧度转角度 * @param radian* @return * */public static function radianToangle(radian:Number):Number{return radian*Math.PI/180*radian}/***获取x单位45度移动增量 * @param targetPoint 目标坐标* @param leftVertex 新开始点* @param origin 原点* @return * */public static function vx(targetPoint:Point, cell:CellGird45):Number{var pox:Number=(targetPoint.x+cell.leftVertex.x)/2 - (targetPoint.y-cell.leftVertex.y)-cell.origin.x;return pox;}/*** 获取y单位45度移动增量 * @param targetPoint* @param leftVertex* @param origin* @return * */public static function vy(targetPoint:Point, cell:CellGird45):Number{var poy:Number=(targetPoint.x+cell.leftVertex.x)/2 + (targetPoint.y-cell.leftVertex.y)-cell.origin.x;return poy;}/***获取直角坐标某点的x45度坐标 * @param targetPoint* @param newLeftVertex* @return * */public static function x45(targetPoint:Point, cell:CellGird45):Point{var poX:Number=vx(targetPoint,cell)return new Point(cell.leftVertex.x+poX,cell.leftVertex.y-poX*.5)}/*** 获取直角坐标某点的y45度坐标 * @param targetPoint* @param newLeftVertex* @return * */public static function y45(targetPoint:Point, cell:CellGird45):Point{var poY:Number=vy(targetPoint,cell)return new Point(cell.leftVertex.x+poY,cell.leftVertex.y+poY*.5)}/***将直角坐标系的一点转换为45度坐标系 对应点* @param targetPoint* @param newLeftVertex* @return * */public static function xy45(targetPoint:Point, cell:CellGird45):Point{var poX:Point=x45(targetPoint,cell)var poY:Point=y45(targetPoint,cell)var x:Number=poX.x+poY.x-cell.leftVertex.x;var y:Number=poX.y+poY.y-cell.leftVertex.y;return new Point(x,y)}/***获取棋盘整型坐标 * @param point* @return * */public static function getIndex(point:Point,cell:CellGird45):Point{point==null?point=new Point:''var pox:Number= Math.floor((point.x-cell.leftVertex.x)/cell.cellWidth - (point.y-cell.leftVertex.y)/cell.cellHeight);var poy:Number=Math.floor((point.x-cell.leftVertex.x)/cell.cellWidth + (point.y-cell.leftVertex.y)/cell.cellHeight);return new Point(pox,poy)}/***将45度坐标转换为直角坐标系 * @param point* @return * */public static function local45ToGlobal(point:Point,cell:CellGird45):Point{var size:Number=cell.cellHeight;var pox:Number=cell.leftVertex.x+size*(point.x+point.y);var poy:Number=cell.leftVertex.y+size/2*(point.y-point.x);return new Point(pox,poy)};public static function symmetryChange(oldSide:Point,newSide:Point,camerPoint:Point,oblPoint:Point):Point{var dirX:int=newSide.x-oldSide.xvar dirY:int=newSide.y-oldSide.yif(dirX!=0){dirX>0?dirX=1:dirX=-1}if(dirY!=0){dirY>0?dirY=1:dirY=-1}var disx:int=(oblPoint.x-camerPoint.x)*dirX;var disy:int=(oblPoint.y-camerPoint.y)*dirY;var newPoint:Point=new Point(oblPoint.x+disx*2,oblPoint.y+disy*2)return newPoint}/*** * @param point* @param cell* @param isReverse* @return * */public static function fitPoint(point:Point,cell:CellGird45,isReverse:Boolean=false):Point{var newPoint:Pointif(isReverse){newPoint=local45ToGlobal(getIndex(point,cell),cell)return getIndex(newPoint,cell)}newPoint=getIndex(point,cell)return local45ToGlobal(newPoint,cell)} /***平面90度旋转转换 * @param camerPoint* @param oldPoint* @param cell* @param dir* @return * */public static function rotationChange2(camerPoint:Point,oldPoint:Point,cell:CellGird45,dir:int=-1):Point{if(!Rectangle45.ARI_MODE)camerPoint=new Point;//var radian:Array=[dir*Math.PI*.5,Math.PI,3*Math.PI/2,2*Math.PI]var x:Number=oldPoint.x-camerPoint.x;var y:Number=oldPoint.y-camerPoint.yvar radius:Number=Math.sqrt(x*x+y*y)var angle2:Number=Math.atan2(x,y)var u:Number=Math.round(camerPoint.x+Math.sin(dir*Math.PI*.5+angle2) * radius);var v:Number=Math.round(camerPoint.y+Math.cos(dir*Math.PI*.5+angle2) * radius)var point:Point=new Point( u,v);return point}}} /***45度坐标系* 45度转换单位* code wxsr */package com.engine.data.referenceFrame45{import flash.geom.Point;import flash.net.registerClassAlias;public class CellGird45{private var _anglex:Number=Math.asin(1/Math.sqrt(5))private var _angley:Number=Math.asin(2/Math.sqrt(5))private var _origin:Point;private var _leftVertex:Point;private var _topVertex:Pointprivate var _rightVertex:Point;private var _bottonVertex:Point;private var _midpoint:Point;private var _cellSize:Numberprivate var _cellWidth:Numberprivate var _cellHeigth:Number;private var _key:String;private var _keyPoint:Pointprivate var _type:uint=1private var _color:uint=0xffff00;private var _lock:Boolean;public var source:String;//信息所属的元件idpublic var imgKey:Stringpublic function CellGird45(cellSize:Number=25,originPoint:Point=null){registerClassAlias("com.save.cellGird45",CellGird45)registerClassAlias("com.save.point",Point)this.cellSize=cellSizeif(originPoint==null){this.origin=new Point()}else {this.origin=originPoint;}}/***是否不可设置 * @return * */public function get lock():Boolean{return this._lock} public function set lock(val:Boolean):void{this._lock=val}/***类型 * @return * */public function get type():uint{return this._type}public function set type(val:uint):void{this._type=val}/***颜色 * @return * */public function get color():uint{return this._color}public function set color(val:uint):void{this._color=val}/***注册点 * @param point* */public function set origin(point:Point):void{this._origin=point;var x:Number=this._origin.x;var y:Number=this._origin.y;var size:Number=this._cellHeigthsize=24this._leftVertex=new Point(x,y+size/2)this._topVertex=new Point(x+size,y)this._rightVertex=new Point(x+size*2,y+size/2)this._bottonVertex=new Point(x+size,y+size)this._midpoint=new Point(x+size,y+size/2)}/***单元格大小 * @param val* */public function set cellSize(val :Number):void{this._cellWidth=(val*this._angley*2);this._cellHeigth=(this._cellWidth/2)this._cellSize=val}/***单元格宽度 * @param val* */public function set cellWidth (val:Number):void{this._cellWidth=(val);this._cellHeigth=val/2this._cellSize=val/this._angley*.5}/***单元格高度 * @param val* */public function set cellHeight(val:Number):void{this._cellWidth=(val*2);this._cellHeigth=valthis._cellSize=val/this._anglex*.5}/***索引点* @param point* */public function set keyPoint(point:Point):void{this._keyPoint=point;this._key=point.x+','+point.y}public function get keyPoint():Point{return this._keyPoint;}/***键值 * @return * */public function get key():String{return this._key;}public function get cellSize():Number{return this._cellSize}public function get cellWidth():Number{return this._cellWidth}public function get cellHeight():Number{return this._cellHeigth}public function get origin():Point{return this._origin;}public function get leftVertex():Point{return this._leftVertex;}/***顶部的点 * @return * */public function get topVertex():Point{return this._topVertex;}/***右边的点 * @return * */public function get rightVertex():Point{return this._rightVertex;}/***下方的点 * @return * */public function get bottonVertex():Point{return this._bottonVertex;}/***中间的点位置 * @return * */public function get midpoint():Point{return this._midpoint;}}}
?

读书人网 >编程

热点推荐