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; } /** <p>判斷線段是否在矩形內 </p> * 先看線段所在直線是否與矩形相交, * 如果不相交則返回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; } } } }
http://blog.sqstudio.com/as3/algorithm/842.html#codesyntax_1