首页 > 代码库 > Intersecting Lines---poj1269(求两直线的位置关系)

Intersecting Lines---poj1269(求两直线的位置关系)

题目链接:http://poj.org/problem?id=1269

 题意:给你两条直线上的任意不同的两点,然后求两条直线的位置关系,如果相交于一点输出该点坐标;

技术分享
#include<iostream>#include<algorithm>#include<math.h>#include<string.h>#include<stdio.h>#include<map>#include<vector>#include<queue>using namespace std;#define met(a, b) memset(a, b, sizeof(a))#define mod 1000000007typedef long long LL;//////////////////////////////////////////////////////////////const int INF = 0x3f3f3f3f;const int N = 21;const double eps = 1e-8;int Sign(double x){    if(fabs(x) < eps) return 0;    if(x<0) return -1;    return 1;}struct point{    double x, y;    point(double x_=0, double y_=0):x(x_),y(y_){}    point operator -(const point &b)const    {        return point(x-b.x, y-b.y);    }    double operator ^(const point &b)const    {        return (x*b.y - b.x*y);    }};struct line{    point p1, p2;    line(){}    line(point p1_, point p2_):p1(p1_), p2(p2_){}};///找到直线L1与L2的交点p0,返回值不同,含义不同;int FindLinePoint(line L1, line L2, point &p0){    double k = ((L1.p1-L1.p2) ^ (L2.p1-L2.p2));    if(Sign(k) == 0)    {        double t = ((L1.p1-L1.p2)^(L1.p1-L2.p1));        if(Sign(t) == 0) return 2;///共线;        return 1;///平行    }    p0 = L1.p1;    double t = ((L1.p1-L2.p1)^(L2.p1-L2.p2))/((L1.p1-L1.p2)^(L2.p1-L2.p2));    p0.x += (L1.p2.x - L1.p1.x)*t;    p0.y += (L1.p2.y - L1.p1.y)*t;    return 3;///相交于一点p0;}int main(){    printf("INTERSECTING LINES OUTPUT\n");    int T;    scanf("%d", &T);    while(T--)    {        point p0;        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 = line(point(x1,y1), point(x2,y2));        line L2 = line(point(x3,y3), point(x4,y4));        int ans = FindLinePoint(L1, L2, p0);        if(ans == 1) printf("NONE\n");///平行;        if(ans == 2) printf("LINE\n");///共线;        if(ans == 3) printf("POINT %.2f %.2f\n", p0.x, p0.y);    }    printf("END OF OUTPUT\n");    return 0;}
View Code

 

Intersecting Lines---poj1269(求两直线的位置关系)