POJ1269

We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.

Input

The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).

Output

There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".

Sample Input

5
0 0 4 4 0 4 4 0
5 0 7 6 1 0 2 3
5 0 7 6 3 -6 4 -3
2 0 2 27 1 5 18 5
0 3 4 0 1 2 2 5

Sample Output

INTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
END OF OUTPUT

 

思路:1.利用叉积是否共线 2.利用叉积判断是否平行 3.前面都不符合则求交点

(题目够坑,两条直线(不是线段)的位置关系只有三种情况:共线,平行,相交)

两条直线求交点的情况分两种情况:一种是给定的四个点两条直线已经有交点,另一种是需要延长其中一条直线。

 

#include<cstdio>
#include<string.h>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
using namespace std;
static const double eps = 1e-8;
int n;

struct point{
    double x, y;
    point(double a, double b):x(a), y(b){}
};

point operator - (point a, point b)
{
    return point(a.x - b.x, a.y - b.y);
}

point operator + (point a, point b)
{
    return point(a.x + b.x, a.y + b.y);
}

point operator * (point a, double t)
{
    return point(a.x * t, a.y * t);
}

//平面上的向量叉乘(a叉乘b,返回在z轴上的投影)
double across(point a, point b)
{
    return a.x * b.y - a.y * b.x;
}

struct line{
    point a, b;
    line(point a1, point b1):a(a1),b(b1){}
};


//判断两条直线是否平行
bool parallel(line l1, line l2)
{
    //利用叉乘判断是否平行
    point s1 = l1.a - l1.b;
    point s2 = l2.a - l2.b;

    if (fabs(across(s1, s2)) < eps)
        return true;

    return false;
}

//判断两条平行的直线是否在同一条线上:首先要平行,否则会有特例(其中一个点位于另一条线上,这样就是相交而不是在同一条线上)
bool isline(line l1, line l2)
{
    //利用叉乘判断是否在同一直线
//    point p1 = l1.a - l2.a; //两个向量
//    point p2 = l1.a - l2.b;
//
    point p1 = l1.a - l2.a;
    point p2 = l1.b - l2.a;

    point p3 = l1.a - l2.b;
    point p4 = l1.b - l2.b;

    if (fabs(across(p1, p2)) < eps && fabs(across(p3, p4)) < eps)
        return true;

    return false;
}

//判断两直线相交
bool intersection1(line l1, line l2)
{
    point p1 = l1.a - l2.a;
    point p2 = l1.a - l2.b;
    point p3 = l1.b - l2.a;
    point p4 = l1.b - l2.b;
    double a1 = across(p1, p2);
    double a2 = across(p3, p4);

    point p5 = l2.a - l1.a;
    point p6 = l2.a - l1.b;
    point p7 = l2.b - l1.a;
    point p8 = l2.b - l1.b;
    double a3 = across(p5, p6);
    double a4 = across(p7, p8);

    if (a1 * a2 <= eps && a3 * a4 <= eps)
        return true;
    else
        return false;
}

//求两直线相交的交点
point intersection2(line l1, line l2)
{
    point p1 = l1.b - l1.a;
    point p2 = l2.a - l1.a;
    double d1 = fabs(across(p1, p2));

    point p3 = l2.b - l1.a;
    double d2 = fabs(across(p1, p3));

    double t = d1 / (d1 + d2);

    return l2.a + (l2.b - l2.a) * t;
}

point intersection3(line l1, line l2)
{
    point p1 = l1.b - l1.a;
    point p2 = l2.a - l1.a;
    double d1 = fabs(across(p1, p2));

    point p3 = l2.b - l1.a;
    double d2 = fabs(across(p1, p3));

    double t = d2 / (d1 - d2);

    return l2.a + (l2.b - l2.a) * (1 + t);
}

//题目要求判断两条直线(只有共线,平行,相交三种情况)
int main()
{
    scanf("%d", &n);
    printf("INTERSECTING LINES OUTPUT\n");

    for (int i = 0; i < n; i++)
    {
        double x1, y1, x2, y2, x3, y3, x4, y4;
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf",
              &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);

        line l1(point(x1, y1), point(x2, y2));
        line l2(point(x3, y3), point(x4, y4));


        if (isline(l1, l2)) //共线
            printf("LINE\n");
        else if (parallel(l1, l2))  //平行
            printf("NONE\n");
//        else if (!intersection1(l1, l2))   //不平行也不相交
//            printf("NONE\n");
        else //相交
        {
            if (!intersection1(l1, l2))
            {
                point tmp = intersection3(l1, l2);
                printf("POINT %.2lf %.2lf\n", tmp.x, tmp.y);
            }
            else
            {
                point tmp = intersection2(l1, l2);
                printf("POINT %.2lf %.2lf\n", tmp.x, tmp.y);
            }
        }
    }
    printf("END OF OUTPUT\n");

    return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值