Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <map>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cmath>
  7. using namespace std;
  8.  
  9. int nextInt() {
  10.     int x;
  11.     scanf("%d", &x);
  12.     return x;
  13. }
  14. double nextDouble() {
  15.     double x;
  16.     scanf("%lf", &x);
  17.     return x;
  18. }
  19.  
  20. struct Point {
  21.     double x, y;
  22.     Point (double x = 0, double y = 0) : x(x), y(y) {};
  23.     Point operator - (Point op) { return Point(x - op.x, y - op.y); }
  24.     Point operator + (Point op) { return Point(x + op.x, y + op.y); }
  25.     Point operator * (double op) { return Point(x * op, y * op); }
  26.     double operator * (Point op) { return x * op.x + x * op.x; }
  27.     double operator % (Point op) { return x * op.y - y * op.x; }
  28.     double length2() { return x * x + y * y; }
  29.     double length() { return sqrt(x * x + y * y); }
  30. };
  31.  
  32. Point nextPoint() {
  33.     double x = nextDouble();
  34.     double y = nextDouble();
  35.     return Point(x, y);
  36. }
  37.  
  38. typedef vector<Point> Poly;
  39.  
  40. const double eps = 1e-9;
  41.  
  42. double totalLength(Poly &p) {
  43.     double res = 0;
  44.     for (int i = 1; i < p.size(); ++i) {
  45.         res += (p[i] - p[i - 1]).length();
  46.     }
  47.     return res;
  48. }
  49.  
  50. Point getLPoint(Poly &p, double f) {
  51.     double L = totalLength(p);
  52.     double alp = 0;
  53.     for (int i = 1; i < p.size(); ++i) {
  54.         double l = (p[i] - p[i - 1]).length();
  55.         double beta = alp + l;
  56.         if (alp <= f + eps && f <= beta + eps) {
  57.             Point res = p[i - 1] + (p[i] - p[i - 1]) * ((f - alp) / l);
  58.             return res;
  59.         }
  60.         alp = beta;
  61.     }
  62.     cerr << "botva!2"; throw 0;
  63. }
  64.  
  65. Point convert(Point p, Point s0, Point e0, Point s1, Point e1) {
  66.     Point a0 = e0 - s0;
  67.     Point a1 = e1 - s1;
  68.     Point v0 = p - s0;
  69.     double A = a1.length2() / a0.length2() * (a0 * v0);
  70.     double B = a1.length2() / a0.length2() * (a0 % v0);
  71.     double D = a1.length2();
  72.     double Dx = a1.x * B + a1.y * A;
  73.     double Dy = a1.y * B - a1.x * A;
  74.     Point v1(Dx / D, Dy / D);
  75.     Point res = s1 + v1;
  76.     return res;
  77. }
  78.  
  79. Point solve(int d, double f, Poly &p, Point s, Point e) {
  80.     if (d == 0) {
  81.         Point r = getLPoint(p, f);
  82.         r = convert(r, p[0], p.back(), s, e);
  83.         return r;
  84.     }
  85.    
  86.     double L = totalLength(p);
  87.     double alp = 0;
  88.     for (int i = 1; i < p.size(); ++i) {
  89.         double l = (p[i] - p[i - 1]).length();
  90.         cerr << l << endl;
  91.         double beta = alp + l;
  92.         cerr << alp << " " << beta << endl;
  93.         if (alp <= f + eps && f <= beta + eps) {
  94.             Point ns = convert(p[i - 1], s, e, p[i - 1], p[i]);
  95.             Point ne = convert(p[i], s, e, p[i - 1], p[i]);
  96.             return solve(d - 1, (f - alp) / l, p, ns, ne);
  97.         }
  98.         alp = beta;
  99.     }
  100.     cerr << "botva!"; throw 0;
  101. }
  102.  
  103. int main() {
  104.     int c = nextInt();
  105.     while (c--) {
  106.         int n = nextInt();
  107.         Poly p(n);
  108.         for (int i = 0; i < n; ++i) {
  109.             p[i] = nextPoint();
  110.             cerr << p[i].x << " " << p[i].y << endl;
  111.         }
  112.         int d = nextInt();
  113.         double f = nextDouble();
  114.         Point res = solve(d, f, p, p[0], p.back());
  115.         printf("(%.10lf,%.10lf)\n", res.x, res.y);
  116.     }
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement