//分割所有的线段,得到新的线段数组
vector<CLine> CMinimumClosedArea::breaklines(vector<CLine> input)
{
vector<CLine> lines;
lines.clear();
lines = input;
vector<CLine> output;
output.clear();
for (unsigned int i = 0; i < lines.size(); i++)
{
vector<Point> crosspointVec;
for (unsigned int j = 0; j < lines.size(); j++)
{
if (doselinecross(lines[i], lines[j])) //两条line相交
{
//交点不是lines[i]的起点和终点
Point crosspoint;
crosspoint = getcrosspoint(lines[i], lines[j]);
if ((!Be_equal_to(crosspoint, lines[i].getStartPoint()))
&& (!Be_equal_to(crosspoint, lines[i].getEndPoint())))
{
crosspointVec.push_back(crosspoint);
}
}
}
crosspointVec.insert(crosspointVec.begin(),lines[i].getStartPoint());
crosspointVec.push_back(lines[i].getEndPoint());
//计算每一个交点与line的起点距离,并增序排序
for (unsigned int m = 0;m < crosspointVec.size() - 1; m++)
{
for (unsigned int n = 0; n < crosspointVec.size()-m-1; n++ )
{
double Distm = lines[i].getStartPoint().Distance(crosspointVec[n]);
double Distn = lines[i].getStartPoint().Distance(crosspointVec[n+1]);
if (Distm > Distn)
{
swap(crosspointVec[n],crosspointVec[n+1]);
}
}
}
//将line[i]所截断的线段放入output中
for (unsigned int k = 0;k < crosspointVec.size()-1; k++)
{
CLine lineSegment;
lineSegment.setPoints(crosspointVec[k],crosspointVec[k + 1]);
output.push_back(lineSegment);
}
}
return output;
}
//获取交点
Point CMinimumClosedArea::getcrosspoint(CLine L1, CLine L2)
{
double x1 = L1.getStartPoint().x;
double y1 = L1.getStartPoint().y;
double x2 = L1.getEndPoint().x;
double y2 = L1.getEndPoint().y;
double x3 = L2.getStartPoint().x;
double y3 = L2.getStartPoint().y;
double x4 = L2.getEndPoint().x;
double y4 = L2.getEndPoint().y;
Point Q;
Q.x = 0;
Q.y = 0;
if (!doselinecross(L1, L2)) //判断两条lime是否相交
{
return Q;
}
//L1的起点与L2的起点或端点相交
if (Be_equal_to(L1.getStartPoint(), L2.getStartPoint()) || Be_equal_to(L1.getStartPoint(), L2.getEndPoint()))
{
return L1.getStartPoint();
}
//L1的终点与L2的起点或终点相交
if (Be_equal_to(L1.getEndPoint(), L2.getStartPoint()) || Be_equal_to(L1.getEndPoint(), L2.getEndPoint()))
{
return L1.getEndPoint();
}
//计算交点
double X = ((x1 - x2) * (x3 * y4 - x4 * y3) - (x3 - x4) * (x1 * y2 - x2 * y1)) / ((x3 - x4) * (y1 - y2) - (x1 - x2) * (y3 - y4));
double Y = ((y1 - y2) * (x3 * y4 - x4 * y3) - (x1 * y2 - x2 * y1) * (y3 - y4)) / ((y1 - y2) * (x3 - x4) - (x1 - x2) * (y3 - y4));
Q.x = X;
Q.y = Y;
//Q点同时在L1和L2上
if (isonline(L1.getStartPoint(), L1.getEndPoint(), Q) && isonline(L2.getStartPoint(), L2.getEndPoint(), Q))
{
return Q;
}
else
{
Q.x = 0;
Q.y = 0;
return Q;
}
}
/判断线段是否相交
bool CMinimumClosedArea::doselinecross(CLine L1, CLine L2)
{
double x1 = L1.getStartPoint().x;
double y1 = L1.getStartPoint().y;
double x2 = L1.getEndPoint().x;
double y2 = L1.getEndPoint().y;
double x3 = L2.getStartPoint().x;
double y3 = L2.getStartPoint().y;
double x4 = L2.getEndPoint().x;
double y4 = L2.getEndPoint().y;
//两条直线重合
if ((Be_equal_to(L1.getStartPoint(), L2.getStartPoint()) && Be_equal_to(L1.getEndPoint(), L2.getEndPoint())) || (Be_equal_to(L1.getStartPoint(), L2.getEndPoint()) && Be_equal_to(L1.getEndPoint() ,L2.getStartPoint()))) //两条line的端点相同
{
return false;
}
if (Be_equal_to((x3 - x4) * (y1 - y2) - (x1 - x2) * (y3 - y4), 0.f) || Be_equal_to((y1 - y2) * (x3 - x4) - (x1 - x2) * (y3 - y4), 0.f))//斜率相等
{
if (Be_equal_to(L1.getStartPoint(), L2.getStartPoint()) || Be_equal_to(L1.getEndPoint(), L2.getEndPoint()) || Be_equal_to(L1.getEndPoint(), L2.getStartPoint()) || Be_equal_to(L1.getEndPoint(), L2.getEndPoint()))
{
return true;
}
else //不相交
{
return false;
}
}
double X = ((x1 - x2) * (x3 * y4 - x4 * y3) - (x3 - x4) * (x1 * y2 - x2 * y1)) / ((x3 - x4) * (y1 - y2) - (x1 - x2) * (y3 - y4));
double Y = ((y1 - y2) * (x3 * y4 - x4 * y3) - (x1 * y2 - x2 * y1) * (y3 - y4)) / ((y1 - y2) * (x3 - x4) - (x1 - x2) * (y3 - y4));
Point Q;
Q.x = X;
Q.y = Y;
//判断交点是否同时在L1、L2上
if (isonline(L1.getStartPoint(), L1.getEndPoint(), Q) && isonline(L2.getStartPoint(), L2.getEndPoint(), Q))
{
return true;
}
else//交点在延长线上
{
return false;
}
}
//判断两个浮点数是否相等
bool CMinimumClosedArea::Be_equal_to(double A, double B)
{
if (((A - B) < 0.1) && ((A - B) > -0.1))
{
return true;
}
else
{
return false;
}
}
//判断点A和点B是否相等
bool CMinimumClosedArea::Be_equal_to(Point A, Point B)
{
if (Be_equal_to(A.x, B.x) && Be_equal_to(A.y, B.y))
{
return true;
}
else
{
return false;
}
}
//判断点是否在线上
bool CMinimumClosedArea::isonline(Point P1, Point P2, Point Q) //FVector表示点,判断Q是否在line(p1,p2)上
{
if (Be_equal_to(Q, P1) || Be_equal_to(Q, P2)) //点Q是线段的端点
{
return true;
}
if (Be_equal_to(Q.Distance(P1) + Q.Distance(P2), P1.Distance(P2)))//点Q在line的两端点之间
{
return true;
}
return false;
}