Advertisement
cincout

new geometry (в процессе)

Jul 25th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. #define double long double
  7.  
  8. namespace geom
  9. {
  10. // if use the geom use precision and setf(ios::fixed)
  11.  
  12. // conts's
  13.  
  14. double pi = acos(-1);
  15. double eps = 1e-9;
  16. double parallel = 2e18;
  17. double Z = 0;
  18.  
  19. // help's funcs
  20.  
  21. bool is_equal(double &a, double &b)
  22. {
  23. return fabs(a - b) < eps;
  24. }
  25.  
  26. struct line
  27. {
  28. double A, B, C;
  29. line()
  30. {
  31. A = B = C = 0;
  32. }
  33. line(double _A, double _B, double _C)
  34. {
  35. A = _A, B = _B, C = _C;
  36. }
  37. };
  38.  
  39. void normalize(line & x)
  40. {
  41. double hyp = hypot(x.A, x.B);
  42. x.A /= hyp;
  43. x.B /= hyp;
  44. x.C /= hyp;
  45. }
  46.  
  47. struct pt
  48. {
  49. double x, y;
  50. };
  51.  
  52. double get_y(line h, double &x)
  53. {
  54. if (is_equal(h.B, Z))
  55. return Z;
  56. return (-h.C -h.A * x) / h.B;
  57. }
  58.  
  59. double get_x(line h, double &y)
  60. {
  61. if (is_equal(h.A, Z))
  62. return Z;
  63. return (-h.C - h.B * y) / h.A;
  64. }
  65.  
  66. double get_value(line h, pt b)
  67. {
  68. return h.A * b.x + h.B * b.y + h.C;
  69. }
  70.  
  71. void normalize(pt & a)
  72. {
  73. double hyp = hypot(a.x, a.y);
  74. a.x /= hyp;
  75. a.y /= hyp;
  76. }
  77.  
  78. double polar(pt a)
  79. {
  80. double ret = atan2(a.y, a.x);
  81. if (ret < 0)
  82. ret += 2 * pi;
  83. return ret;
  84. }
  85.  
  86. double operator * (pt a, pt b)
  87. {
  88. return a.x * b.y - b.x * a.y;
  89. }
  90.  
  91. double operator ^ (pt a, pt b)
  92. {
  93. return a.x * b.x + a.y * b.y;
  94. }
  95.  
  96. pt operator - (pt a, pt b)
  97. {
  98. return {a.x - b.x, a.y - b.y};
  99. }
  100.  
  101. pt operator + (pt a, pt b)
  102. {
  103. return {a.x + b.x, a.y + b.y};
  104. }
  105.  
  106. pt operator * (pt a, double k)
  107. {
  108. return {a.x * k, a.y * k};
  109. }
  110.  
  111. bool operator == (pt a, pt b)
  112. {
  113. return is_equal(a.x, b.x) && is_equal(a.y, b.y);
  114. }
  115.  
  116. void operator += (pt &a, pt b)
  117. {
  118. a.x += b.x;
  119. a.y += b.y;
  120. }
  121.  
  122. double ang_between(pt a, pt b)
  123. {
  124. return fabs(atan2(a * b, a ^ b));
  125. }
  126.  
  127. double oriented_dist(pt a, line b)
  128. {
  129. normalize(b);
  130. return get_value(b, a);
  131. }
  132.  
  133. double dist(pt a, line b)
  134. {
  135. return fabs(oriented_dist(a, b));
  136. }
  137.  
  138. line to_line(pt a, pt b)
  139. {
  140. return {a.y - b.y, b.x - a.x, a * b};
  141. }
  142.  
  143. double len(pt a)
  144. {
  145. return hypot(a.x, a.y);
  146. }
  147.  
  148. double dist(pt a, pt b)
  149. {
  150. return len(a - b);
  151. }
  152.  
  153. struct ray
  154. {
  155. pt a, b;
  156. ray(pt _a, pt _b)
  157. {
  158. a = _a, b = _b;
  159. }
  160. };
  161.  
  162. line to_line(ray b)
  163. {
  164. return to_line(b.a, b.b);
  165. }
  166.  
  167. bool on_ray(pt a, ray b)
  168. {
  169. pt f = b.b - b.a;
  170. pt c = a - b.a;
  171. double z = f * c;
  172. return is_equal(z, Z);
  173. }
  174.  
  175. pt projection(pt a, line b)
  176. {
  177. pt az = {fabs(b.A), fabs(b.B)};
  178. normalize(az);
  179. a += az * -oriented_dist(a, b);
  180. return a;
  181. }
  182.  
  183. /*double dist(pt a, ray b)
  184. {
  185.  
  186. }*/
  187.  
  188. // in, out
  189. istream& operator >> (istream& in, pt &t) { return in >> t.x >> t.y; }
  190. ostream& operator << (ostream& out, pt t) { return out << t.x << ' ' << t.y; }
  191. istream& operator >> (istream& in, line &t) { return in >> t.A >> t.B >> t.C; }
  192. ostream& operator << (ostream& out, line t) { return out << t.A << ' ' << t.B << ' ' << t.C; ; }
  193. istream& operator >> (istream &in, ray &t) { return in >> t.a >> t.b; }
  194. ostream& operator << (ostream& out, ray t) { return out << t.a << ' ' << t.b; }
  195. }
  196.  
  197. using namespace geom;
  198.  
  199. void solve()
  200. {
  201. pt a;
  202. line c;
  203. cin >> a >> c;
  204. cout << projection(a, c);
  205. }
  206.  
  207. signed main()
  208. {
  209. ios::sync_with_stdio(0), cin.tie(0);
  210. cout.setf(ios::fixed), cout.precision(20);
  211. #ifdef ONLINE_JUDGE
  212. freopen("distance2.in", "r", stdin);
  213. freopen("distance2.out", "w", stdout);
  214. #endif
  215. solve();
  216. return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement