poj 1269 判断两直线的关系并求交点

主要是利用向量的叉积,求交点的时候就是解二元一次方程组,没什么难度,采用斜率法算的因为精度损失过大,可能导致wrong

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define eps 1e-8

using namespace std;

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

struct Line
{
    Point u,v;
};

int cross ( Point a , Point b , Point c , Point d )
{
    return (a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x);
}

int meet ( Line l1 , Line l2 )
{
    if ( fabs (cross ( l1.u , l1.v , l2.u , l2.v )) > eps )
       return 1;
    if ( fabs ( cross( l1.u , l2.u , l2.u , l2.v )) < eps )
       return 2;
    return 0; 
}

double area ( Point a , Point b , Point c )
{
    return fabs ( cross ( a , b , a , c ) );
}

/*Point intersect ( Line l1 , Line l2 )
{
    double k = area ( l1.u , l1.v , l2.u ) / area ( l1.u, l1.v ,l2.v ); 
    return Point ((l2.u.x+k*l2.v.x)/(1+k) , (l2.u.y+k*l2.v.y)/(1+k));
}*/
Point intersect ( Line l1 , Line l2 )
{
	double a1 = l1.u.y - l1.v.y;
	double b1 = l1.v.x - l1.u.x;
	double c1 = l1.u.x*l1.v.y-l1.v.x*l1.u.y;
	double a2 = l2.u.y - l2.v.y;
	double b2 = l2.v.x - l2.u.x;
	double c2 = l2.u.x*l2.v.y - l2.v.x*l2.u.y;
	double x = ( b1*c2 - b2*c1 ) / (a1*b2 - a2*b1 );
	double y = ( a2*c1 - a1*c2 ) / (a1*b2 - a2*b1 );
	return Point ( x , y );
}


int main ( )
{
    int n;
    while ( ~scanf ( "%d" , &n ) )
    {
        Line l[3];
        puts ( "INTERSECTING LINES OUTPUT" );
        for ( int i = 1 ; i <= n ; i++ )
        {
            for ( int i =1 ; i <=2 ; i++ )
                scanf ( "%lf%lf%lf%lf" , &l[i].u.x , &l[i].u.y , 
                                     &l[i].v.x , &l[i].v.y);
            int temp = meet ( l[1] , l[2] );
            if ( temp == 1 )
            {
                Point p = intersect ( l[1] , l[2] );
                printf ( "POINT %.2f %.2f\n" , p.x , p.y );
            }
            if ( temp == 2 )
                puts ( "LINE" );
            if ( temp == 0 )
                puts ( "NONE" );
        }
        puts ( "END OF OUTPUT" );
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值