关闭

AS3 算法,菱形分8份三角形,检测点在哪个个三角形中

标签: 算法functionimportclass扩展c
1132人阅读 评论(0) 收藏 举报
分类:

今天,由于公事,研究了一下,算法:


简单的三角形类:

package
{
	import flash.display.Sprite;
	import flash.geom.Point;

	/**
	 * 三角形
	 * @author Jave.Lin
	 */	
	public class Triangle extends Sprite
	{
		private var _p1:Point;
		private var _p2:Point;
		private var _p3:Point;
		private var _c:uint;
		
		public function get p1():Point{return _p1;}
		public function get p2():Point{return _p2;}
		public function get p3():Point{return _p3;}
		public function get c():uint{return _c;}
		
		public function set p1(value:Point):void
		{
			if(value.x!=_p1.x && value.y!=_p1.y)
			{
				_p1=value;
				redraw();
			}
		}
		public function set p2(value:Point):void
		{
			if(value.x!=_p2.x && value.y!=_p2.y)
			{
				_p2=value;
				redraw();
			}
		}
		public function set p3(value:Point):void
		{
			if(value.x!=_p3.x && value.y!=_p3.y)
			{
				_p3=value;
				redraw();
			}
		}
		
		public function Triangle($p1:Point,$p2:Point,$p3:Point,$c:uint=0xff0000)
		{
			super();
			setPointsRedraw($p1,$p2,$p3,$c);
		}
		
		public function setPointsRedraw($p1:Point,$p2:Point,$p3:Point,$c:uint=0xff0000):void
		{
			_p1=$p1;
			_p2=$p2;
			_p3=$p3;
			_c=$c;
			redraw();
		}
		
		private function redraw():void
		{
			// clear
			this.graphics.clear();
			// begine and set style
			this.graphics.beginFill(_c,0.5);
			this.graphics.lineStyle(1,_c,0.5);
			// start position
			this.graphics.moveTo(_p1.x,_p1.y);
			
			// draw lines
			this.graphics.lineTo(_p2.x,_p2.y);
			this.graphics.lineTo(_p3.x,_p3.y);
			this.graphics.lineTo(_p1.x,_p1.y);
			
			// end draw
			this.graphics.endFill();
		}
	}
}

菱形类:

package
{
	import flash.display.Sprite;
	/**
	 * 菱形类
	 * @author Jave.Lin
	 */
	public class Diamond extends Sprite
	{
		protected var _xR:Number;
		protected var _yR:Number;
		
		public function get xR():Number{return _xR;}
		public function set xR(value:Number):void
		{
			if(value!=_xR)
			{
				_xR=value;
				redraw();
			}
		}
		
		public function get yR():Number{return _yR;}
		public function set yR(value:Number):void
		{
			if(value!=_yR)
			{
				_yR=value;
				redraw();
			}
		}
		
		public function Diamond($xR:Number,$yR:Number)
		{
			setXYRAndRedraw($xR,$yR);
		}
		
		public function setXYRAndRedraw($xR:Number,$yR:Number):void
		{
			_xR=$xR;
			_yR=$yR;
			redraw();
		}
		
		private function redraw():void
		{
			// clear
			this.graphics.clear();
			// begine and set style
			this.graphics.beginFill(0xff0000,0.5);
			this.graphics.lineStyle(1,0xff0000,0.5);
			// start position
			this.graphics.moveTo(_xR,0);
			
			// draw lines
			this.graphics.lineTo(0,_yR);
			this.graphics.lineTo(-_xR,0);
			this.graphics.lineTo(0,-_yR);
			this.graphics.lineTo(_xR,0);
			
			// end draw
			this.graphics.endFill();
		}
	}
}

菱形扩展类:

