Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. #include <cmath>
  2. #include <climits>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. /*
  8. Geometry lib by Blaise1
  9. */
  10.  
  11. //magic numbers
  12. using T = double;
  13. const T EPS = 1e-12;
  14. const T INF = 1e18;
  15. //constants
  16. const bool isTangentIntersection = false;
  17.  
  18. struct point {
  19. T x, y;
  20.  
  21. point(T x, T y) : x(x), y(y) { }
  22.  
  23. //Equal operator
  24. bool operator==(const point &o) const{
  25. return x == o.x ? (y == o.y ? true : false) : false;
  26. }
  27.  
  28. //Unequal operator
  29. bool operator!=(const point &o) const{
  30. return !(*this == o);
  31. }
  32.  
  33. //Sum of points
  34. point operator+(const point &o) const{
  35. return {x + o.x, y + o.y};
  36. }
  37.  
  38. //Unary negation
  39. point operator-() const{
  40. return {-x, -y};
  41. }
  42.  
  43. //Difference of points
  44. point operator-(const point &o) const{
  45. return *this + (-o);
  46. }
  47.  
  48. //Scalar product
  49. point operator*(const T &d) const{
  50. return {x * d, y * d};
  51. }
  52.  
  53. //Vectorial product (pointwise)
  54. point operator*(const point &o) const{
  55. return {x * o.x, y * o.y};
  56. }
  57.  
  58. //Dot product
  59. T operator%(const point &o) const{
  60. return (*this * o).magnitude();
  61. }
  62.  
  63. //Cross product
  64. T operator^(const point &o) const{
  65. return x * o.y - y * o.x;
  66. }
  67.  
  68. //Distance between points
  69. T distance(const point &o) const{
  70. return hypot(fabs(x - o.x), fabs(y - o.y));
  71. }
  72.  
  73. //Normalize
  74. point normalized() const{
  75. point tmp = {x, y};
  76. tmp.normalize();
  77. return tmp;
  78. }
  79.  
  80. //Normalize
  81. void normalize(){
  82. T hy = hypot(x, y);
  83. x /= hy;
  84. y /= hy;
  85. }
  86.  
  87. T magnitude() const{
  88. return x + y;
  89. }
  90.  
  91. point rotated_rad(T rad) const{
  92. point tmp = {x, y};
  93. tmp.rotate_rad(rad);
  94. return tmp;
  95. }
  96.  
  97. //O(log n)
  98. void rotate_rad(T rad){
  99. if(rad == M_PI/2){
  100. swap(x,y);
  101. x = -x;
  102. }else if(rad == -M_PI/2){
  103. swap(x,y);
  104. y = -y;
  105. } else if(rad == M_PI || rad == -M_PI){
  106. x = -x;
  107. y = -y;
  108. } else {
  109. T newx = x * cos(rad) - y * sin(rad);
  110. T newy = x * sin(rad) + y * cos(rad);
  111. x = newx;
  112. y = newy;
  113. }
  114. }
  115.  
  116. T angle(const point& other) const{
  117. point self = this->normalized();
  118. point oth = other.normalized();
  119. if(self == oth)
  120. return 0;
  121. point origin = {0,0};
  122.  
  123. T cos_alpha = self % oth;
  124. return fabs(acos(cos_alpha));
  125. }
  126. };
  127.  
  128. struct line{
  129. //DO NOT MODIFY FOR ANY REASONS!!!
  130. //Create copies instead
  131. point dir;
  132. point perp;
  133. T a,b,c;
  134.  
  135. line(T a, T b, T c) : perp(a,b), dir(0,0) {
  136. assert(!(a == 0 && b == 0));
  137. perp.normalize();
  138. dir = perp.rotated_rad(-M_PI/2);
  139. this->c = c / hypot(a,b);
  140. this->a = perp.x;
  141. this->b = perp.y;
  142. }
  143.  
  144. line(point p1, point p2) : perp(0,0), dir(p2-p1) {
  145. assert(p1 != p2);
  146. dir.normalize();
  147. perp = dir.rotated_rad(M_PI/2);
  148. a = perp.x;
  149. b = perp.y;
  150. c = -(perp % p1);
  151. }
  152.  
  153. T q() const{
  154. return -c / b;
  155. }
  156.  
  157. T m() const{
  158. return -a / b;
  159. }
  160.  
  161. bool inLine(const point &p) const{
  162. return fabs(dir.x * p.x + dir.y * p.y + c) <= EPS;
  163. }
  164.  
  165. line operator-() const{
  166. line inv(-a,-b,-c);
  167. return inv;
  168. }
  169.  
  170. bool operator==(const line &o) const{
  171. if(fabs(a) - fabs(o.a) > EPS)
  172. return false;
  173. line tmp(o);
  174. if(a != o.a)
  175. tmp = -tmp;
  176.  
  177. if(fabs(a - tmp.a) <= EPS && fabs(b - tmp.b) <= EPS && fabs(c - tmp.c) <= EPS)
  178. return true;
  179. return false;
  180. }
  181.  
  182. T signed_distance(const point &p) const{
  183. return perp % p + c;
  184. }
  185.  
  186. point project(const point &p) const{
  187. T dist = signed_distance(p);
  188. return p - (perp * dist);
  189. }
  190.  
  191. line parallel(const point &p) const{
  192. T dist = signed_distance(p);
  193. line res = *this;
  194. res.c -= dist;
  195. return res;
  196. }
  197.  
  198. point intersect(const line &s) const{
  199. line l = *this;
  200. T d1 = l.a * s.b - s.a * l.b;
  201. //Check parallelism
  202. if(fabs(d1) <= EPS)
  203. return {-INF, -INF};
  204. T d2 = -d1;
  205. return { (s.c * l.b - l.c * s.b) / d1, (s.c * l.a - l.c * s.a) / d2 };
  206. }
  207. };
  208.  
  209. struct segment{
  210. point start, end;
  211.  
  212. segment(point p1, point p2) : start(p1), end(p2) { }
  213.  
  214. bool lies(const point &a) const{
  215. point b = start;
  216. point c = end;
  217. line l(b,c);
  218. T dist = l.signed_distance(a);
  219. if(!(fabs(dist) <= EPS))
  220. return false;
  221.  
  222. point ba = a-b;
  223. point bc = c-b;
  224. point ca = a-c;
  225. point cb = b-c;
  226.  
  227. if(ba % bc < 0)
  228. return false;
  229. if(ca % cb < 0)
  230. return false;
  231. return true;
  232. }
  233.  
  234. point intersect(const segment &s2) const{
  235. segment s1 = *this;
  236. line l1(s1.start, s1.end);
  237. line l2(s2.start, s2.end);
  238.  
  239. point inter = l1.intersect(l2);
  240. point inf(-INF, -INF);
  241. //parallel
  242. if(inter == inf){
  243. //check endpoints
  244. if(l1 == l2){
  245. bool ok1 = s1.lies(s2.start);
  246. bool ok2 = s1.lies(s2.end);
  247. bool ok3 = s2.lies(s1.start);
  248. bool ok4 = s2.lies(s1.end);
  249.  
  250. if(ok1)
  251. return s2.start;
  252. if(ok2)
  253. return s2.end;
  254. if(ok3)
  255. return s1.start;
  256. if(ok4)
  257. return s1.end;
  258.  
  259. return inf;
  260. }
  261. return inf;
  262. }
  263.  
  264. //check if s1 and s2 have inter
  265. if(s1.lies(inter) && s2.lies(inter))
  266. return inter;
  267. return inf;
  268. }
  269.  
  270. T distance(const point &p) const{
  271. line l(start, end);
  272. point projection = l.project(p);
  273. if(lies(projection))
  274. return fabs(l.signed_distance(p));
  275. return min(start.distance(p), end.distance(p));
  276. }
  277. };
  278.  
  279. struct half_plain{
  280. line l;
  281.  
  282. half_plain(line l) : l(l) { }
  283.  
  284. bool inPlain(const point &p) const{
  285. return l.dir.x * p.x + l.dir.y * p.y + l.c >= 0;
  286. }
  287. };
  288.  
  289. struct circle{
  290. point centre;
  291. T radius;
  292.  
  293. circle(point centre, T radius) : centre(centre), radius(radius) {}
  294.  
  295. //point must be outside circle
  296. segment tangent_points(const point &p) const{
  297. //I dont p.distance(centre) cause of precision
  298. T pc_squared = fabs(p.x - centre.x)*fabs(p.x - centre.x) + fabs(p.y - centre.y)*fabs(p.y - centre.y);
  299. T ca_squared = radius*radius;
  300. T pa_squared = pc_squared - ca_squared;
  301. T pc = p.distance(centre);
  302.  
  303. T dc = (pa_squared - ca_squared - pc_squared) / (-2 * pc);
  304. T pd = pc - dc;
  305.  
  306. point pc_vec = centre - p;
  307. pc_vec.normalize();
  308. pc_vec = pc_vec * pd;
  309. point d = pc_vec + p;
  310. point da_vec = d.rotated_rad(M_PI/2);
  311. point db_vec = d.rotated_rad(-M_PI/2);
  312. da_vec.normalize();
  313. db_vec.normalize();
  314.  
  315. T da = sqrt(pa_squared - pd*pd);
  316. da_vec = da_vec * da;
  317. point a = da_vec + d;
  318. db_vec = db_vec * da;
  319. point b = db_vec + d;
  320.  
  321. return {a,b};
  322. }
  323.  
  324. bool is_segment_intersecting(const segment &s) const{
  325. T dd = s.distance(centre);
  326. if(fabs(dd - radius) < EPS)
  327. return isTangentIntersection; //tangent intersection
  328. if(dd > radius)
  329. return false;
  330. return true;
  331. }
  332.  
  333. //a and b are in circonference
  334. T arc(const point &a, const point &b){
  335. point ca_vec = a - centre;
  336. point cb_vec = b - centre;
  337. T angle = ca_vec.angle(cb_vec);
  338. return radius * angle;
  339. }
  340. };
  341.  
  342. int main(){k
  343.  
  344. return 0;
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement