/// <summary>
/// 判断线段的方向 1 由上到下 2 由下到上, 3 由左到右, 4由右到左\
/// </summary>
/// <param name="points"> 起始点</param>
/// <returns></returns>
public int GetOrientation(List<Point> points)
{
/// 判断线段的方向。 1 由上到下 2 由下到上, 3 由左到右, 4由右到左\
int type = -1;
Point lineSPoint = points[0]; // 第一段线 线段 开始点
Point lineEPoint = points[1]; // 第一段线 线段结束
if ((lineSPoint.X - lineEPoint.X) > 0)
{
type = 4;
}
else if ((lineSPoint.X - lineEPoint.X) < 0)
{
type = 3;
}
else
{
if ((lineSPoint.Y - lineEPoint.Y) > 0)
{
//如果开始点的 y方向
type = 2;
}
else if ((lineSPoint.Y - lineEPoint.Y) < 0)
{
type = 1;
}
}
return type;
}
/// <summary>
/// 线段与矩形是否相交
/// </summary>
/// <param name="startLineX">直线坐标起点X</param>
/// <param name="startLineY">直线坐标起点Y</param>
/// <param name="endLineX">直线坐标终点X</param>
/// <param name="endLineY">直线坐标终点Y</param>
/// <param name="rectangleLeftTopX">矩形 x</param>
/// <param name="rectangleLeftTopY">矩形 Y</param>
/// <param name="rectangleRightBottomX">X长度</param>
/// <param name="rectangleRightBottomY"> Y长度</param>
/// <param name="sDoc"> 开始点是否在矩形内部</param>
/// <param name="eDoc">结束点 是否在矩形内部</param>
/// <returns></returns>
public static bool isLineIntersectRectangle(float startLineX, float startLineY, float endLineX, float endLineY, float rectangleLeftTopX, float rectangleLeftTopY, float rectangleRightBottomX, float rectangleRightBottomY, out bool sDoc, out bool eDoc)
{
sDoc = isDocIntersectRectangle(startLineX, startLineY, rectangleLeftTopX, rectangleLeftTopY, rectangleRightBottomX, rectangleRightBottomY);
eDoc = isDocIntersectRectangle(endLineX, endLineY, rectangleLeftTopX, rectangleLeftTopY, rectangleRightBottomX, rectangleRightBottomY);
float lineHeight = startLineY - endLineY;
float lineWidth = endLineX - startLineX; // 计算叉乘
float c = startLineX * endLineY - endLineX * startLineY;
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)
{
float temp = rectangleLeftTopX;
rectangleLeftTopX = rectangleRightBottomX;
rectangleRightBottomX = temp;
}
if (rectangleLeftTopY < rectangleRightBottomY)
{
float temp1 = rectangleLeftTopY;
rectangleLeftTopY = rectangleRightBottomY;
rectangleRightBottomY = temp1;
}
if ((startLineX < rectangleLeftTopX && endLineX < rectangleLeftTopX)
|| (startLineX > rectangleRightBottomX && endLineX > rectangleRightBottomX)
|| (startLineY > rectangleLeftTopY && endLineY > rectangleLeftTopY)
|| (startLineY < rectangleRightBottomY && endLineY < rectangleRightBottomY))
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
/// <summary>
/// 判断 点位是否在矩形内部
/// </summary>
/// <param name="startLineX">开始坐标X </param>
/// <param name="startLineY">结束坐标X</param>
/// <param name="rectangleLeftTopX">矩形 x</param>
/// <param name="rectangleLeftTopY">矩形 Y</param>
/// <param name="rectangleRightBottomX">X长度</param>
/// <param name="rectangleRightBottomY"> Y长度</param>
/// <returns></returns>
public static bool isDocIntersectRectangle(float startLineX, float startLineY, float rectangleLeftTopX, float rectangleLeftTopY, float rectangleRightBottomX, float rectangleRightBottomY)
{
bool result = false;
if (startLineX <= rectangleRightBottomX && startLineX >= rectangleLeftTopX && startLineY >= rectangleLeftTopY && startLineY <= rectangleRightBottomY)
{
result = true;
}
return result;
}
/// <summary>
/// 计算两条直线的交点 弃用 计算不是很精准
/// </summary>
/// <param name="lineFirstStar">L1的点1坐标</param>
/// <param name="lineFirstEnd">L1的点2坐标</param>
/// <param name="lineSecondStar">L2的点1坐标</param>
/// <param name="lineSecondEnd">L2的点2坐标</param>
/// <returns></returns>
public static PointF GetIntersection(PointF lineFirstStar, PointF lineFirstEnd, PointF lineSecondStar, PointF lineSecondEnd)
{
/*
* L1,L2都存在斜率的情况:
* 直线方程L1: ( y - y1 ) / ( y2 - y1 ) = ( x - x1 ) / ( x2 - x1 )
* => y = [ ( y2 - y1 ) / ( x2 - x1 ) ]( x - x1 ) + y1
* 令 a = ( y2 - y1 ) / ( x2 - x1 )
* 有 y = a * x - a * x1 + y1 .........1
* 直线方程L2: ( y - y3 ) / ( y4 - y3 ) = ( x - x3 ) / ( x4 - x3 )
* 令 b = ( y4 - y3 ) / ( x4 - x3 )
* 有 y = b * x - b * x3 + y3 ..........2
*
* 如果 a = b,则两直线平等,否则, 联解方程 1,2,得:
* x = ( a * x1 - b * x3 - y1 + y3 ) / ( a - b )
* y = a * x - a * x1 + y1
*
* L1存在斜率, L2平行Y轴的情况:
* x = x3
* y = a * x3 - a * x1 + y1
*
* L1 平行Y轴,L2存在斜率的情况:
* x = x1
* y = b * x - b * x3 + y3
*
* L1与L2都平行Y轴的情况:
* 如果 x1 = x3,那么L1与L2重合,否则平等
*
*/
float a = 0, b = 0;
int state = 0;
if (lineFirstStar.X != lineFirstEnd.X)
{
a = (lineFirstEnd.Y - lineFirstStar.Y) / (lineFirstEnd.X - lineFirstStar.X);
state |= 1;
}
if (lineSecondStar.X != lineSecondEnd.X)
{
b = (lineSecondEnd.Y - lineSecondStar.Y) / (lineSecondEnd.X - lineSecondStar.X);
state |= 2;
}
switch (state)
{
case 0: //00
{
if (lineFirstStar.X == lineSecondStar.X)
{
//throw new Exception("两条直线互相重合,且平行于Y轴,无法计算交点。");
return new PointF(0, 0);
}
else
{
//throw new Exception("两条直线互相平行,且平行于Y轴,无法计算交点。");
return new PointF(0, 0);
}
}
case 1: //L1存在斜率, L2平行Y轴
{
float x = lineSecondStar.X;
float y = (lineFirstStar.X - x) * (-a) + lineFirstStar.Y;
return new PointF(x, y);
}
case 2: //L1 平行Y轴,L2存在斜率
{
float x = lineFirstStar.X;
//网上有相似代码的,这一处是错误的。你可以对比case 1 的逻辑 进行分析
//源code:lineSecondStar * x + lineSecondStar * lineSecondStar.X + p3.Y;
float y = (lineSecondStar.X - x) * (-b) + lineSecondStar.Y;
return new PointF(x, y);
}
case 3: //L1,L2都存在斜率
{
if (a == b)
{
// throw new Exception("两条直线平行或重合,无法计算交点。");
return new PointF(0, 0);
}
float x = (a * lineFirstStar.X - b * lineSecondStar.X - lineFirstStar.Y + lineSecondStar.Y) / (a - b);
float y = a * x - a * lineFirstStar.X + lineFirstStar.Y;
return new PointF(x, y);
}
}
// throw new Exception("不可能发生的情况");
return new PointF(0, 0);
}
#region 两条线段是否正交 判断!
/// <summary>
/// 比较算法
/// </summary>
/// <param name="f1"></param>
/// <param name="f2"></param>
/// <returns></returns>
public bool Equal(float f1, float f2)
{
return (Math.Abs(f1 - f2) < 1f);
}
/// <summary>
/// 比较两点坐标大小,先比较x坐标,若相同则比较y坐标
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public bool dayu(Point p1, Point p2)比较两点坐标大小,先比较x坐标,若相同则比较y坐标
{
return (p1.X > p2.X || (Equal(p1.X, p2.X) && p1.Y > p2.Y));
}
/// <summary>
/// 判断两点是否相等
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public bool dengyu(Point p1, Point p2)判断两点是否相等
{
return (Equal(p1.X, p2.X) && Equal(p1.Y, p2.Y));
}
/// <summary>
/// 计算两向量外积
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public float ji(Point p1, Point p2)计算两向量外积
{
return (p1.X * p2.Y - p1.Y * p2.X);
}
//判定两线段位置关系,并求出交点(如果存在)。返回值列举如下:
//[有重合] 完全重合(6),1个端点重合且共线(5),部分重合(4)
//[无重合] 两端点相交(3),交于线上(2),正交(1),无交(0),参数错误(-1)
private int GetIntersection(Point p1, Point p2, Point p3, Point p4, ref Point point)
{
//保证参数p1!=p2,p3!=p4
if (p1 == p2 || p3 == p4)
{
return -1; //返回-1代表至少有一条线段首尾重合,不能构成线段
}
//为方便运算,保证各线段的起点在前,终点在后。
if (dayu(p1, p2))
{
Point pTemp = p1;
p1 = p2;
p2 = pTemp;
// swap(p1, p2);
}
if (dayu(p3, p4))
{
Point pTemp = p3;
p3 = p4;
p4 = pTemp;
//swap(p3, p4);
}
//判定两线段是否完全重合
if (p1 == p3 && p2 == p4)
{
return 6;
}
//求出两线段构成的向量
Point v1 = new Point(p2.X - p1.X, p2.Y - p1.Y), v2 = new Point(p4.X - p3.X, p4.Y - p3.Y);
//求两向量外积,平行时外积为0
float Corss = ji(v1, v2);
//如果起点重合
if (dengyu(p1, p3))
{
point = p1;
//起点重合且共线(平行)返回5;不平行则交于端点,返回3
return (Equal(Corss, 0) ? 5 : 3);
}
//如果终点重合
if (dengyu(p2, p4))
{
point = p2;
//终点重合且共线(平行)返回5;不平行则交于端点,返回3
return (Equal(Corss, 0) ? 5 : 3);
}
//如果两线端首尾相连
if (dengyu(p1, p4))
{
point = p1;
return 3;
}
if (dengyu(p2, p3))
{
point = p2;
return 3;
}//经过以上判断,首尾点相重的情况都被排除了
//将线段按起点坐标排序。若线段1的起点较大,则将两线段交换
if (dayu(p1, p3))
{
Point pTemp = p1;
p1 = p3;
p3 = pTemp;
pTemp = p2;
p2 = p4;
p4 = pTemp;
pTemp = v1;
v1 = v2;
v2 = pTemp;
//swap(p1, p3);
//swap(p2, p4);
//更新原先计算的向量及其外积
//swap(v1, v2);
Corss = ji(v1, v2);
}
//处理两线段平行的情况
if (Equal(Corss, 0))
{
//做向量v1(p1, p2)和vs(p1,p3)的外积,判定是否共线
Point vs = new Point(p3.X - p1.X, p3.Y - p1.Y);
//外积为0则两平行线段共线,下面判定是否有重合部分
if (Equal(ji(v1, vs), 0))
{
//前一条线的终点大于后一条线的起点,则判定存在重合
if (dayu(p2, p3))
{
point = p3;
return 4;//返回值4代表线段部分重合
}
}//若三点不共线,则这两条平行线段必不共线。
//不共线或共线但无重合的平行线均无交点
return 0;
}//以下为不平行的情况,先进行快速排斥试验
//x坐标已有序,可直接比较。y坐标要先求两线段的最大和最小值
float ymax1 = p1.Y, ymin1 = p2.Y, ymax2 = p3.Y, ymin2 = p4.Y;
if (ymax1 < ymin1)
{
float fTemp = ymax1;
ymax1 = ymin1;
ymin1 = fTemp;
//swap(ymax1, ymin1);
}
if (ymax2 < ymin2)
{
//swap(ymax2, ymin2);
float fTemp = ymax2;
ymax2 = ymin2;
ymin2 = fTemp;
}
//如果以两线段为对角线的矩形不相交,则无交点
if (p1.X > p4.X || p2.X < p3.X || ymax1 < ymin2 || ymin1 > ymax2)
{
return 0;
}//下面进行跨立试验
Point vs1 = new Point(p1.X - p3.X, p1.Y - p3.Y), vs2 = new Point(p2.X - p3.X, p2.Y - p3.Y);
Point vt1 = new Point(p3.X - p1.X, p3.Y - p1.Y), vt2 = new Point(p4.X - p1.X, p4.Y - p1.Y);
float s1v2, s2v2, t1v1, t2v1;
//根据外积结果判定否交于线上
if (Equal(s1v2 = ji(vs1, v2), 0) && dayu(p4, p1) && dayu(p1, p3))
{
point = p1;
return 2;
}
if (Equal(s2v2 = ji(vs2, v2), 0) && dayu(p4, p2) && dayu(p2, p3))
{
point = p2;
return 2;
}
if (Equal(t1v1 = ji(vt1, v1), 0) && dayu(p2, p3) && dayu(p3, p1))
{
point = p3;
return 2;
}
if (Equal(t2v1 = ji(vt2, v1), 0) && dayu(p2, p4) && dayu(p4, p1))
{
point = p4;
return 2;
}//未交于线上,则判定是否相交
if (s1v2 * s2v2 > 0 || t1v1 * t2v1 > 0)
{
return 0;
}//以下为相交的情况,算法详见文档
//计算二阶行列式的两个常数项
float ConA = p1.X * v1.Y - p1.Y * v1.X;
float ConB = p3.X * v2.Y - p3.Y * v2.X;
//计算行列式D1和D2的值,除以系数行列式的值,得到交点坐标
point.X = (int)((ConB * v1.X - ConA * v2.X) / Corss);
point.Y = (int)((ConB * v1.Y - ConA * v2.Y) / Corss);
//正交返回1
return 1;
}
/// <summary>
/// 根据 矩形和 移动方向获取 行走路径。
/// </summary>
/// <param name="p1"> 矩形 左上角</param>
/// <param name="p2">矩形 右下角</param>
/// <param name="p3"> lin 开始点</param>
/// <param name="p4">lin 结束点</param>
/// <param name="regionDir"> 角朝向</param>
/// <param name="startDoc"> 开始点 是否在矩形内</param>
/// <param name="endDoc">结束点 是否在矩形内</param>
/// <returns></returns>
public List<Point> GetPointList(Point p1, Point p2, Point p3, Point p4, string regionDir, bool startDoc, bool endDoc)
{
List<Point> points = new List<Point>();
Point point = new Point();
Point startPoint1 = new Point(), endPoint1 = new Point(), startPoint2 = new Point(), endPoint2 = new Point();
switch (regionDir)
{
case "左上角":
startPoint1 = new Point(p1.X, p2.Y);
endPoint1 = p2;
startPoint2 = new Point(p2.X, p1.Y);
endPoint2 = p2;
break;
case "左下角":
startPoint1 = p1;
endPoint1 = new Point(p2.X, p1.Y);
startPoint2 = new Point(p2.X, p1.Y);
endPoint2 = p2;
break;
case "右上角":
startPoint1 = new Point(p1.X, p2.Y);
endPoint1 = p2;
startPoint2 = p1;
endPoint2 = new Point(p1.X, p2.Y);
break;
case "右下角":
startPoint1 = p1;
endPoint1 = new Point(p2.X, p1.Y);
startPoint2 = p1;
endPoint2 = new Point(p1.X, p2.Y);
break;
}
// 获取 交点信息。
var posDoc1 = GetIntersection(startPoint1, endPoint1, p3, p4, ref point);
var posDoc2 = GetIntersection(startPoint2, endPoint2, p3, p4, ref point);
if (startDoc)
{
if (posDoc1 == 1 || posDoc2 == 1)
{
points.Add(point);
points.Add(p4);
}
}
else if (endDoc)
{
if (posDoc1 == 1 || posDoc2 == 1)
{
points.Add(p3);
points.Add(point);
}
}
return points;
}
public List<Point> NotRetraction(string regionDir, double[] edgeInfo, ref Point sPoint, ref Point ePoint)
{
double leftOffset = 0, topOffset = 0, rightOffset = 0, bottomOffset = 0;
switch (edgeInfo.Length)
{
case 1:
leftOffset = edgeInfo[0];
topOffset = edgeInfo[0];
rightOffset = edgeInfo[0];
bottomOffset = edgeInfo[0];
break;
//case 2:
// //leftOffset = edgeInfo[0];
// //rightOffset = edgeInfo[0];
// //topOffset = edgeInfo[1];
// //bottomOffset = edgeInfo[1];
// break;
//case 3:
// //leftOffset = edgeInfo[0];
// //topOffset = edgeInfo[1];
// //rightOffset = edgeInfo[2];
// //bottomOffset = edgeInfo[3];
// break;
case 4:
leftOffset = edgeInfo[0];
topOffset = edgeInfo[1];
rightOffset = edgeInfo[2];
bottomOffset = edgeInfo[3];
break;
}
switch (regionDir)
{
case "左上角":
sPoint = new Point((int)(sPoint.X), (int)(sPoint.Y));
ePoint = new Point((int)(ePoint.X + leftOffset), (int)(ePoint.Y + topOffset));
break;
case "左下角":
sPoint = new Point((int)(sPoint.X), (int)(sPoint.Y - bottomOffset));
ePoint = new Point((int)(ePoint.X + leftOffset), ePoint.Y);
break;
case "右上角":
sPoint = new Point((int)(sPoint.X - rightOffset), (int)(sPoint.Y));
ePoint = new Point((int)(ePoint.X), (int)(ePoint.Y + topOffset));
break;
case "右下角":
sPoint = new Point((int)(sPoint.X - rightOffset), (int)(sPoint.Y - bottomOffset));
ePoint = new Point((int)(ePoint.X), (int)(ePoint.Y));
break;
case "左边":
sPoint = new Point((int)(sPoint.X), (int)(sPoint.Y - topOffset));
ePoint = new Point((int)(ePoint.X + leftOffset), (int)(ePoint.Y + bottomOffset));
break;
case "右边":
sPoint = new Point((int)(sPoint.X - rightOffset), (int)(sPoint.Y - topOffset));
ePoint = new Point((int)(ePoint.X), (int)(ePoint.Y + bottomOffset));
break;
case "上边":
sPoint = new Point((int)(sPoint.X - leftOffset), (int)(sPoint.Y));
ePoint = new Point((int)(ePoint.X + rightOffset), (int)(ePoint.Y + topOffset));
break;
case "下边":
sPoint = new Point((int)(sPoint.X - leftOffset), (int)(sPoint.Y - bottomOffset));
ePoint = new Point((int)(ePoint.X + rightOffset), (int)(ePoint.Y));
break;
}
return null;
}
#endregion