## Friday, March 11, 2011

### poj_1269_Intersecting_Lines.cpp

poj1269, uva00378,

## Problem:

Intersecting Lines
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4630 Accepted: 2223
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```
Source
Mid-Atlantic 1996

## Solution:

`The following test case is very helpful. `
```The following input:
```
```7
-7 2 -7 8 2 3 2 7
-2 3 13 3 -2 -5 13 -5
0 0 3 5 6 10 -3 -5
-3 10 -3 -15 -3 5 2 5
5 1 15 5 8 5 4 11
3 6 3 3 3 -3 3 -5
3 6 3 3 2 7 2 8
```
```should produce the following output:

INTERSECTING LINES OUTPUT
NONE
NONE
LINE
POINT -3.00 5.00
POINT 9.47 2.79
LINE
NONE
END OF OUTPUT```
` `

`You should not ignore any situation involved with a and b, which is very important to determine he slope of the line: ax + by = c. `

## Source Code:

//Fri Mar 11 20:27:29 CST 2011
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

const double ERR = 0.000001;

/*
* ax + by = c
*
*/
class Line
{
public:
double a;
double b;
double c;

Line(double x1, double y1, double x2, double y2)
{
if (x1 != x2 && y1 == y2)
{
a = 0.0;
b = 1.0;
c = y1;
}
else if (x1 == x2 && y1 != y2)
{
a = 1.0;
b = 0.0;
c = x1;
}
else
{
a = (y1 - y2) / (x1 - x2);
b = -1.0;
c = x1 * (y2 - y1) / (x2 - x1) - y1;
}
}

void check(Line other)
{
//Same slope;
if (fabs(this->a * other.b - this->b * other.a) <= ERR)
{

if ((fabs(this->a * other.c - other.a * this->c) <= ERR) && (fabs(other.c * this->b - this->c * other.b) <= ERR))
{
cout << "LINE" << endl;
return;
}
else
{
cout << "NONE" << endl;
return;
}
}
double x = (this->c * other.b - other.c * this->b) / (this->a * other.b - other.a * this->b);
double y = (this->c * other.a - other.c * this->a) / (other.a * this->b - this->a * other.b);
cout << setiosflags(ios::fixed) << setprecision(2) << "POINT " << setprecision(2) << x << " " << y << endl;
return;
}
};

int main(int argc, const char* argv[])
{
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
cout << "INTERSECTING LINES OUTPUT" << endl;
int N;
cin >> N;
while (N--)
{
double x1, x2, x3, x4;
double y1, y2, y3, y4;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
Line a(x1, y1, x2, y2);
Line b(x3, y3, x4, y4);
a.check(b);
}
cout << "END OF OUTPUT" << endl;
//fclose(stdin);
//fclose(stdout);
return 0;
}