Geometry.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // C++ routines for computational geometry.
  2. double INF = 1e100;
  3. double EPS = 1e-12;
  4. struct PT {
  5. double x, y;
  6. PT() {}
  7. PT(double x, double y) : x(x), y(y) {}
  8. PT(const PT &p) : x(p.x), y(p.y) {}
  9. PT operator + (const PT &p) const { return PT(x+p.x, y+p.y); }
  10. PT operator - (const PT &p) const { return PT(x-p.x, y-p.y); }
  11. PT operator * (double c) const { return PT(x*c, y*c ); }
  12. PT operator / (double c) const { return PT(x/c, y/c ); }
  13. };
  14. double dot(PT p, PT q) { return p.x*q.x+p.y*q.y; }
  15. double dist2(PT p, PT q) { return dot(p-q,p-q); }
  16. double cross(PT p, PT q) { return p.x*q.y-p.y*q.x; }
  17. ostream &operator<<(ostream &os, const PT &p) {
  18. os << "(" << p.x << "," << p.y << ")";
  19. }
  20. // rotate a point CCW or CW around the origin
  21. PT RotateCCW90(PT p) { return PT(-p.y,p.x); }
  22. PT RotateCW90(PT p) { return PT(p.y,-p.x); }
  23. PT RotateCCW(PT p, double t) {
  24. return PT(p.x*cos(t)-p.y*sin(t), p.x*sin(t)+p.y*cos(t));
  25. }
  26. // project point c onto line through a and b
  27. // assuming a != b
  28. PT ProjectPointLine(PT a, PT b, PT c) {
  29. return a + (b-a)*dot(c-a, b-a)/dot(b-a, b-a);
  30. }
  31. // project point c onto line segment through a and b
  32. PT ProjectPointSegment(PT a, PT b, PT c) {
  33. double r = dot(b-a,b-a);
  34. if (fabs(r) < EPS) return a;
  35. r = dot(c-a, b-a)/r;
  36. if (r < 0) return a;
  37. if (r > 1) return b;
  38. return a + (b-a)*r;
  39. }
  40. // compute distance from c to segment between a and b
  41. double DistancePointSegment(PT a, PT b, PT c) {
  42. return sqrt(dist2(c, ProjectPointSegment(a, b, c)));
  43. }
  44. // compute distance between point (x,y,z) and plane ax+by+cz=d
  45. double DistancePointPlane(double x, double y, double z,
  46. double a, double b, double c, double d)
  47. {
  48. return fabs(a*x+b*y+c*z-d)/sqrt(a*a+b*b+c*c);
  49. }
  50. // determine if lines from a to b and c to d are parallel or collinear
  51. bool LinesParallel(PT a, PT b, PT c, PT d) {
  52. return fabs(cross(b-a, c-d)) < EPS;
  53. }
  54. bool LinesCollinear(PT a, PT b, PT c, PT d) {
  55. return LinesParallel(a, b, c, d)
  56. && fabs(cross(a-b, a-c)) < EPS
  57. && fabs(cross(c-d, c-a)) < EPS;
  58. }
  59. // determine if line segment from a to b intersects with
  60. // line segment from c to d
  61. bool SegmentsIntersect(PT a, PT b, PT c, PT d) {
  62. if (LinesCollinear(a, b, c, d)) {
  63. if (dist2(a, c) < EPS || dist2(a, d) < EPS ||
  64. dist2(b, c) < EPS || dist2(b, d) < EPS) return true;
  65. if (dot(c-a, c-b) > 0 && dot(d-a, d-b) > 0 && dot(c-b, d-b) > 0)
  66. return false;
  67. return true;
  68. }
  69. if (cross(d-a, b-a) * cross(c-a, b-a) > 0) return false;
  70. if (cross(a-c, d-c) * cross(b-c, d-c) > 0) return false;
  71. return true;
  72. }
  73. // compute intersection of line passing through a and b
  74. // with line passing through c and d, assuming that unique
  75. // intersection exists; for segment intersection, check if
  76. // segments intersect first
  77. PT ComputeLineIntersection(PT a, PT b, PT c, PT d) {
  78. b=b-a; d=c-d; c=c-a;
  79. assert(dot(b, b) > EPS && dot(d, d) > EPS);
  80. return a + b*cross(c, d)/cross(b, d);
  81. }
  82. // compute center of circle given three points
  83. PT ComputeCircleCenter(PT a, PT b, PT c) {
  84. b=(a+b)/2;
  85. c=(a+c)/2;
  86. return ComputeLineIntersection(b, b+RotateCW90(a-b), c, c+RotateCW90(a-c));
  87. }
  88. // determine if point is in a possibly non-convex polygon (by William
  89. // Randolph Franklin); returns 1 for strictly interior points, 0 for
  90. // strictly exterior points, and 0 or 1 for the remaining points.
  91. // Note that it is possible to convert this into an *exact* test using
  92. // integer arithmetic by taking care of the division appropriately
  93. // (making sure to deal with signs properly) and then by writing exact
  94. // tests for checking point on polygon boundary
  95. bool PointInPolygon(const vector<PT> &p, PT q) {
  96. bool c = 0;
  97. for (int i = 0; i < p.size(); i++){
  98. int j = (i+1)%p.size();
  99. if ((p[i].y <= q.y && q.y < p[j].y ||
  100. p[j].y <= q.y && q.y < p[i].y) &&
  101. q.x < p[i].x + (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y))
  102. c = !c;
  103. }
  104. return c;
  105. }
  106. // determine if point is on the boundary of a polygon
  107. bool PointOnPolygon(const vector<PT> &p, PT q) {
  108. for (int i = 0; i < p.size(); i++)
  109. if (dist2(ProjectPointSegment(p[i], p[(i+1)%p.size()], q), q) < EPS)
  110. return true;
  111. return false;
  112. }
  113. // compute intersection of line through points a and b with
  114. // circle centered at c with radius r > 0
  115. vector<PT> CircleLineIntersection(PT a, PT b, PT c, double r) {
  116. vector<PT> ret;
  117. b = b-a;
  118. a = a-c;
  119. double A = dot(b, b);
  120. double B = dot(a, b);
  121. double C = dot(a, a) - r*r;
  122. double D = B*B - A*C;
  123. if (D < -EPS) return ret;
  124. ret.push_back(c+a+b*(-B+sqrt(D+EPS))/A);
  125. if (D > EPS)
  126. ret.push_back(c+a+b*(-B-sqrt(D))/A);
  127. return ret;
  128. }
  129. // compute intersection of circle centered at a with radius r
  130. // with circle centered at b with radius R
  131. vector<PT> CircleCircleIntersection(PT a, PT b, double r, double R) {
  132. vector<PT> ret;
  133. double d = sqrt(dist2(a, b));
  134. if (d > r+R || d+min(r, R) < max(r, R)) return ret;
  135. double x = (d*d-R*R+r*r)/(2*d);
  136. double y = sqrt(r*r-x*x);
  137. PT v = (b-a)/d;
  138. ret.push_back(a+v*x + RotateCCW90(v)*y);
  139. if (y > 0)
  140. ret.push_back(a+v*x - RotateCCW90(v)*y);
  141. return ret;
  142. }
  143. // This code computes the area or centroid of a (possibly nonconvex)
  144. // polygon, assuming that the coordinates are listed in a clockwise or
  145. // counterclockwise fashion. Note that the centroid is often known as
  146. // the "center of gravity" or "center of mass".
  147. double ComputeSignedArea(const vector<PT> &p) {
  148. double area = 0;
  149. for(int i = 0; i < p.size(); i++) {
  150. int j = (i+1) % p.size();
  151. area += p[i].x*p[j].y - p[j].x*p[i].y;
  152. }
  153. return area / 2.0;
  154. }
  155. double ComputeArea(const vector<PT> &p) {
  156. return fabs(ComputeSignedArea(p));
  157. }
  158. PT ComputeCentroid(const vector<PT> &p) {
  159. PT c(0,0);
  160. double scale = 6.0 * ComputeSignedArea(p);
  161. for (int i = 0; i < p.size(); i++){
  162. int j = (i+1) % p.size();
  163. c = c + (p[i]+p[j])*(p[i].x*p[j].y - p[j].x*p[i].y);
  164. }
  165. return c / scale;
  166. }
  167. // tests whether or not a given polygon (in CW or CCW order) is simple
  168. bool IsSimple(const vector<PT> &p) {
  169. for (int i = 0; i < p.size(); i++) {
  170. for (int k = i+1; k < p.size(); k++) {
  171. int j = (i+1) % p.size();
  172. int l = (k+1) % p.size();
  173. if (i == l || j == k) continue;
  174. if (SegmentsIntersect(p[i], p[j], p[k], p[l]))
  175. return false;
  176. }
  177. }
  178. return true;
  179. }
  180. // angle of vector y relative to x
  181. double angle(PT x, PT y) {
  182. return atan2(y.y,y.x) - atan2(x.y,y.x);
  183. }
  184. int test() {
  185. // expected: (-5,2)
  186. cerr << RotateCCW90(PT(2,5)) << endl;
  187. // expected: (5,-2)
  188. cerr << RotateCW90(PT(2,5)) << endl;
  189. // expected: (-5,2)
  190. cerr << RotateCCW(PT(2,5),M_PI/2) << endl;
  191. // expected: (5,2)
  192. cerr << ProjectPointLine(PT(-5,-2), PT(10,4), PT(3,7)) << endl;
  193. // expected: (5,2) (7.5,3) (2.5,1)
  194. cerr << ProjectPointSegment(PT(-5,-2), PT(10,4), PT(3,7)) << " "
  195. << ProjectPointSegment(PT(7.5,3), PT(10,4), PT(3,7)) << " "
  196. << ProjectPointSegment(PT(-5,-2), PT(2.5,1), PT(3,7)) << endl;
  197. // expected: 6.78903
  198. cerr << DistancePointPlane(4,-4,3,2,-2,5,-8) << endl;
  199. // expected: 1 0 1
  200. cerr << LinesParallel(PT(1,1), PT(3,5), PT(2,1), PT(4,5)) << " "
  201. << LinesParallel(PT(1,1), PT(3,5), PT(2,0), PT(4,5)) << " "
  202. << LinesParallel(PT(1,1), PT(3,5), PT(5,9), PT(7,13)) << endl;
  203. // expected: 0 0 1
  204. cerr << LinesCollinear(PT(1,1), PT(3,5), PT(2,1), PT(4,5)) << " "
  205. << LinesCollinear(PT(1,1), PT(3,5), PT(2,0), PT(4,5)) << " "
  206. << LinesCollinear(PT(1,1), PT(3,5), PT(5,9), PT(7,13)) << endl;
  207. // expected: 1 1 1 0
  208. cerr << SegmentsIntersect(PT(0,0), PT(2,4), PT(3,1), PT(-1,3)) << " "
  209. << SegmentsIntersect(PT(0,0), PT(2,4), PT(4,3), PT(0,5)) << " "
  210. << SegmentsIntersect(PT(0,0), PT(2,4), PT(2,-1), PT(-2,1)) << " "
  211. << SegmentsIntersect(PT(0,0), PT(2,4), PT(5,5), PT(1,7)) << endl;
  212. // expected: (1,2)
  213. cerr << ComputeLineIntersection(PT(0,0), PT(2,4), PT(3,1), PT(-1,3)) << endl;
  214. // expected: (1,1)
  215. cerr << ComputeCircleCenter(PT(-3,4), PT(6,1), PT(4,5)) << endl;
  216. vector<PT> v;
  217. v.push_back(PT(0,0)); v.push_back(PT(5,0)); v.push_back(PT(5,5)); v.push_back(PT(0,5));
  218. // expected: 1 1 1 0 0
  219. cerr << PointInPolygon(v, PT(2,2)) << " "
  220. << PointInPolygon(v, PT(2,0)) << " "
  221. << PointInPolygon(v, PT(0,2)) << " "
  222. << PointInPolygon(v, PT(5,2)) << " "
  223. << PointInPolygon(v, PT(2,5)) << endl;
  224. // expected: 0 1 1 1 1
  225. cerr << PointOnPolygon(v, PT(2,2)) << " "
  226. << PointOnPolygon(v, PT(2,0)) << " "
  227. << PointOnPolygon(v, PT(0,2)) << " "
  228. << PointOnPolygon(v, PT(5,2)) << " "
  229. << PointOnPolygon(v, PT(2,5)) << endl;
  230. // expected: (1,6)
  231. // (5,4) (4,5)
  232. // blank line
  233. // (4,5) (5,4)
  234. // blank line
  235. // (4,5) (5,4)
  236. vector<PT> u = CircleLineIntersection(PT(0,6), PT(2,6), PT(1,1), 5);
  237. for (int i = 0; i < u.size(); i++) cerr << u[i] << " "; cerr << endl;
  238. u = CircleLineIntersection(PT(0,9), PT(9,0), PT(1,1), 5);
  239. for (int i = 0; i < u.size(); i++) cerr << u[i] << " "; cerr << endl;
  240. u = CircleCircleIntersection(PT(1,1), PT(10,10), 5, 5);
  241. for (int i = 0; i < u.size(); i++) cerr << u[i] << " "; cerr << endl;
  242. u = CircleCircleIntersection(PT(1,1), PT(8,8), 5, 5);
  243. for (int i = 0; i < u.size(); i++) cerr << u[i] << " "; cerr << endl;
  244. u = CircleCircleIntersection(PT(1,1), PT(4.5,4.5), 10, sqrt(2.0)/2.0);
  245. for (int i = 0; i < u.size(); i++) cerr << u[i] << " "; cerr << endl;
  246. u = CircleCircleIntersection(PT(1,1), PT(4.5,4.5), 5, sqrt(2.0)/2.0);
  247. for (int i = 0; i < u.size(); i++) cerr << u[i] << " "; cerr << endl;
  248. // area should be 5.0
  249. // centroid should be (1.1666666, 1.166666)
  250. PT pa[] = { PT(0,0), PT(5,0), PT(1,1), PT(0,5) };
  251. vector<PT> p(pa, pa+4);
  252. PT c = ComputeCentroid(p);
  253. cerr << "Area: " << ComputeArea(p) << endl;
  254. cerr << "Centroid: " << c << endl;
  255. return 0;
  256. }