1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| bool onSeg(Point p, Point a, Point b) { return !dcmp((p - a) ^ (p - b)) && dcmp((p - a) * (p - b)) <= 0; } bool onLine(Point p, Point a, Point b) { return !dcmp((p - a) ^ (p - b)); } double toLine(Point p, Point a, Point b) { Point v = b - a; return fabs(v ^ (p - a)) / v.len(); } Point foot(Point p, Point a, Point b) { Point v = b - a; return a + v * ((v * (p - a)) / (v * v)); } int side(Point p, Point a, Point b) { return dcmp((p - a) ^ (b - a)); } double toSeg(Point p, Point a, Point b) { if (a == b) return (p - a).len(); Point x = p - a, y = p - b, z = b - a; if (dcmp(x * z) < 0) return x.len(); if (dcmp(y * z) > 0) return y.len(); return toLine(p, a, b); } Point inter(Point a, Point b, Point c, Point d) { Point x = b - a, y = d - c, z = a - c; return a + x * ((y ^ z) / (x ^ y)); } struct Line { Point a, b; double k; Line() {} Line(Point a, Point b) : a(a), b(b) { k = atan2(b.y - a.y, b.x - a.x); } bool operator<(Line rhs) const { if (dcmp(k - rhs.k)) return dcmp(k - rhs.k) < 0; return side(rhs.a, a, b) == 1; } }; Point inter(Line p, Line q) { return inter(p.a, p.b, q.a, q.b); }
|