线段与矩形 是否相交 矩形与矩形

/*
   功能:判断线段和矩形是否相交 
   先判断线段的俩个端点是否在矩形的内部,在就必然相交
    
   其次判断线段的包围盒是否和矩形相交,不相交的话线段和矩形肯定也不相交   
    
   最后判断,矩形的四个顶点是否位于线段的两侧,是则必然相交,否则就不相交
*/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class LineConverttoRectangleClass
    {
        public class Point
        {
            public Point(float xx,float yy)
            {
                x = xx;
                y = yy;
            }
            public float x,y;
        }
        public class Line
        {
            public Line(Point pp1, Point pp2)
            {
                p1 = pp1;
                p2 = pp2;
            }
            public Point p1;
            public Point p2;
        }
        public class Rectangle
        {
            public Rectangle(Point pp1, Point pp2)
            {
                p1 = pp1;
                p2 = pp2;
            }
            public Point p1;
            public Point p2;
            public float MinX { get { return p1.x < p2.x ? p1.x : p2.x; } }
            public float MaxX { get { return p1.x > p2.x ? p1.x : p2.x; } }
            public float MinY { get { return p1.y < p2.y ? p1.y : p2.y; } }
            public float MaxY { get { return p1.y > p2.y ? p1.y : p2.y; } }
        };
        public static bool PointInsideRectangle(Point point,Rectangle rect)
        {
            if (point.x >= rect.MinX && point.x <= rect.MaxX)
                if (point.y >= rect.MinY && point.y <= rect.MaxY)
                    return true;
            return false;
        }
        // -1 点位于线段的左侧,0 点位于线段上面 ,1 点位于线段右侧
        public static int PointAtLineLeftRight(Point point,Line line)
        {
            Point vect1 = new Point((line.p1.x - point.x),(line.p1.y - point.y));
            Point vect2 = new Point((line.p2.x - point.x), (line.p2.y - point.y));

            float nRet = vect1.x * vect2.y - vect1.y * vect2.x;
            if (nRet == 0)
                return 0;
            else if (nRet > 0)
                return 1;
            else if (nRet < 0)
                return -1;
            return 0;
        }
        public static bool IsTwoLineIntersect(Line line1,Line line2)
        {
            int a = PointAtLineLeftRight(line1.p1,line2);
            int b = PointAtLineLeftRight(line1.p2,line2);
            if (a * b > 0)
                return false;

            a = PointAtLineLeftRight(line2.p1, line1);
            b = PointAtLineLeftRight(line2.p2, line1);
            if (a * b > 0)
                return false;
            return true;
        }
        public static bool IsLineIntersectRect(Line line,Rectangle rect)
        {
            if (PointInsideRectangle(line.p1, rect)
                || PointInsideRectangle(line.p2, rect))
                return true;

            if (PointInsideRectangle(new Point(line.p1.x, line.p2.y), rect)
                || PointInsideRectangle(new Point(line.p2.x, line.p1.y), rect))
                return true;

            if (IsTwoLineIntersect(line, new Line(rect.p1, new Point(rect.p1.x, rect.p2.y))))
                return true;
            if (IsTwoLineIntersect(line, new Line(rect.p1, new Point(rect.p2.x, rect.p1.y))))
                return true;

            if (IsTwoLineIntersect(line, new Line(rect.p2, new Point(rect.p1.x, rect.p2.y))))
                return true;
            if (IsTwoLineIntersect(line, new Line(rect.p2, new Point(rect.p2.x, rect.p1.y))))
                return true;
            return false;
        }
        public static bool LineConverttoRectangle(Line line,Rectangle rect)
        {
            if (PointInsideRectangle(line.p1, rect) 
                || PointInsideRectangle(line.p2, rect))
                return true;
            if (!IsLineIntersectRect(line,rect))
                return false;
            int b1 = PointAtLineLeftRight(rect.p1,line);
            int b2 = PointAtLineLeftRight(rect.p2, line);
            int b3 = PointAtLineLeftRight(new Point(rect.p1.x, rect.p2.y), line);
            int b4 = PointAtLineLeftRight(new Point(rect.p2.x, rect.p1.y), line);
            if (b1 + b2 + b3 + b4 == 4)
                return false;
            if (b1 + b2 + b3 + b4 == -4)
                return false;
            return true;
        }
        public static void Main()
        {
            Console.WriteLine(LineConverttoRectangle(new Line(new Point(5.0f, 1.8f), 
                new Point(5.0f, -15f)), new Rectangle(new Point(4.7f, -0.7f), new Point(6.7f, 1.4f))));
        }
    }
}
    public bool RectCrossRect(Rect r1, Rect r2, ref Vector2 start, ref Vector2 end)
    {
        float x, y, z, w; ;
        x = Mathf.Max(r1.xMin, r2.xMin);
        y = Mathf.Max(r1.yMin, r2.yMin);
        w = Mathf.Min(r1.xMax, r2.xMax);
        z = Mathf.Min(r1.yMax, r2.yMax);

        if (x >= w || y >= z)
            return false;
        start = new Vector2(x, y);
        end = new Vector2(w, z);
        return true;
    }





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值