计算两条线的交点,以及判断一个点是否在直线上

写的小例子,备份
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" creationComplete="init()">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			
			//存放鼠标电击的点,最多存放2个
			private var clickPoint:Array = [];
			//存放画线的点.
			private var pointArray:Array = [];
			
			
			private function init():void{
				

			}
			
			//判断p是否在p1,p2组成的线段上.
			private function isOnLine(p:Point,p1:Point,p2:Point):Boolean{
				//根据数学,求出直接的表达示:y=kx+b
				var k:Number = (p1.y-p2.y)/(p1.x-p2.x);
				var b:Number = p1.y-k*p1.x;
				
				//将点的x坐标代入表达示中,判断该点是否在直线上
				var py:Number = p.x*k+b;
				var value:Number = p.y-py;
				
				if(value>-0.01&&value<0.01){
					return true;
				}else{
					return false;
				}
			}
			
			//画布区域的点击事件
			private function click(event:MouseEvent):void{
				var p:Point = new Point(event.localX,event.localY);
				//存放鼠标点击的坐标
				clickPoint.push(p);
				if(clickPoint.length>1){
					var length:int = clickPoint.length;
					startPoint.text = clickPoint[length-2].x + "," +clickPoint[length-2].y;
					endPoint.text = p.x + "," +p.y;
					
					if(length>2){
						clickPoint.shift();
					}
				}else{
					startPoint.text = p.x + "," +p.y;
				}
				
//				var p:Point = new Point(event.localX,event.localY);
//				
//				var p1:Point = new Point(0,0);
//				var p2:Point = new Point(100,100);
//				
//				
//				var result:Boolean = isOnLine(p,p1,p2);
//				
//				trace(p.x+","+p.y);
//				trace(result);
				//Alert.show(result.toString());
			}
			
			//计算p1,p2组成的线段和p3,p4组成的线段的交点
			private function getHitPoint(p1:Point,p2:Point,p3:Point,p4:Point):Point{
				//根据数学,求出直接的表达示:y=kx+b
				var p:Point = null;
				
				//两条线均不与Y轴平行
				if(p1.x!=p2.x&&p3.x!=p4.x){
					var k1:Number = (p1.y-p2.y)/(p1.x-p2.x);
					var b1:Number = p1.y-k1*p1.x;
					
					var k2:Number = (p3.y-p4.y)/(p3.x-p4.x);
					var b2:Number = p3.y-k2*p3.x;
					
					//两条线平行
					if(k1 == k2) return null;
					
					p = new Point;
					p.x = -(b1-b2)/(k1-k2);
					p.y = (k1*b2-k2*b1)/(k1-k2);
					
				}else{
					var k:Number;
					var b:Number;
					//经过p1,p2的直线与y轴平行,
					if(p1.x == p2.x && p3.x!=p4.x){
						k = (p3.y-p4.y)/(p3.x-p4.x);
						b = p3.y-k*p3.x;
						
						p = new Point;
						p.x = p1.x;
						p.y = k*p.x+b;
						
					}else if(p1.x != p2.x){
						k = (p1.y-p2.y)/(p1.x-p2.x);
						b = p1.y-k*p1.x;
						
						p = new Point;
						p.x = p3.x;
						p.y = k*p.x+b;
						
					}
				}
				
				//如果两条线段没有相交,延长线相交则返回null
				if(p){
					if(p.x<Math.min(p1.x,p2.x)||p.x>Math.max(p1.x,p2.x)||p.x<Math.min(p3.x,p4.x)||p.x>Math.max(p3.x,p4.x)||
							p.y<Math.min(p1.y,p2.y)||p.y>Math.max(p1.y,p2.y)||p.y<Math.min(p3.y,p4.y)||p.y>Math.max(p3.y,p4.y)){
						return null;
					}
				}
				
				return p;
			}
			
			private function drawLine(event:MouseEvent):void{
				if(clickPoint.length>1){
					createLine(clickPoint[0],clickPoint[1]);
					if(pointArray.length == 0){
						line1.text = "("+clickPoint[0].x+","+clickPoint[0].y+"),"+"("+clickPoint[1].x+","+clickPoint[1].y+")";
					}else{
						line2.text = "("+clickPoint[0].x+","+clickPoint[0].y+"),"+"("+clickPoint[1].x+","+clickPoint[1].y+")";
					}
					pointArray = pointArray.concat(clickPoint);
					clickPoint = [];
				}
			}
			
			//根据两点,画线
			private function createLine(p1:Point, p2:Point):void{
				canvas.graphics.lineStyle(2,0xFF0000,1);
				canvas.graphics.moveTo(p1.x,p1.y);
				canvas.graphics.lineTo(p2.x,p2.y);
			}
			
			//清除线
			private function clearLine(event:MouseEvent):void{
				clickPoint = [];
				pointArray = [];
				startPoint.text = "";
				endPoint.text = "";
				line1.text = "";
				line2.text = "";
				crossPoint.text = "";
				canvas.graphics.clear();
			}
			
			private function getPoint(event:MouseEvent):void{
				if(pointArray.length == 4){
					var point:Point = getHitPoint(pointArray[0],pointArray[1],pointArray[2],pointArray[3]);
					if(point){
						crossPoint.text = "("+point.x+","+point.y+")";
					}else{
						crossPoint.text = "无交点";
					}
				}
			}
			
		]]>
	</mx:Script>
	<mx:Canvas height="30">
		<mx:Text text="起点:" x="0" width="40"/>
		<mx:TextInput id="startPoint" x="45" width="40" editable="false"/>
		<mx:Text text="终点:" x="85" width="40"/>
		<mx:TextInput id="endPoint" x="130" width="40" editable="false"/>
		<mx:Button id="btn" x="200" label="画线" click="drawLine(event)" width="60"/>
		<mx:Button id="clear" x="280" label="清除" click="clearLine(event)" width="60"/>
	</mx:Canvas>
	<mx:Canvas y="40" height="30">
		<mx:Text text="线1:" x="0" width="40"/>
		<mx:TextInput id="line1" x="45" width="120" editable="false"/>
		<mx:Text text="线2:" x="170" width="40"/>
		<mx:TextInput id="line2" x="215" width="120" editable="false"/>
		<mx:Button x="340" label="求交点" click="getPoint(event)" width="60"/>
		<mx:Text text="焦点:" x="405" width="40"/>
		<mx:TextInput id="crossPoint" x="450" width="80" editable="false"/>
	</mx:Canvas>
	<mx:Canvas y="80" id="canvas" width="200" height="200" click="click(event)" borderStyle="solid"/>
</mx:Application>

你可能感兴趣的:(计算)