package
{
	import flash.geom.Point;
	import flash.utils.getTimer;

	/**
	 * 菱形的一个扩展类
	 * @author Jave.Lin
	 */	
	public class DiamondExt extends Diamond
	{
		private var _triangles:Vector.<Triangle>=new Vector.<Triangle>();
		private var _angle1:Number;
		private var _angle2:Number;
		
		public function DiamondExt($xR:Number, $yR:Number)
		{
			super($xR, $yR);
			generateTriangle();
		}
		
		private function generateTriangle():void
		{
//			_angle1=Math.abs(Math.tan(_yR/_xR));//有误差?
			_angle1=Math.abs(Math.atan2((-_yR-0),((0-(-_xR)))));
			if(_angle1>(Math.PI/2))_angle1-=(Math.PI/2);
			
			_angle2=Math.PI/2-_angle1;
			
			_triangles[0]=new Triangle(
				new Point(0,0),new Point(_xR,0),new Point(_xR/2,_yR/2));
			_triangles[1]=new Triangle(
				new Point(0,0),new Point(_xR/2,_yR/2),new Point(0,_yR));
			_triangles[2]=new Triangle(
				new Point(0,0),new Point(0,_yR),new Point(-(_xR/2),_yR/2));
			_triangles[3]=new Triangle(
				new Point(0,0),new Point(-(_xR/2),_yR/2),new Point(-_xR,0));
			_triangles[4]=new Triangle(
				new Point(0,0),new Point(-_xR,0),new Point(-(_xR/2),-(_yR/2)));
			_triangles[5]=new Triangle(
				new Point(0,0),new Point(0,-_yR),new Point(-(_xR/2),-(_yR/2)));
			_triangles[6]=new Triangle(
				new Point(0,0),new Point(_xR/2,-(_yR/2)),new Point(0,-_yR));
			_triangles[7]=new Triangle(
				new Point(0,0),new Point(_xR,0),new Point(_xR/2,-(_yR/2)));
				
			for (var i:int = 0; i < _triangles.length; i++) 
			{
				addChild(_triangles[i]);
			}
		}
		
		public function checkPointAndLightTheTriangle(point:Point):void
		{
			var tempA:Number=Math.atan2(point.y,point.x);
			
			resetTriangles();
			if(tempA>=0 && tempA<_angle1)
			{
				_triangles[0].alpha=0.5;
			}
			else if(tempA>=_angle1 && tempA<(_angle1+_angle2))
			{
				_triangles[1].alpha=0.5;
			}
			else if(tempA>=Math.PI/2 && tempA<(Math.PI/2+_angle2))
			{
				_triangles[2].alpha=0.5;
			}
			else if(tempA>=(Math.PI/2+_angle2) && tempA<Math.PI)
			{
				_triangles[3].alpha=0.5;
			}
			else if(tempA>=-Math.PI && tempA<(-Math.PI+_angle1))
			{
				_triangles[4].alpha=0.5;
			}
			else if(tempA>=(-Math.PI+_angle1) && tempA<-Math.PI/2)
			{
				_triangles[5].alpha=0.5;
			}
			else if(tempA>=-Math.PI/2 && tempA<-_angle1)
			{
				_triangles[6].alpha=0.5;
			}
			else if(tempA>=-_angle1 && tempA<0)
			{
				_triangles[7].alpha=0.5;
			}
		}
		
		public function testLargeNumSummaryPerformancea(point:Point,num:int=100000):void
		{
			var num1Right:Number=_angle1+_angle2;
			var num2Right:Number=Math.PI/2+_angle2;
			var num3Right:Number=Math.PI;
			var num4Left:Number=-Math.PI;
			var num4Right:Number=-Math.PI+_angle1;
			var num5Right:Number=-Math.PI/2;
			var num6Right:Number=-_angle1;
			
			var startTime:int=getTimer();
			var triangleNum:int=0;
			var tempA:Number=Math.atan2(point.y,point.x);
			
			for (var i:int = 0; i < num; i++) 
			{
				if(tempA>=0 && tempA<_angle1)
				{
					triangleNum=0;
				}
				else if(tempA>=_angle1 && tempA<num1Right)
				{
					triangleNum=1;
				}
				else if(tempA>=num1Right && tempA<num2Right)
				{
					triangleNum=2;
				}
				else if(tempA>=num2Right && tempA<num3Right)
				{
					triangleNum=3;
				}
				else if(tempA>=num4Left && tempA<num4Right)
				{
					triangleNum=4;
				}
				else if(tempA>=num4Right && tempA<num5Right)
				{
					triangleNum=5;
				}
				else if(tempA>=num5Right && tempA<num6Right)
				{
					triangleNum=6;
				}
				else if(tempA>=num6Right && tempA<0)
				{
					triangleNum=7;
				}
			}
			
			var endTime:int=getTimer();
			
			trace("run "+num+" times elpasedTime:"+(endTime-startTime));
			// run 1000000 times elpasedTime : 26 ms 左右
		}
		
		public function resetTriangles():void
		{
			for (var i:int = 0; i < _triangles.length; i++) 
			{
				_triangles[i].alpha=1;
			}
		}
	}
}

应用示例:

package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class DiamondTest extends Sprite
	{
		private var _diamond:Diamond;
		private var _triangle:Triangle;
		private var _diamondExt:DiamondExt;
		
		public function DiamondTest()
		{
			super();
			
			_diamond=new Diamond(50,25);
			addChild(_diamond);
			
			_diamond.x=200;
			_diamond.y=200;
			
			_diamond.addEventListener(MouseEvent.MOUSE_OVER,onDiamondOverHandler);
			_diamond.addEventListener(MouseEvent.MOUSE_OUT,onDiamondOutHandler);
			
			_triangle=new Triangle(new Point(0,0),new Point(25,-25),new Point(-25,-25),0x00ffff);
			addChild(_triangle);
			
			_triangle.x=50;
			_triangle.y=200;
			
			_diamondExt=new DiamondExt(50,25);
			addChild(_diamondExt);
			
			_diamondExt.x=400;
			_diamondExt.y=200;
			
			_diamondExt.addEventListener(MouseEvent.MOUSE_MOVE,onDiamondExtMoveHandler);
			_diamondExt.addEventListener(MouseEvent.MOUSE_OUT,onDiamondExtOutHandler);
		}
		
		private function onDiamondExtOutHandler(e:MouseEvent):void
		{
			_diamondExt.resetTriangles();
		}
		
		private function onDiamondExtMoveHandler(e:MouseEvent):void
		{
			_diamondExt.checkPointAndLightTheTriangle(new Point(mouseX-_diamondExt.x,mouseY-_diamondExt.y));
			_diamondExt.testLargeNumSummaryPerformancea(new Point(mouseX-_diamondExt.x,mouseY-_diamondExt.y));//,1000000);
		}
		
		private function onDiamondOverHandler(e:MouseEvent):void
		{
			trace("over");
		}
		
		private function onDiamondOutHandler(e:MouseEvent):void
		{
			trace("out");
		}
	}
}


设计前草稿图:



运行效果,用鼠标经过带有三角形的菱形,分别移动不同的三角形位置上,即可看到效果。

可以把这个扩展的菱形,长度弄大一些。

显示全图


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类
    最新评论