判断线段和矩形是否相交

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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

本文转自jiahuafu博客园博客,原文链接http://www.cnblogs.com/jiahuafu/p/4024661.html如需转载请自行联系原作者


jiahuafu

相关文章
|
11月前
|
Ubuntu 安全 Linux
|
监控 C#
C# | 使用Chart动态展示实时折线图数据
实时折线图是展示数据变化趋势的有效方式,可以用于监控系统性能、物理实验、股票走势等多个领域。 在C#中,我们可以使用Chart控件来实现实时折线图的展示,其动态性和可交互性可以帮助用户更好地理解数据。 本文将介绍如何使用Chart控件展示实时折线图数据,希望能帮助读者快速掌握这个技能,应用于实际场景中。
1633 0
C# | 使用Chart动态展示实时折线图数据
halcon联合c#、WPF学习笔记二(简单案例)
halcon联合c#、WPF学习笔记二(简单案例)
1048 0
|
Java
离线安装jdk8
离线安装jdk8
377 0
|
3天前
|
人工智能 运维 安全
|
1天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
8天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
765 109