您的位置:首页 > 其它

UVA 378 Intersecting Lines POJ 1249【直线位置情况简单判定】

2013-05-03 00:21 309 查看
题目大意:判断一组两条直线(是直线不是线段)位置情况:

1,相交,按照格式输出交点;

2,平行,输出“NONE“;

3,重叠,输出”LINE“;

解题策略:1, 由于一直考虑用求向量叉积求解,所以抛弃解析几何方法,题目简单,需要仔细耐心;

2, 直线相交情况复杂,所以一开始一直wa,考虑首先通过求叉积求两条直线是否平行(包含共线),

继而判断共线(重叠)与平行(不共线情况),

如果不属于上述两种情况,则最后一种情况必定是相交,直接计算出交点并输出;

/*
UVA 378 Intersecting Lines
AC by J_Dark
ON 2013/5/3 0:00
Time 0.012s
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int LL;

struct point{
LL x, y;
point(LL p=0, LL q=0){
x = p;
y = q;
}
};
struct line{
point a, b;
line(point x, point y){
a = x;
b = y;
}
};
vector<line> Seg;
double ansX, ansY;
//////////////////////////////////
void Input(){
Seg.clear();
LL ax1, ay1, bx1, by1, ax2, ay2, bx2, by2;
cin >> ax1 >> ay1 >> bx1 >> by1 >> ax2 >> ay2 >> bx2 >> by2;
point s1(ax1, ay1), e1(bx1, by1), s2(ax2, ay2), e2(bx2, by2);
Seg.push_back(line(s1, e1));
Seg.push_back(line(s2, e2));
}

double Direction(point Pi, point Pj, point Pk){
return (Pj.x-Pi.x)*(Pk.y-Pi.y)-(Pk.x-Pi.x)*(Pj.y-Pi.y);
}
//判断向量p q平行(包含共线情况)
bool isParallel(line p, line q){
if( !((p.b.x-p.a.x)*(q.b.y-q.a.y)-(q.b.x-q.a.x)*(p.b.y-p.a.y)) )
return true;
return false;
}
//判断线段p,q是否相交
int isIntersect(line p, line q){
LL d1, d2, d3, d4;
d1 = Direction(p.a, p.b, q.a);
d2 = Direction(p.a, p.b, q.b);
d3 = Direction(q.a, q.b, p.a);
d4 = Direction(q.a, q.b, p.b);
if(isParallel(p, q)){
if(((!d1 && !d2) || (!d3 && !d4))){ //重叠
return 2;
}
else return 3;  //平行不共线
}
//相交
else{
ansX = (double)(q.a.x*d2 - q.b.x*d1)/(d2 - d1);
ansY = (double)(q.a.y*d2 - q.b.y*d1)/(d2 - d1);
return 1;
}
}

void Compute(){
int ff;
ff = isIntersect(Seg[0], Seg[1]);
if(ff == 1){//相交 输出交点
printf("POINT %.2lf %.2lf\n", ansX, ansY);
}
if(ff == 2)   cout << "LINE" << endl; //重叠
if(ff == 3)   cout << "NONE" << endl; //平行
}

//////////////////////////////////
int main(){
int testCase;
while(cin >> testCase)
{
cout << "INTERSECTING LINES OUTPUT" << endl;
while(testCase--)
{
Input();
Compute();
}
cout << "END OF OUTPUT" << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: