概述
/*
功能:判断线段和矩形是否相交
先判断线段的俩个端点是否在矩形的内部,在就必然相交
其次判断线段的包围盒是否和矩形相交,不相交的话线段和矩形肯定也不相交
最后判断,矩形的四个顶点是否位于线段的两侧,是则必然相交,否则就不相交
*/
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; }
最后
以上就是忧郁钻石为你收集整理的线段与矩形 是否相交 矩形与矩形的全部内容,希望文章能够帮你解决线段与矩形 是否相交 矩形与矩形所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复