public Point Center
{
get
{
Point ptCenter = new Point();
int i, j;
double ai, atmp = 0, xtmp = 0, ytmp = 0;
if (m_LogicPoints.Count == 1)
return m_LogicPoints[0];
if ((m_LogicPoints.Count == 2) || (m_LogicPoints.Count == 3 && m_LogicPoints[0] == m_LogicPoints[2]))
return new Point() { X = (m_LogicPoints[1].X + m_LogicPoints[0].X) / 2, Y = (m_LogicPoints[1].Y + m_LogicPoints[0].Y) / 2 };
int n = m_LogicPoints.Count;
for (i = n - 1, j = 0; j < n; i = j, j++)
{
ai = m_LogicPoints[i].X * m_LogicPoints[j].Y - m_LogicPoints[j].X * m_LogicPoints[i].Y;
atmp += ai;
xtmp += (m_LogicPoints[j].X + m_LogicPoints[i].X) * ai;
ytmp += (m_LogicPoints[j].Y + m_LogicPoints[i].Y) * ai;
}
if (atmp != 0)
{
ptCenter.X = Convert.ToInt32(xtmp / (3 * atmp));
ptCenter.Y = Convert.ToInt32(ytmp / (3 * atmp));
}
return ptCenter;
}
}
public bool isColide(Line line)//判断是否相交,直线p1p2与线段p3p4
{
//判处端点
if (this.V0 == line.V0 || this.V0 == line.V1 || this.V1 == line.V0 || this.V1 == line.V1)
return false;
double deviation = 0.1;//允许误差
//排斥试验,判断p1p2在q1q2为对角线的矩形区之外
if (Math.Max(line.V0.X, line.V1.X) < Math.Min(this.V0.X, this.V1.X)- deviation)
{//P1P2中最大的X比Q1Q2中的最小X还要小,说明P1P2在Q1Q2的最左点的左侧,不可能相交。
return false;
}
if (Math.Min(line.V0.X, line.V1.X) > Math.Max(this.V0.X, this.V1.X)+ deviation)
{//P1P2中最小的X比Q1Q2中的最大X还要大,说明P1P2在Q1Q2的最右点的右侧,不可能相交。
return false;
}
if (Math.Max(line.V0.Y, line.V1.Y) < Math.Min(this.V0.Y, this.V1.Y)- deviation)
{//P1P2中最大的Y比Q1Q2中的最小Y还要小,说明P1P2在Q1Q2的最低点的下方,不可能相交。
return false;
}
if (Math.Min(line.V0.Y, line.V1.Y) > Math.Max(this.V0.Y, this.V1.Y)+ deviation)
{//P1P2中最小的Y比Q1Q2中的最大Y还要大,说明P1P2在Q1Q2的最高点的上方,不可能相交。
return false;
}
//跨立试验
var crossP1P2Q1 = Cross(line.V0, line.V1, this.V0);
var crossP1Q2P2 = Cross(line.V0, this.V1, line.V1);
var crossQ1Q2P1 = Cross(this.V0, this.V1, line.V0);
var crossQ1P2Q2 = Cross(this.V0, line.V1, this.V1);
return (crossP1P2Q1 * crossP1Q2P2 >= 0 || Math.Abs(crossP1P2Q1 * crossP1Q2P2) < 0.01) && (crossQ1Q2P1 * crossQ1P2Q2 >= 0 || Math.Abs(crossQ1Q2P1 * crossQ1P2Q2) < 0.01);
}
参考: https://zh.wikipedia.org/wiki/几何中心 https://blog.csdn.net/xxdddail/article/details/70264399