算法1:
直接计算出两直线的交点,然后判断交点是不是在线段内,代码如下
struct line
{
double x1, y1, x2, y2;
bool fun(line l)
{
double xx, yy; //两直线的交点
double k1, k2; //两直线的斜率
if (fabs(this->x1-this->x2) < 1e-7 && fabs(l.x1-l.x2) < 1e-7) //两条直线都垂直于y轴
{
if (fabs(l.x1-this->x1) < 1e-7) return true;
return false;
}
else if (fabs(this->x1-this->x2) < 1e-7) //
{
if ((this->x1-l.x1)*(this->x1-l.x2) > -1e-8) return false;
k2 = (l.y1-l.y2)/(l.x1-l.x2);
xx = this->x1;
yy = k2*(xx-l.x1) + l.y1;
if ((this->y1-yy)*(yy-this->y2) >=