博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
判断线段和矩形是否相交
阅读量:4080 次
发布时间:2019-05-25

本文共 3479 字,大约阅读时间需要 11 分钟。

package{	import flash.display.Sprite;	import flash.events.MouseEvent;	import flash.text.TextField; 	[SWF(width=375,height=300,backgroundColor="0xeeeeee")]	public class RectLine extends Sprite	{		private var txt:TextField;		public function RectLine()		{ 			this.txt = new TextField();			addChild(txt); 			stage.addEventListener(MouseEvent.CLICK,ckHandler);			ckHandler(null);		} 		private function ckHandler(e:MouseEvent):void		{			graphics.clear();			graphics.beginFill(0x00ffff);			graphics.drawRect(100,100,200,50);			graphics.endFill(); 			var a:int = Math.random()*375;			var b:int = Math.random()*300;			var c:int = Math.random()*375;			var d:int = Math.random()*300; 			graphics.lineStyle(2,0xff0000);			graphics.moveTo(a,b);			graphics.lineTo(c,d);			trace(isLineIntersectRectangle(a,b,c,d,100,100,300,150));			var isF:Boolean = isLineIntersectRectangle(a,b,c,d,100,100,300,150);			txt.text = "是否相交:"+isF;		}		 		/** 

判断线段是否在矩形内

* 先看线段所在直线是否与矩形相交, * 如果不相交则返回false, * 如果相交, * 则看线段的两个点是否在矩形的同一边(即两点的x(y)坐标都比矩形的小x(y)坐标小,或者大), * 若在同一边则返回false, * 否则就是相交的情况。 * @param linePointX1 线段起始点x坐标 * @param linePointY1 线段起始点y坐标 * @param linePointX2 线段结束点x坐标 * @param linePointY2 线段结束点y坐标 * @param rectangleLeftTopX 矩形左上点x坐标 * @param rectangleLeftTopY 矩形左上点y坐标 * @param rectangleRightBottomX 矩形右下点x坐标 * @param rectangleRightBottomY 矩形右下点y坐标 * @return 是否相交 */ private function isLineIntersectRectangle(linePointX1:Number, linePointY1:Number, linePointX2:Number, linePointY2:Number, rectangleLeftTopX:Number, rectangleLeftTopY:Number, rectangleRightBottomX:Number, rectangleRightBottomY:Number):Boolean { var lineHeight:Number = linePointY1 - linePointY2; var lineWidth:Number = linePointX2 - linePointX1; // 计算叉乘 var c:Number = linePointX1 * linePointY2 - linePointX2 * linePointY1; if ((lineHeight * rectangleLeftTopX + lineWidth * rectangleLeftTopY + c >= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleRightBottomY + c <= 0) || (lineHeight * rectangleLeftTopX + lineWidth * rectangleLeftTopY + c <= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleRightBottomY + c >= 0) || (lineHeight * rectangleLeftTopX + lineWidth * rectangleRightBottomY + c >= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleLeftTopY + c <= 0) || (lineHeight * rectangleLeftTopX + lineWidth * rectangleRightBottomY + c <= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleLeftTopY + c >= 0)) { if (rectangleLeftTopX > rectangleRightBottomX) { var temp:Number = rectangleLeftTopX; rectangleLeftTopX = rectangleRightBottomX; rectangleRightBottomX = temp; } if (rectangleLeftTopY < rectangleRightBottomY) { var temp1:Number = rectangleLeftTopY; rectangleLeftTopY = rectangleRightBottomY; rectangleRightBottomY = temp1; } if ((linePointX1 < rectangleLeftTopX && linePointX2 < rectangleLeftTopX) || (linePointX1 > rectangleRightBottomX && linePointX2 > rectangleRightBottomX) || (linePointY1 > rectangleLeftTopY && linePointY2 > rectangleLeftTopY) || (linePointY1 < rectangleRightBottomY && linePointY2 < rectangleRightBottomY)) { return false; } else { return true; } } else { return false; } } }}

posted on 2014-10-14 16:22 阅读(...) 评论(...)

转载地址:http://lfvni.baihongyu.com/

你可能感兴趣的文章
二叉树深度优先遍历和广度优先遍历
查看>>
生产者消费者模型,循环队列实现
查看>>
PostgreSQL代码分析,查询优化部分,process_duplicate_ors
查看>>
PostgreSQL代码分析,查询优化部分,canonicalize_qual
查看>>
PostgreSQL代码分析,查询优化部分,pull_ands()和pull_ors()
查看>>
IA32时钟周期的一些内容
查看>>
获得github工程中的一个文件夹的方法
查看>>
《PostgreSQL技术内幕:查询优化深度探索》养成记
查看>>
PostgreSQL查询优化器详解之逻辑优化篇
查看>>
STM32中assert_param的使用
查看>>
C语言中的 (void*)0 与 (void)0
查看>>
vu 是什么
查看>>
io口的作用
查看>>
IO口的作用
查看>>
UIView的使用setNeedsDisplay
查看>>
归档与解归档
查看>>
Window
查看>>
为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性
查看>>
字符串的截取
查看>>
2. Add Two Numbers
查看>>