forked from Dev-XYS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
判断两条线段是否有交点(附HDU - 1086 You can Solve a Geometry Problem too代码)
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <cstdio> | ||
|
||
using namespace std; | ||
|
||
struct point | ||
{ | ||
double x, y; | ||
point() { } | ||
point(double _x, double _y) : x(_x), y(_y) { } | ||
}; | ||
|
||
struct segment | ||
{ | ||
point s, t; | ||
segment() { } | ||
segment(point _s, point _t) : s(_s), t(_t) { } | ||
}s[110]; | ||
|
||
double cross(segment base, segment x) | ||
{ | ||
return (base.s.x - base.t.x) * (x.s.y - x.t.y) - (base.s.y - base.t.y) * (x.s.x - x.t.x); | ||
} | ||
|
||
bool segment_intersect(segment x, segment y) | ||
{ | ||
return cross(x, segment(x.s, y.s)) * cross(x, segment(x.s, y.t)) <= 0 && cross(y, segment(y.s, x.s)) * cross(y, segment(y.s, x.t)) <= 0; | ||
} | ||
|
||
int main() | ||
{ | ||
int m; | ||
while (true) | ||
{ | ||
scanf("%d", &m); | ||
if (m == 0) | ||
{ | ||
break; | ||
} | ||
for (int i = 0; i < m; i++) | ||
{ | ||
scanf("%lf%lf%lf%lf", &s[i].s.x, &s[i].s.y, &s[i].t.x, &s[i].t.y); | ||
} | ||
int ans = 0; | ||
for (int i = 0; i < m; i++) | ||
{ | ||
for (int j = 0; j < i; j++) | ||
{ | ||
if (segment_intersect(s[i], s[j]) == true) | ||
{ | ||
ans++; | ||
} | ||
} | ||
} | ||
printf("%d\n", ans); | ||
} | ||
return 0; | ||
} |