You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
5.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using GummingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GummingSupport
{
public enum CrossingType
{
UnDefined, //未定义
Parallel,//平行
SolidCross,//实线相交
DottedCross,//虚线相交
}
public class LineCheck
{
/// <summary>
/// 判断两条线是否相交
/// </summary>
/// <param name="a">线段1起点坐标</param>
/// <param name="b">线段1终点坐标</param>
/// <param name="c">线段2起点坐标</param>
/// <param name="d">线段2终点坐标</param>
/// <param name="intersection">相交点坐标</param>
/// <returns>是否相交 0:两线平行 -1:不平行且未相交 1:两线相交</returns>
public static CrossingType GetCrossingType(Point a, Point b, Point c, Point d, ref Point intersection)
{
//判断异常
if (Math.Abs(b.x - a.y) + Math.Abs(b.x - a.x) + Math.Abs(d.y - c.y) + Math.Abs(d.x - c.x) == 0)
{
if (c.x - a.x == 0)
{
return CrossingType.UnDefined;// Debug.Print("ABCD是同一个点");
}
else
{
return CrossingType.UnDefined;//Debug.Print("AB是一个点CD是一个点且AC不同");
}
}
if (Math.Abs(b.y - a.y) + Math.Abs(b.x - a.x) == 0)
{
if ((a.x - d.x) * (c.y - d.y) - (a.y - d.y) * (c.x - d.x) == 0)
{
return CrossingType.UnDefined;// Debug.Print("A、B是一个点且在CD线段上");
}
else
{
return CrossingType.UnDefined;//Debug.Print("A、B是一个点且不在CD线段上");
}
}
if (Math.Abs(d.y - c.y) + Math.Abs(d.x - c.x) == 0)
{
if ((d.x - b.x) * (a.y - b.y) - (d.y - b.y) * (a.x - b.x) == 0)
{
return CrossingType.UnDefined;//Debug.Print("C、D是一个点且在AB线段上");
}
else
{
return CrossingType.UnDefined;//Debug.Print("C、D是一个点且不在AB线段上");
}
}
if ((b.y - a.y) * (c.x - d.x) - (b.x - a.x) * (c.y - d.y) == 0)
{
return CrossingType.Parallel; //Debug.Print("线段平行,无交点!");
}
intersection.x = ((b.x - a.x) * (c.x - d.x) * (c.y - a.y) - c.x * (b.x - a.x) * (c.y - d.y) + a.x * (b.y - a.y) * (c.x - d.x)) / ((b.y - a.y) * (c.x - d.x) - (b.x - a.x) * (c.y - d.y));
intersection.y = ((b.y - a.y) * (c.y - d.y) * (c.x - a.x) - c.y * (b.y - a.y) * (c.x - d.x) + a.y * (b.x - a.x) * (c.y - d.y)) / ((b.x - a.x) * (c.y - d.y) - (b.y - a.y) * (c.x - d.x));
//使用0.1 是避免浮点型数据产生极小误差
if ((intersection.x - a.x) * (intersection.x - b.x) <= 0.1 && (intersection.x - c.x) * (intersection.x - d.x) <= 0.1 && (intersection.y - a.y) * (intersection.y - b.y) <= 0.1 && (intersection.y - c.y) * (intersection.y - d.y) <= 0.1)
{
return CrossingType.SolidCross; //Debug.Print("线段相交于点(" + intersection.x + "," + intersection.y + ")");
}
else
{
return CrossingType.DottedCross; //Debug.Print("线段相交于虚交点(" + intersection.x + "," + intersection.y + ")");
}
}
public static void GetRegion(Point a, Point b, Point c, Point d, ref Point e, ref Point f)
{
double xMax = (a.x > b.x) ? a.x : b.x;
double yMax = (a.y > b.y) ? a.y : b.y;
double xMin = (a.x < b.x) ? a.x : b.x;
double yMin = (a.y < b.y) ? a.y : b.y;
xMax = (xMax > c.x) ? xMax : c.x;
yMax = (yMax > c.y) ? yMax : c.y;
xMin = (xMin < c.x) ? xMin : c.x;
yMin = (yMin < c.y) ? yMin : c.y;
xMax = (xMax > d.x) ? xMax : d.x;
yMax = (yMax > d.y) ? yMax : d.y;
xMin = (xMin < d.x) ? xMin : d.x;
yMin = (yMin < d.y) ? yMin : d.y;
e = new GummingEntity.Point(xMin, yMin);
f = new GummingEntity.Point(xMax, yMax);
}
public static double GetDistanceP2L(double x, double y, double x1, double y1, double x2, double y2)
{
double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
if (cross <= 0) return Math.Sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
double d2 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
if (cross >= d2) return Math.Sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
double r = cross / d2;
double px = x1 + (x2 - x1) * r;
double py = y1 + (y2 - y1) * r;
return Math.Sqrt((x - px) * (x - px) + (py - y1) * (py - y1));
}
}
}