线段 矩形 矩形与矩形 相交

[csharp]  view plain copy print ?
  1. /* 
  2.    功能:判断线段和矩形是否相交  
  3.    先判断线段的俩个端点是否在矩形的内部,在就必然相交 
  4.      
  5.    其次判断线段的包围盒是否和矩形相交,不相交的话线段和矩形肯定也不相交    
  6.      
  7.    最后判断,矩形的四个顶点是否位于线段的两侧,是则必然相交,否则就不相交 
  8. */  
[csharp]  view plain copy print ?
  1.   
[csharp]  view plain copy print ?
  1.   
[csharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConsoleApplication1  
  7. {  
  8.     class LineConverttoRectangleClass  
  9.     {  
  10.         public class Point  
  11.         {  
  12.             public Point(float xx,float yy)  
  13.             {  
  14.                 x = xx;  
  15.                 y = yy;  
  16.             }  
  17.             public float x,y;  
  18.         }  
  19.         public class Line  
  20.         {  
  21.             public Line(Point pp1, Point pp2)  
  22.             {  
  23.                 p1 = pp1;  
  24.                 p2 = pp2;  
  25.             }  
  26.             public Point p1;  
  27.             public Point p2;  
  28.         }  
  29.         public class Rectangle  
  30.         {  
  31.             public Rectangle(Point pp1, Point pp2)  
  32.             {  
  33.                 p1 = pp1;  
  34.                 p2 = pp2;  
  35.             }  
  36.             public Point p1;  
  37.             public Point p2;  
  38.             public float MinX { get { return p1.x < p2.x ? p1.x : p2.x; } }  
  39.             public float MaxX { get { return p1.x > p2.x ? p1.x : p2.x; } }  
  40.             public float MinY { get { return p1.y < p2.y ? p1.y : p2.y; } }  
  41.             public float MaxY { get { return p1.y > p2.y ? p1.y : p2.y; } }  
  42.         };  
  43.         public static bool PointInsideRectangle(Point point,Rectangle rect)  
  44.         {  
  45.             if (point.x >= rect.MinX && point.x <= rect.MaxX)  
  46.                 if (point.y >= rect.MinY && point.y <= rect.MaxY)  
  47.                     return true;  
  48.             return false;  
  49.         }  
  50.         // -1 点位于线段的左侧,0 点位于线段上面 ,1 点位于线段右侧  
  51.         public static int PointAtLineLeftRight(Point point,Line line)  
  52.         {  
  53.             Point vect1 = new Point((line.p1.x - point.x),(line.p1.y - point.y));  
  54.             Point vect2 = new Point((line.p2.x - point.x), (line.p2.y - point.y));  
  55.   
  56.             float nRet = vect1.x * vect2.y - vect1.y * vect2.x;  
  57.             if (nRet == 0)  
  58.                 return 0;  
  59.             else if (nRet > 0)  
  60.                 return 1;  
  61.             else if (nRet < 0)  
  62.                 return -1;  
  63.             return 0;  
  64.         }  
  65.         public static bool IsTwoLineIntersect(Line line1,Line line2)  
  66.         {  
  67.             int a = PointAtLineLeftRight(line1.p1,line2);  
  68.             int b = PointAtLineLeftRight(line1.p2,line2);  
  69.             if (a * b > 0)  
  70.                 return false;  
  71.   
  72.             a = PointAtLineLeftRight(line2.p1, line1);  
  73.             b = PointAtLineLeftRight(line2.p2, line1);  
  74.             if (a * b > 0)  
  75.                 return false;  
  76.             return true;  
  77.         }  
  78.         public static bool IsLineIntersectRect(Line line,Rectangle rect)  
  79.         {  
  80.             if (PointInsideRectangle(line.p1, rect)  
  81.                 || PointInsideRectangle(line.p2, rect))  
  82.                 return true;  
  83.   
  84.             if (PointInsideRectangle(new Point(line.p1.x, line.p2.y), rect)  
  85.                 || PointInsideRectangle(new Point(line.p2.x, line.p1.y), rect))  
  86.                 return true;  
  87.   
  88.             if (IsTwoLineIntersect(line, new Line(rect.p1, new Point(rect.p1.x, rect.p2.y))))  
  89.                 return true;  
  90.             if (IsTwoLineIntersect(line, new Line(rect.p1, new Point(rect.p2.x, rect.p1.y))))  
  91.                 return true;  
  92.   
  93.             if (IsTwoLineIntersect(line, new Line(rect.p2, new Point(rect.p1.x, rect.p2.y))))  
  94.                 return true;  
  95.             if (IsTwoLineIntersect(line, new Line(rect.p2, new Point(rect.p2.x, rect.p1.y))))  
  96.                 return true;  
  97.             return false;  
  98.         }  
  99.         public static bool LineConverttoRectangle(Line line,Rectangle rect)  
  100.         {  
  101.             if (PointInsideRectangle(line.p1, rect)   
  102.                 || PointInsideRectangle(line.p2, rect))  
  103.                 return true;  
  104.             if (!IsLineIntersectRect(line,rect))  
  105.                 return false;  
  106.             int b1 = PointAtLineLeftRight(rect.p1,line);  
  107.             int b2 = PointAtLineLeftRight(rect.p2, line);  
  108.             int b3 = PointAtLineLeftRight(new Point(rect.p1.x, rect.p2.y), line);  
  109.             int b4 = PointAtLineLeftRight(new Point(rect.p2.x, rect.p1.y), line);  
  110.             if (b1 + b2 + b3 + b4 == 4)  
  111.                 return false;  
  112.             if (b1 + b2 + b3 + b4 == -4)  
  113.                 return false;  
  114.             return true;  
  115.         }  
  116.         public static void Main()  
  117.         {  
  118.             Console.WriteLine(LineConverttoRectangle(new Line(new Point(5.0f, 1.8f),   
  119.                 new Point(5.0f, -15f)), new Rectangle(new Point(4.7f, -0.7f), new Point(6.7f, 1.4f))));  
  120.         }  
  121.     }  
  122. }  
[csharp]  view plain copy print ?
  1. <pre name="code" class="csharp">    public bool RectCrossRect(Rect r1, Rect r2, ref Vector2 start, ref Vector2 end)  
  2.     {  
  3.         float x, y, z, w; ;  
  4.         x = Mathf.Max(r1.xMin, r2.xMin);  
  5.         y = Mathf.Max(r1.yMin, r2.yMin);  
  6.         w = Mathf.Min(r1.xMax, r2.xMax);  
  7.         z = Mathf.Min(r1.yMax, r2.yMax);  
  8.   
  9.         if (x >= w || y >= z)  
  10.             return false;  
  11.         start = new Vector2(x, y);  
  12.         end = new Vector2(w, z);  
  13.         return true;  
  14.     }</pre><br>  
  15. <br>  
  16. <pre></pre>  
  17. <p><br>  
  18. </p>  
  19. <br>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值