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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| #include <bits/stdc++.h> using namespace std; typedef double db; const db eps = 1e-8; const db inf = 1e20; const db pi = acos(-1.0);
int sgn(db x){ if(fabs(x) < eps)return 0; if(x < 0)return -1; else return 1; }
struct Point{ db x, y; Point(){} Point(db _x, db _y){ x = _x; y = _y; } void input(){ scanf("%lf%lf", &x, &y); } bool operator == (Point b)const{ return sgn(x-b.x) == 0 && sgn(y-b.y) == 0; } Point operator -(const Point &b)const{ return Point(x-b.x, y-b.y); } db operator ^(const Point &b)const{ return x*b.y - y*b.x; } db operator *(const Point &b)const{ return x*b.x + y*b.y; } db dis(Point p){ return hypot(x-p.x, y-p.y); } Point operator +(const Point &b)const{ return Point(x+b.x, y+b.y); } Point operator *(const db &k)const{ return Point(x*k, y*k); } Point operator /(const db &k)const{ return Point(x/k, y/k); } };
struct Line{ Point s,e; Line(){} Line(Point _s,Point _e){ s = _s; e = _e; } bool pointonseg(Point p){ return sgn((p-s)^(e-s)) == 0 && sgn((p-s) * (p-e)) <= 0; } Point crosspoint(Line v){ double a1 = (v.e-v.s)^(s-v.s); double a2 = (v.e-v.s)^(e-v.s); return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1)); } };
Point get_mid(Point a, Point b) { return (a + b) * 0.5; }
int main() { int T; scanf("%d", &T); while(T--) { Point a, b, c, p; Line ab, bc, ac; a.input(), b.input(), c.input(), p.input(); ab = Line(a, b); bc = Line(b, c); ac = Line(a, c); db area = fabs((b - a) ^ (c - a) * 0.5); if(ab.pointonseg(p)) { if(a.dis(p) < b.dis(p)) { Point l = c, r = b; Point mid = get_mid(l, r); int times = 1000; while(times--) { mid = get_mid(l, r); db s = fabs((mid - p) ^ (b - p)); if(sgn(s - area) > 0) { l = mid; } else if(sgn(s - area) == 0) { break; } else { r = mid; } } printf("%.10lf %.10lf\n", mid.x, mid.y); } else { Point l = c, r = a; Point mid = get_mid(l, r); int times = 1000; while(times--) { mid = get_mid(l, r); db s = fabs((mid - p) ^ (a - p)); if(sgn(s - area) > 0) { l = mid; } else if(sgn(s - area) == 0) { break; } else { r = mid; } } printf("%.10lf %.10lf\n", mid.x, mid.y); } } else if(bc.pointonseg(p)) { if(b.dis(p) < c.dis(p)) { Point l = a, r = c; Point mid = get_mid(l, r); int times = 1000; while(times--) { mid = get_mid(l, r); db s = fabs((mid - p) ^ (c - p)); if(sgn(s - area) > 0) { l = mid; } else if(sgn(s - area) == 0) { break; } else { r = mid; } } printf("%.10lf %.10lf\n", mid.x, mid.y); } else { Point l = a, r = b; Point mid = get_mid(l, r); int times = 1000; while(times--) { mid = get_mid(l, r); db s = fabs((mid - p) ^ (b - p)); if(sgn(s - area) > 0) { l = mid; } else if(sgn(s - area) == 0) { break; } else { r = mid; } } printf("%.10lf %.10lf\n", mid.x, mid.y); } } else if(ac.pointonseg(p)) { if(a.dis(p) < c.dis(p)) { Point l = b, r = c; Point mid = get_mid(l, r); int times = 1000; while(times--) { mid = get_mid(l, r); db s = fabs((mid - p) ^ (c - p)); if(sgn(s - area) > 0) { l = mid; } else if(sgn(s - area) == 0) { break; } else { r = mid; } } printf("%.10lf %.10lf\n", mid.x, mid.y); } else { Point l = b, r = a; Point mid = get_mid(l, r); int times = 1000; while(times--) { mid = get_mid(l, r); db s = fabs((mid - p) ^ (a - p)); if(sgn(s - area) > 0) { l = mid; } else if(sgn(s - area) == 0) { break; } else { r = mid; } } printf("%.10lf %.10lf\n", mid.x, mid.y); } } else { printf("-1\n"); } }
return 0; }
|