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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include<bits/stdc++.h>
using namespace std;
const double eps = 1e-8; const double PI = 4 * atan2(1, 1); const double INF = 1e16; inline int sgn(double x){ if(fabs(x) < eps) return 0; else if(x > 0) return 1; else return -1; } inline int cmp(double x, double y){ return sgn(x-y); }
struct Vector{ double x, y; Vector() {} Vector(double x, double y):x(x), y(y){} void read(){ scanf("%lf%lf", &x, &y); } }; typedef Vector Point; Vector operator + (Vector A, Vector B){ return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Vector A, Vector B){ return Vector(A.x - B.x, A.y - B.y); } Vector operator * (double k, Vector A){ return Vector(k * A.x, k * A.y); } Vector operator * (Vector A, double k){ return k * A; } Vector operator / (Vector A, double k){ return Vector(A.x / k, A.y / k); } bool operator < (const Vector &A, const Vector &B){ return cmp(A.x, B.x) == 0 ? cmp(A.y, B.y) < 0 : cmp(A.x, B.x) < 0; } bool operator == (const Vector &A, const Vector &B){ return (cmp(A.x, B.x) == 0) && (cmp(A.y, B.y) == 0); }
double operator * (Vector A, Vector B){ return A.x * B.x + A.y * B.y; }
double operator ^ (Vector A, Vector B){ return A.x * B.y - A.y * B.x; } double Length(Vector A){ return sqrt(A * A); }
Vector Rotate(Vector A, double rad){ double co = cos(rad), si = sin(rad); return Vector(A.x * co - A.y * si, A.x * si + A.y * co); }
bool ToTheLeft(Vector A, Vector B){ return sgn(A ^ B) > 0; }
struct Line{ Point p; Vector v; double ang; Line() {} Line(Point p, Vector v):p(p), v(v){ ang = atan2(v.y, v.x); } Line(double a, double b, double c){ if(sgn(a) == 0) p = Point(0, -c/b), v = Vector(1, 0); else if(sgn(b) == 0) p = Point(-c/a, 0), v = Vector(0, 1); else p = Point(0, -c/b), v = Vector(-b, a); } Point getPoint(double t){ return p + v * t; } bool operator < (const Line &L) const{ return ang < L.ang; } }; bool PointOnLine(Point p, Line l){ return sgn(l.v ^ (p-l.p)) == 0; } bool PointOnRight(Point p, Line l){ return sgn(l.v ^ (p-l.p)) < 0; } Point GetLineIntersection(Line l1, Line l2){ Vector u = l1.p - l2.p; double t = (l2.v ^ u) / (l1.v ^ l2.v); return l1.p + l1.v * t; }
Point P; Vector v; double L; int k;
inline void solve1(){ double y = -(k/2) * (sqrt(3)/2) * L; Line l(Point(0, y), Vector(1, 0)); Line route(P, v); Point inter = GetLineIntersection(l, route); if(k & 1){ printf("%.8f\n", Length(inter - P) / Length(v)); return; } else{ int kl = 0, kr = 0; Line ll, lr; if(k % 4 == 0){ kl = floor(inter.x / L - 0.5), kr = ceil(inter.x / L - 0.5); while(cmp((kl+1)*L+L/2, inter.x) < 0) kl++; while(cmp((kr-1)*L+L/2, inter.x) > 0) kr--; if(kl != kr-1) puts("Something wrong1!"); ll = Line(Point(kl*L+L/2, y), Vector(1, sqrt(3))); lr = Line(Point(kr*L+L/2, y), Vector(-1, sqrt(3))); } else{ kl = floor(inter.x / L), kr = ceil(inter.x / L); while(cmp((kl+1)*L, inter.x) < 0) kl++; while(cmp((kr-1)*L, inter.x) > 0) kr--; if(kl != kr-1) puts("Something wrong2!"); ll = Line(Point(kl*L, y), Vector(1, sqrt(3))); lr = Line(Point(kr*L, y), Vector(-1, sqrt(3))); }
Point interl = GetLineIntersection(ll, route), interr = GetLineIntersection(lr, route); if(cmp(interl.y, y) >= 0 && cmp(interl.y, y+sqrt(3)/2*L) <= 0){ printf("%.8f\n", Length(interl - P) / Length(v)); return; } else if(cmp(interr.y, y) >= 0 && cmp(interr.y, y+sqrt(3)/2*L) <= 0){ printf("%.8f\n", Length(interr - P) / Length(v)); return; } else puts("Something wrong3!"); } }
inline void solve2(){ P.x += L/2; P = Rotate(P, -PI/3), v = Rotate(v, -PI/3);
double y = -((k+1)/2) * (sqrt(3)/2) * L; Line l(Point(0, y), Vector(1, 0)); Line route(P, v); Point inter = GetLineIntersection(l, route); if(!(k & 1)){ printf("%.8f\n", Length(inter - P) / Length(v)); return; } else{ int kl = 0, kr = 0; Line ll, lr; if((k+1) % 4){ kl = floor(inter.x / L - 0.5), kr = ceil(inter.x / L - 0.5); while(cmp((kl+1)*L+L/2, inter.x) < 0) kl++; while(cmp((kr-1)*L+L/2, inter.x) > 0) kr--; if(kl != kr-1) puts("Something wrong4!"); ll = Line(Point(kl*L+L/2, y), Vector(1, sqrt(3))); lr = Line(Point(kr*L+L/2, y), Vector(-1, sqrt(3))); } else{ kl = floor(inter.x / L), kr = ceil(inter.x / L); while(cmp((kl+1)*L, inter.x) < 0) kl++; while(cmp((kr-1)*L, inter.x) > 0) kr--; if(kl != kr-1) puts("Something wrong5!"); ll = Line(Point(kl*L, y), Vector(1, sqrt(3))); lr = Line(Point(kr*L, y), Vector(-1, sqrt(3))); }
Point interl = GetLineIntersection(ll, route), interr = GetLineIntersection(lr, route); if(cmp(interl.y, y) >= 0 && cmp(interl.y, y+sqrt(3)/2*L) <= 0){ printf("%.8f\n", Length(interl - P) / Length(v)); return; } else if(cmp(interr.y, y) >= 0 && cmp(interr.y, y+sqrt(3)/2*L) <= 0){ printf("%.8f\n", Length(interr - P) / Length(v)); return; } else puts("Something wrong6!"); } }
int main(){ int T; for(scanf("%d", &T); T; T--){ scanf("%lf%lf%lf%lf%lf%d", &L, &P.x, &P.y, &v.x, &v.y, &k); Point O(0, sqrt(3)/6*L); Vector PA = Point(-L/2, 0) - P, PB = Point(L/2, 0) - P, PC = Point(0, sqrt(3)/2*L) - P; Vector CA(-1, -sqrt(3)), CB(1, -sqrt(3)); if(ToTheLeft(PB, v) && ToTheLeft(v, PC)){ v = Rotate(v, -2*PI/3); P = O + Rotate(P - O, -2*PI/3); } else if(ToTheLeft(PC, v) && ToTheLeft(v, PA)){ v = Rotate(v, 2*PI/3); P = O + Rotate(P - O, 2*PI/3); }
if(sgn(CA ^ v) < 0) P.x = -P.x, v.x = -v.x; if(sgn(CA ^ v) >= 0 && sgn(v ^ CB) >= 0) solve1(); else solve2(); } return 0; }
|