HDU1086 You can Solve a Geometry Problem too
2013-09-17 20:41
239 查看
微微发亮的传送
计算几何的基础题,给定一些线段,判断有多少个交点,同时遇到很多条直线相交的时候可以重复计算,非常良心了。
我搬出了我的线段类和点类,写的那叫一个复杂,不过好在还是A了,如果不许重复计算的话我们就得求交点了……
计算几何的基础题,给定一些线段,判断有多少个交点,同时遇到很多条直线相交的时候可以重复计算,非常良心了。
我搬出了我的线段类和点类,写的那叫一个复杂,不过好在还是A了,如果不许重复计算的话我们就得求交点了……
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; const double eps = 1e-8; int cmp(double x){ if (fabs(x) < eps) return 0; if (x > 0) return 1; return -1; } const double pi = acos(-1.0); inline double sqr(double x){ return x * x; } struct point{ double x, y; point() {} point(double a, double b) : x(a), y(b){} void input(){ scanf("%lf%lf", &x, &y); } friend point operator + (const point &a, const point &b){ return point(a.x + b.x, a.y + b.y); } friend point operator - (const point &a, const point &b){ return point(a.x - b.x, a.y - b.y); } friend bool operator == (const point &a, const point &b){ return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0; } friend point operator * (const double &b, const point &a){ return point(a.x * b, a.y * b); } friend point operator / (const point &a, const double &b){ return point(a.x / b, a.y / b); } friend bool operator < (const point &a, const point &b){ return a.x < b.x; } double norm(){ return sqrt(sqr(x) + sqr(y)); } }; double det(const point &a, const point &b){ return a.x * b.y - a.y * b.x; } double dot(const point &a, const point &b){ return a.x * b.x + a.y * b.y; } double dist(const point &a, const point &b){ return (a - b).norm(); } point rotate_point(const point &p, double A){ double tx = p.x, ty = p.y; return point(tx * cos(A) - ty * sin(A), tx * sin(A) + ty * cos(A)); } struct line{ point a, b; line() {}; line(point x, point y) : a(x), b(y){} }; line point_make_line(const point a, const point b){ return line(a, b); } double dist_point_line(const point p, const point s, const point t){ if (cmp(dot(p - s, t - s)) < 0) return (p - s).norm(); if (cmp(dot(p - t, s - t)) < 0) return (p - t).norm(); return fabs(det(s - p, t - p) / dist(s, t)); } void PointProjLine(const point p, const point s, const point t, point &cp){ double r = dot((t - s), (p - s)) / dot(t - s, t - s); cp = s + r * (t - s); } bool PointOnSegment(point p, point s, point t){ return cmp(det(p - s, t - s)) == 0 && cmp(dot(p - s, p - t) <= 0); } bool parallel(line a, line b){ return !cmp(det(a.a - a.b, b.a - b.b)); } bool line_make_point(line a, line b, point &res){ if (parallel(a, b)) return 0; double s1 = det(a.a - b.a, b.b - b.a); double s2 = det(a.b - b.a, b.b - b.a); res = (s1 * a.b - s2 * a.a) / (s1 - s2); return 1; } line line_move_d(line a, const double &len){ point d = a.b - a.a; d = d / d.norm(); d = rotate_point(d, pi / 2); return line(a.a + len * d, a.b + len * d); } bool seg_make_point(line a, line b){ double d1, d2, d3, d4; d1 = det(a.b - a.a, b.a - a.a); d2 = det(a.b - a.a, b.b - a.a); d3 = det(b.b - b.a, a.a - b.a); d4 = det(b.b - b.a, a.b - b.a); if (d1 * d2 < 0 && d3 * d4 < 0) return 1; if (d1 == 0 && dot(a.b - b.a, a.a - b.a) <= 0) return 1; if (d2 == 0 && dot(a.b - b.b, a.a - b.b) <= 0) return 1; if (d3 == 0 && dot(b.b - a.a, b.a - a.a) <= 0) return 1; if (d4 == 0 && dot(b.b - a.b, b.a - a.b) <= 0) return 1; return 0; } int n; line seg[110]; point tmp; int main(){ #ifndef ONLINE_JUDGE freopen("in", "rt", stdin); #endif while(scanf("%d", &n) == 1 && n){ point s, t; for (int i = 0; i < n; i++){ s.input(); t.input(); seg[i] = point_make_line(s, t); } int ans = 0; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (seg_make_point(seg[i], seg[j])) ans += 1; printf("%d\n", ans); } return 0; }
相关文章推荐
- You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交
- hdu1086 You can Solve a Geometry Problem too (判断两线段是否相交)
- HDU1086 You can Solve a Geometry Problem too
- HDU1086_You can Solve a Geometry Problem too(几何/叉积判断点线关系)
- You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交
- HDU1086 You can Solve a Geometry Problem too
- hdu1086 You can Solve a Geometry Problem too
- hdu1086 You can Solve a Geometry Problem too【排斥实验+跨立实验】
- HDU1086 You can Solve a Geometry Problem too(数学几何)
- hdu1086 You can Solve a Geometry Problem too
- ACM-计算几何之You can Solve a Geometry Problem too——hdu1086
- hdu1086 You can Solve a Geometry Problem too
- HDU1086_You can Solve a Geometry Problem too_判断两线段相交
- hdu1086 You can Solve a Geometry Problem too
- HDU1086:You can Solve a Geometry Problem too(线段相交模板)
- hdoj1086 You can Solve a Geometry Problem too(数学几何题)
- HDU 1086 You can Solve a Geometry Problem too
- HDU-1086-You can Solve a Geometry Problem too
- HDU_1086 You can Solve a Geometry Problem too(几何题)
- HDU 1086 You can Solve a Geometry Problem too