Skip to content

Commit 2eae153

Browse files
Merge pull request #732 from NurlashKO/add_geometry_2d
Add Geometry in 2D section and source code for line intersection.
2 parents b80b59f + d4ffd04 commit 2eae153

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Solution of UVA 378 problem.
2+
// https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=5&problem=314
3+
// Short statement: Find intersection of two lines or output if there is no such or it is infinite.
4+
5+
#include <iostream>
6+
#include <cmath>
7+
#include <cstring>
8+
9+
using namespace std;
10+
11+
struct pt {
12+
double x, y;
13+
};
14+
15+
struct line {
16+
double a, b, c;
17+
line(pt p1, pt p2) {
18+
a = p2.y - p1.y;
19+
b = p1.x - p2.x;
20+
c = -a * p1.x - b * p1.y;
21+
}
22+
};
23+
24+
const double EPS = 1e-9;
25+
26+
double det (double a, double b, double c, double d) {
27+
return a * d - b * c;
28+
}
29+
30+
bool intersect (line m, line n, pt & res) {
31+
double zn = det (m.a, m.b, n.a, n.b);
32+
if (abs (zn) < EPS)
33+
return false;
34+
res.x = - det (m.c, m.b, n.c, n.b) / zn;
35+
res.y = - det (m.a, m.c, n.a, n.c) / zn;
36+
return true;
37+
}
38+
39+
bool parallel (line m, line n) {
40+
return abs (det (m.a, m.b, n.a, n.b)) < EPS;
41+
}
42+
43+
bool equivalent (line m, line n) {
44+
return abs (det (m.a, m.b, n.a, n.b)) < EPS
45+
&& abs (det (m.a, m.c, n.a, n.c)) < EPS
46+
&& abs (det (m.b, m.c, n.b, n.c)) < EPS;
47+
}
48+
49+
void solve(line a, line b) {
50+
if (equivalent(a, b)) {
51+
cout << "LINE\n";
52+
return ;
53+
}
54+
if (parallel(a, b)) {
55+
cout << "NONE\n";
56+
return ;
57+
}
58+
pt res;
59+
intersect(a, b, res);
60+
cout.precision(2);
61+
cout << "POINT " << fixed << res.x << " " << res.y << "\n";
62+
}
63+
64+
int main() {
65+
int t;
66+
cin >> t;
67+
cout << "INTERSECTING LINES OUTPUT\n";
68+
while (t--) {
69+
pt p1, p2;
70+
cin >> p1.x >> p1.y >> p2.x >> p2.y;
71+
line a = line(p1, p2);
72+
cin >> p1.x >> p1.y >> p2.x >> p2.y;
73+
line b = line(p1, p2);
74+
75+
solve(a, b);
76+
}
77+
cout << "END OF OUTPUT\n";
78+
return 0;
79+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,5 @@ ACM-ICPC Algorithms is a collection of important algorithms and data structures
202202
* [Top K Frequent Words In Java](/String/top_k_frequent_words_in_java)
203203
* [Uncompressing Strings](/String/Uncompressing_Strings)
204204
* [Parsing Arithmetic](/String/ParsingArithmetic)
205+
* [Geometry 2D](/Geometry%202D)
206+
* [Lines Intersection](/Geometry%202D/Lines%20Intersection)

0 commit comments

Comments
 (0)