您的位置:首页 > 其它

poj1269直线交点

2016-07-18 09:15 134 查看
Intersecting Lines

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14118 Accepted: 6249
Description
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

直线判共线:

A1 * B2 == A2 * B1 &&

B1 * C2 == B2 * C1 &&

A1 * C2 == A2 * C1

#include <iostream>
#include <string>
#include <algorithm>

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

using namespace std;
const double eps = 1e-8;

double iabs(const double &k)
{	return k >= 0 ? k : -k;}

bool cmp(const double &a, const double &b)
{	return iabs(a - b) < eps;}

class Point{
public:
double x, y;
Point(){}
Point(const double &a, const double &b) : x(a), y(b){}
Point(const Point &k) { this->x = k.x; this->y = k.y;}

Point operator - (const Point &k)
{	return Point(this->x - k.x, this->y - k.y);}
Point operator + (const Point &k)
{	return Point(this->x + k.x, this->y + k.y);}
void input()
{	scanf("%lf%lf", &x, &y);}
void output()
{
if(cmp(x, 0))
x = 0.0;
if(cmp(y, 0))
y = 0.0;
printf("%.2lf %.2lf\n", x, y);
}
};
typedef Point Vector;

class Line{
public:
double a, b, c;
void init(const double &x, const double &y, const double &z)
{	a = x; b = y; c = z;}
void init(const Point &A, const Point &B)
{
if( cmp( A.x, B.x) )
{
b = 0;
a = 1.0;
c = -(A.x + B.x) / 2.0;
}
else
{
b = 1.0;

//a = -(B.y - A.y) / (B.x - A.x);
//c = (A.x * this->a) - A.y;
a = - (A.y - B.y) / (A.x - B.x);
c = (A.y * B.x - B.y * A.x) / (A.x - B.x);
}
}
Point crossPoint( const Line &k)
{
double d =  (this->a * k.b - this->b * k.a);
double x = -(this->c * k.b - this->b * k.c) / d;
double y =  (this->c * k.a - this->a * k.c) / d;
return Point(x, y);
}

int operator == (const Line &k)
{
//if( this->b / this->a == k.b / k.a)
if( cmp(0, this->a * k.b - this->b * k.a))
{
if( cmp(this->a * k.b, this->b * k.a) && cmp(this->b * k.c, this->c * k.b) && cmp( this->a * k.c, this->c * k.a))
return 1;
return 2;
}
return 0;
}

void output()
{
printf("%g X + %g y + %g = 0\n", a, b, c);
}
};

void solve()
{
Point A, B, C, D, E, ans;
Vector CB, CA;
Line l1, l2;

int T;
cin >> T;
puts("INTERSECTING LINES OUTPUT");
while(T--)
{
A.input();
B.input();
C.input();
D.input();

l1.init(A, B);
l2.init(C, D);

//l1.output();
//l2.output();

int k = (l1 == l2);

if(k == 1)
puts("LINE");
else if(k == 2)
puts("NONE");
else
{
printf("POINT ");
ans = l1.crossPoint( l2 );
ans.output();
}
}
puts("END OF OUTPUT");
}

int main()
{
solve();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: