判断点是否在直线上或线段上 判断线段相交 点与多边形的位置关系

package

{

	import flash.display.Sprite;

	import flash.geom.Point;

	

	public class Arr extends Sprite

	{



		private const INFINITY:Number = 1e10;

		private const ESP:Number = 1e-5;;

		private const MAX_N:int = 1000;

		private var Polygon:Array=new Array;

		

		public function Arr()

		{

			var p0:Point = new Point(120,150);

			var p1:Point = new Point(120,130);

			var p2:Point = new Point(130,130);

			var p3:Point = new Point(130,120);

			var p4:Point = new Point(120,120);

			var p5:Point = new Point(120,110);

			var p6:Point = new Point(130,110);

			var p7:Point = new Point(130,100);

			var p8:Point = new Point(150,100);

			var p9:Point = new Point(150,80);

			var p10:Point = new Point(120,80);

			var p11:Point = new Point(120,0);

			var p12:Point = new Point(100,0);

			var p13:Point = new Point(100,100);

			var p14:Point = new Point(0,100);

			var p15:Point = new Point(0, 150);

			

			var s:Sprite = new Sprite  ;

			addChild(s);

			s.graphics.lineStyle(1);

			s.graphics.moveTo(p0.x,p0.y);

			s.graphics.lineTo(p1.x,p1.y);

			s.graphics.lineTo(p2.x,p2.y);

			s.graphics.lineTo(p3.x,p3.y);

			s.graphics.lineTo(p4.x,p4.y);

			s.graphics.lineTo(p5.x,p5.y);

			s.graphics.lineTo(p6.x,p6.y);

			s.graphics.lineTo(p7.x,p7.y);

			s.graphics.lineTo(p8.x,p8.y);

			s.graphics.lineTo(p9.x,p9.y);

			s.graphics.lineTo(p10.x,p10.y);

			s.graphics.lineTo(p11.x,p11.y);

			s.graphics.lineTo(p12.x,p12.y);

			s.graphics.lineTo(p13.x,p13.y);

			s.graphics.lineTo(p14.x,p14.y);

			s.graphics.lineTo(p15.x,p15.y);

			s.graphics.lineTo(p0.x,p0.y);



			

			Polygon.push(p0);

			Polygon.push(p1);

			Polygon.push(p2);

			Polygon.push(p3);

			Polygon.push(p4);

			Polygon.push(p5);

			Polygon.push(p6);

			Polygon.push(p7);

			Polygon.push(p8);

			Polygon.push(p9);

			Polygon.push(p10);

			Polygon.push(p11);

			Polygon.push(p12);

			Polygon.push(p13);

			Polygon.push(p14);

			Polygon.push(p15);

			

			var X:int = 120;

			var Y:int = 130;

			/*var X:int = 130;

			var Y:int = 165;*/

			/*var X:int = 130;

			var Y:int = 145;*/

			/*var X:int = 120;

			var Y:int = 100;*/

			/*var X:int = 110;

			var Y:int = 100;*/

			/*var X:int = 120;

			var Y:int = 125;*/

			/*var X:int = 90;

			var Y:int = 125;*/

			//var X:int = 120;

			//var Y:int = 185;

			/*var X:int = 130;

			var Y:int = 125;*/

			/*var X:int = 120;

			var Y:int = 200;*/

			var checkpoint:Point = new Point(X, Y);

			var s1:Sprite = new Sprite  ;

			addChild(s1);

			s1.graphics.beginFill(0xff00ff,1);

			s1.graphics.drawCircle(X,Y,2);

			s1.graphics.endFill();



			var m:int = InPolygon(Polygon,checkpoint);

			trace("========="+m);

		}

		// 计算叉乘 |P0P1| × |P0P2|

		public function Multiply(p1:Point,p2:Point,p0:Point):Number

		{

			return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));

		}

		// 判断线段是否包含点point

		private function IsOnline(point:Point,line:LineSegment):Boolean

		{

			return( ( Math.abs(Multiply(line.pt1, line.pt2, point)) < ESP ) &&

			( ( point.x - line.pt1.x ) * ( point.x - line.pt2.x ) <= 0 ) &&

			( ( point.y - line.pt1.y ) * ( point.y - line.pt2.y ) <= 0 ) );

		}

		// 判断线段相交

		private  function Intersect(L1:LineSegment,L2:LineSegment):Boolean

		{

			return((Math.max(L1.pt1.x, L1.pt2.x) >= Math.min(L2.pt1.x, L2.pt2.x)) &&

			(Math.max(L2.pt1.x, L2.pt2.x) >= Math.min(L1.pt1.x, L1.pt2.x)) &&

			(Math.max(L1.pt1.y, L1.pt2.y) >= Math.min(L2.pt1.y, L2.pt2.y)) &&

			(Math.max(L2.pt1.y, L2.pt2.y) >= Math.min(L1.pt1.y, L1.pt2.y)) &&

			(Multiply(L2.pt1, L1.pt2, L1.pt1) * Multiply(L1.pt2, L2.pt2, L1.pt1) >= 0) &&

			(Multiply(L1.pt1, L2.pt2, L2.pt1) * Multiply(L2.pt2, L1.pt2, L2.pt1) >= 0)

			);

		}

    //线段pp1与数组arr组成的线段是否相交

		public function segmentIntersect(arr:Array,p:Point,p1:Point):LineSegment

		{

			var n:int = arr.length;

			var line:LineSegment = new LineSegment();

		   

			line.pt1 = p;

			line.pt2 = p1;

			//排除数组最后一点和第一点组成线段i<n-1就行了

			for (var i:int = 0; i < n; i++ ) 

			{

				var side:LineSegment = new LineSegment();



				side.pt1 = arr[i];

				side.pt2 = arr[(i + 1) % n];

				

				if ( Intersect(side,line) ) 

				{

					//trace("----相交了----");

					return side;

				}

			}

			return null;

		}  		

		/* 

		 * 射线法判断点q与多边形polygon的位置关系,

		 * 要求polygon为简单多边形,

		 * 顶点逆时针排列如果点在多边形内:返回1

		 * 如果点在多边形边上:返回0

		 * 如果点在多边形外:返回-1

		 */

		public function InPolygon(polygon:Array,point:Point):int

		{

			var n:int = polygon.length;

			var count:int = 0;

			var line:LineSegment = new LineSegment();

		   

			line.pt1 = point;

			line.pt2.y = point.y;

			line.pt2.x = - INFINITY;

			for (var i:int = 0; i < n; i++ ) 

			{

				// 得到多边形的一条边

				var side:LineSegment = new LineSegment();

				side.pt1 = polygon[i];

				side.pt2 = polygon[(i + 1) % n];

				if (IsOnline(point, side)) 

				{

					trace("在多边形上");

					return 0 ;

				}

				// 如果side平行x轴则不作考虑

				if ( Math.abs(side.pt1.y - side.pt2.y) < ESP )

				{

					continue;

				}

				if ( IsOnline(side.pt1, line) ) 

				{

					if( side.pt1.y > side.pt2.y )

					{

						count++;

					}

				} 

				else if ( IsOnline(side.pt2, line) ) 

				{

					if( side.pt2.y > side.pt1.y )

					{ 

						count++;

					}

				} 

				else if ( Intersect(line, side) ) 

				{

					count++;

				}

			}

			if ( count % 2 == 1 ) 

			{

				trace("在多边形内");

				return 1;

			}

			else 

			{

				trace("在多边形外");

				return -1;

			}

		}

	}

}



	import flash.geom.Point;

	class LineSegment

	{

    	public var pt1:Point;

    	public var pt2:Point;

    	public function LineSegment()

    	{

    		this.pt1 = new Point();

    		this.pt2 = new Point();

    	}

	}





你可能感兴趣的:(判断)