基础题,注意精度和旋转方向。

  1. #include <iostream>
  2. #include <math.h>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. #define PI acos(-1.0)
  10.  
  11. #define M 100007
  12. #define N 65736
  13.  
  14. const int inf = 0x7f7f7f7f;
  15. const int mod = 1000000007;
  16. const double eps = 1e-6;
  17.  
  18. struct Point
  19. {
  20. double x, y;
  21. Point(double tx = 0, double ty = 0) : x(tx), y(ty){}
  22. };
  23. typedef Point Vtor;
  24. //向量的加减乘除
  25. Vtor operator + (Vtor A, Vtor B) { return Vtor(A.x + B.x, A.y + B.y); }
  26. Vtor operator - (Point A, Point B) { return Vtor(A.x - B.x, A.y - B.y); }
  27. Vtor operator * (Vtor A, double p) { return Vtor(A.x*p, A.y*p); }
  28. Vtor operator / (Vtor A, double p) { return Vtor(A.x / p, A.y / p); }
  29. bool operator < (Point A, Point B) { return A.x < B.x || (A.x == B.x && A.y < B.y); }
  30. int dcmp(double x){ if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }
  31. bool operator == (Point A, Point B) { return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0; }
  32. //向量的点积,长度,夹角
  33. double Dot(Vtor A, Vtor B) { return (A.x*B.x + A.y*B.y); }
  34. double Length(Vtor A) { return sqrt(Dot(A, A)); }
  35. double Angle(Vtor A, Vtor B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
  36. //叉积,三角形面积
  37. double Cross(Vtor A, Vtor B) { return A.x*B.y - A.y*B.x; }
  38. double Area2(Point A, Point B, Point C) { return Cross(B - A, C - A); }
  39. //向量的旋转,求向量的单位法线(即左转90度,然后长度归一)
  40. Vtor Rotate(Vtor A, double rad){ return Vtor(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad)); }
  41. Vtor Normal(Vtor A)
  42. {
  43. double L = Length(A);
  44. return Vtor(-A.y / L, A.x / L);
  45. }
  46. //直线的交点
  47. Point GetLineIntersection(Point P, Vtor v, Point Q, Vtor w)
  48. {
  49. Vtor u = P - Q;
  50. double t = Cross(w, u) / Cross(v, w);
  51. return P + v*t;
  52. }
  53. //点到直线的距离
  54. double DistanceToLine(Point P, Point A, Point B)
  55. {
  56. Vtor v1 = B - A;
  57. return fabs(Cross(P - A, v1)) / Length(v1);
  58. }
  59. //点到线段的距离
  60. double DistanceToSegment(Point P, Point A, Point B)
  61. {
  62. if (A == B) return Length(P - A);
  63. Vtor v1 = B - A, v2 = P - A, v3 = P - B;
  64. if (dcmp(Dot(v1, v2)) < 0) return Length(v2);
  65. else if (dcmp(Dot(v1, v3)) > 0) return Length(v3);
  66. else return fabs(Cross(v1, v2)) / Length(v1);
  67. }
  68. //点到直线的映射
  69. Point GetLineProjection(Point P, Point A, Point B)
  70. {
  71. Vtor v = B - A;
  72. return A + v*Dot(v, P - A) / Dot(v, v);
  73. }
  74.  
  75. //判断线段是否规范相交
  76. bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
  77. {
  78. double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
  79. c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
  80. return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
  81. }
  82. //判断点是否在一条线段上
  83. bool OnSegment(Point P, Point a1, Point a2)
  84. {
  85. return dcmp(Cross(a1 - P, a2 - P)) == 0 && dcmp(Dot(a1 - P, a2 - P)) < 0;
  86. }
  87. //多边形面积
  88. double PolgonArea(Point *p, int n)
  89. {
  90. double area = 0;
  91. for (int i = 1; i < n - 1; ++i)
  92. area += Cross(p[i] - p[0], p[i + 1] - p[0]);
  93. return area / 2;
  94. }
  95.  
  96. struct Line
  97. {
  98. Point p, b;
  99. Vtor v;
  100. Line(){}
  101. Line(Point a, Point b, Vtor v) : p(a), b(b), v(v) {}
  102. Line(Point p, Vtor v) : p(p), v(v){}
  103. Point point(double t) { return p + v*t; }
  104. };
  105. struct Circle
  106. {
  107. Point c;
  108. double r;
  109. Circle(Point tc, double tr) : c(tc), r(tr){}
  110. Point point(double a)
  111. {
  112. return Point(c.x + cos(a)*r, c.y + sin(a)*r);
  113. }
  114. };
  115. //判断圆与直线是否相交以及求出交点
  116. int getLineCircleIntersection(Line L, Circle C, double &t1, double &t2, vector<Point> &sol)
  117. {
  118. // printf(">>>>>>>>>>>>>>>>>>>>>>>>\n");
  119. //注意sol没有清空哦
  120. double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
  121. double e = a*a + c*c, f = 2 * (a*b + c*d), g = b*b + d*d - C.r*C.r;
  122. double delta = f*f - 4.0*e*g;
  123. if (dcmp(delta) < 0) return 0;
  124. else if (dcmp(delta) == 0)
  125. {
  126. t1 = t2 = -f / (2.0*e);
  127. sol.push_back(L.point(t1));
  128. return 1;
  129. }
  130. t1 = (-f - sqrt(delta)) / (2.0 * e); sol.push_back(L.point(t1));
  131. t2 = (-f + sqrt(delta)) / (2.0 * e); sol.push_back(L.point(t2));
  132. return 2;
  133. }
  134. //判断并求出两圆的交点
  135. double angle(Vtor v) { return atan2(v.y, v.x); }
  136. int getCircleIntersection(Circle C1, Circle C2, vector<Point> &sol)
  137. {
  138. double d = Length(C1.c - C2.c);
  139. // 圆心重合
  140. if (dcmp(d) == 0)
  141. {
  142. if (dcmp(C1.r - C2.r) == 0) return -1; // 两圆重合
  143. return 0; // 包含
  144. }
  145.  
  146. // 圆心不重合
  147. if (dcmp(C1.r + C2.r - d) < 0) return 0; // 相离
  148. if (dcmp(fabs(C1.r - C2.r) - d) > 0) return 0; // 包含
  149.  
  150. double a = angle(C2.c - C1.c);
  151. double da = acos((C1.r*C1.r + d*d - C2.r*C2.r) / (2 * C1.r*d));
  152. Point p1 = C1.point(a - da), p2 = C1.point(a + da);
  153. sol.push_back(p1);
  154. if (p1 == p2) return 1;
  155. sol.push_back(p2);
  156. return 2;
  157. }
  158. //求点到圆的切线
  159. int getTangents(Point p, Circle C, Vtor* v)
  160. {
  161. Vtor u = C.c - p;
  162. double dis = Length(u);
  163. if (dis < C.r) return 0;
  164. else if (dcmp(dis - C.r) == 0)
  165. {
  166. v[0] = Rotate(u, PI / 2.0);
  167. return 1;
  168. }
  169. else
  170. {
  171. double ang = asin(C.r / dis);
  172. v[0] = Rotate(u, -ang);
  173. v[1] = Rotate(u, +ang);
  174. return 2;
  175. }
  176. }
  177. //求两圆的切线
  178. int getCircleTangents(Circle A, Circle B, Point *a, Point *b)
  179. {
  180. int cnt = 0;
  181. if (A.r < B.r) { swap(A, B); swap(a, b); }
  182. //圆心距的平方
  183. double d2 = (A.c.x - B.c.x)*(A.c.x - B.c.x) + (A.c.y - B.c.y)*(A.c.y - B.c.y);
  184. double rdiff = A.r - B.r;
  185. double rsum = A.r + B.r;
  186. double base = angle(B.c - A.c);
  187. //重合有无限多条
  188. if (d2 == 0 && dcmp(A.r - B.r) == 0) return -1;
  189. //内切
  190. if (dcmp(d2 - rdiff*rdiff) == 0)
  191. {
  192. a[cnt] = A.point(base);
  193. b[cnt] = B.point(base); cnt++;
  194. return 1;
  195. }
  196. //有外公切线
  197. double ang = acos((A.r - B.r) / sqrt(d2));
  198. a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
  199. a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;
  200.  
  201. //一条内切线
  202. if (dcmp(d2 - rsum*rsum) == 0)
  203. {
  204. a[cnt] = A.point(base); b[cnt] = B.point(PI + base); cnt++;
  205. }//两条内切线
  206. else if (dcmp(d2 - rsum*rsum) > 0)
  207. {
  208. double ang = acos((A.r + B.r) / sqrt(d2));
  209. a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
  210. a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;
  211. }
  212. return cnt;
  213. }
  214.  
  215. //**********************************
  216. Circle CircumscribedCircle(Point A, Point B, Point C)
  217. {
  218. Point tmp1 = Point((B.x + C.x) / 2.0, (B.y + C.y) / 2.0);
  219. Vtor u = C - tmp1;
  220. u = Rotate(u, PI / 2.0);
  221. Point tmp2 = Point((A.x + C.x) / 2.0, (A.y + C.y) / 2.0);
  222. Vtor v = C - tmp2;
  223. v = Rotate(v, -PI / 2.0);
  224. Point c = GetLineIntersection(tmp1, u, tmp2, v);
  225. double r = Length(C - c);
  226. return Circle(c, r);
  227. }
  228. //得到法向量就得到了这个方向上的向量了
  229. //Circle work1(Point p1, Point p2, Point p3)
  230. // {
  231. // Vtor nor1 = Normal(p1 - p2);
  232. // Vtor nor2 = Normal(p2 - p3);
  233. // Point mid1 = (p1 + p2) / 2.0;
  234. // Point mid2 = (p2 + p3) / 2.0;
  235. // Point O = GetLineIntersection(mid1, nor1, mid2, nor2);
  236. // double r = Length(O - p1);
  237. // return Circle(O, r);
  238. //}
  239.  
  240. //不知道为什么我按常规的求法就是不对
  241. //Circle InscribedCircle(Point A,Point B,Point C)
  242. //{
  243. // Vtor u = A - B;
  244. // Vtor v = C - B;
  245. // double ang = Angle(u,v);
  246. // Vtor vv= Rotate(v,ang / 2.0);
  247. // u = A - C;
  248. // v = B - C;
  249. // ang = Angle(u,v);
  250. // Vtor uu = Rotate(u,ang / 2.0);
  251. // Point c = GetLineIntersection(B,vv,C,uu);
  252. // double r = DistanceToLine(c,A,C);
  253. // return Circle(c,r);
  254. //}
  255. Circle work2(Point p1, Point p2, Point p3) {
  256. Vtor v11 = p2 - p1;
  257. Vtor v12 = p3 - p1;
  258. Vtor v21 = p1 - p2;
  259. Vtor v22 = p3 - p2;
  260. double ang1 = (angle(v11) + angle(v12)) / 2.0;
  261. double ang2 = (angle(v21) + angle(v22)) / 2.0;
  262. Vtor vec1 = Vtor(cos(ang1), sin(ang1));
  263. Vtor vec2 = Vtor(cos(ang2), sin(ang2));
  264. Point O = GetLineIntersection(p1, vec1, p2, vec2);
  265. double r = DistanceToLine(O, p1, p2);
  266. return Circle(O, r);
  267. }
  268. vector<Point> solve4(Point A, Point B, double r, Point C)
  269. {
  270. Vtor normal = Normal(B - A);
  271. normal = normal / Length(normal) * r;
  272. vector<Point> ans;
  273. double t1 = 0, t2 = 0;
  274. Vtor tA = A + normal, tB = B + normal;
  275. getLineCircleIntersection(Line(tA, tB, tB - tA), Circle(C, r), t1, t2, ans);
  276. tA = A - normal, tB = B - normal;
  277. getLineCircleIntersection(Line(tA, tB, tB - tA), Circle(C, r), t1, t2, ans);
  278. return ans;
  279. }
  280. vector<Point> solve5(Point A, Point B, Point C, Point D, double r)
  281. {
  282. Line lines[5];
  283. Vtor normal = Normal(B - A) * r;
  284. Point ta, tb, tc, td;
  285. ta = A + normal, tb = B + normal;
  286. lines[0] = Line(ta, tb, tb - ta);
  287. ta = A - normal, tb = B - normal;
  288. lines[1] = Line(ta, tb, tb - ta);
  289.  
  290. normal = Normal(D - C) * r;
  291. tc = C + normal, td = D + normal;
  292. lines[2] = Line(tc, td, td - tc);
  293. tc = C - normal, td = D - normal;
  294. lines[3] = Line(tc, td, td - tc);
  295. vector<Point> ans;
  296. ans.push_back(GetLineIntersection(lines[0].p, lines[0].v, lines[2].p, lines[2].v));
  297. ans.push_back(GetLineIntersection(lines[0].p, lines[0].v, lines[3].p, lines[3].v));
  298. ans.push_back(GetLineIntersection(lines[1].p, lines[1].v, lines[2].p, lines[2].v));
  299. ans.push_back(GetLineIntersection(lines[1].p, lines[1].v, lines[3].p, lines[3].v));
  300. return ans;
  301. }
  302. vector<Point> solve6(Circle C1, Circle C2, double r)
  303. {
  304. vector<Point> vc;
  305. getCircleIntersection(Circle(C1.c, C1.r + r), Circle(C2.c, C2.r + r), vc);
  306. return vc;
  307. }
  308.  
  309. string op;
  310. double x[10];
  311.  
  312. int main()
  313. {
  314. // Read();
  315.  
  316. while (cin >> op)
  317. {
  318. if (op == "CircumscribedCircle")
  319. {
  320. for (int i = 0; i < 6; ++i) cin >> x[i];
  321. Circle ans = CircumscribedCircle(Point(x[0], x[1]), Point(x[2], x[3]), Point(x[4], x[5]));
  322. // Circle ans = work1(Point(x[0],x[1]),Point(x[2],x[3]),Point(x[4],x[5]));
  323. printf("(%.6lf,%.6lf,%.6lf)\n", ans.c.x, ans.c.y, ans.r);
  324. }
  325. else if (op == "InscribedCircle")
  326. {
  327. for (int i = 0; i < 6; ++i) cin >> x[i];
  328. // Circle ans = InscribedCircle(Point(x[0],x[1]),Point(x[2],x[3]),Point(x[4],x[5]));
  329. Circle ans = work2(Point(x[0], x[1]), Point(x[2], x[3]), Point(x[4], x[5]));
  330. printf("(%.6lf,%.6lf,%.6lf)\n", ans.c.x, ans.c.y, ans.r);
  331. }
  332. else if (op == "TangentLineThroughPoint")
  333. {
  334. for (int i = 0; i < 5; ++i) cin >> x[i];
  335. Vtor vc[5];
  336. int len = getTangents(Point(x[3], x[4]), Circle(Point(x[0], x[1]), x[2]), vc);
  337. double tmp[5];
  338. for (int i = 0; i < len; ++i)
  339. {
  340. double ang = angle(vc[i]);
  341. if (ang < 0) ang += PI;
  342. ang = fmod(ang, PI);
  343. tmp[i] = ang * 180 / PI;
  344. }
  345. sort(tmp, tmp + len);
  346. printf("[");
  347. for (int i = 0; i < len; ++i)
  348. {
  349. printf("%.6lf", tmp[i]);
  350. if (i != len - 1) printf(",");
  351. }
  352. printf("]\n");
  353. }
  354. else if (op == "CircleThroughAPointAndTangentToALineWithRadius")
  355. {
  356. for (int i = 0; i < 7; ++i) cin >> x[i];
  357. vector<Point> vc = solve4(Point(x[2], x[3]), Point(x[4], x[5]), x[6], Point(x[0], x[1]));
  358. sort(vc.begin(), vc.end());
  359. printf("[");
  360. for (size_t i = 0; i < vc.size(); ++i)
  361. {
  362. printf("(%.6lf,%.6lf)", vc[i].x, vc[i].y);
  363. if (i != vc.size() - 1) printf(",");
  364. }
  365. printf("]\n");
  366. }
  367. else if (op == "CircleTangentToTwoLinesWithRadius")
  368. {
  369. for (int i = 0; i < 9; ++i) cin >> x[i];
  370. vector<Point> vc = solve5(Point(x[0], x[1]), Point(x[2], x[3]), Point(x[4], x[5]), Point(x[6], x[7]), x[8]);
  371. sort(vc.begin(), vc.end());
  372. printf("[");
  373. for (size_t i = 0; i < vc.size(); ++i)
  374. {
  375. printf("(%.6lf,%.6lf)", vc[i].x, vc[i].y);
  376. if (i != vc.size() - 1) printf(",");
  377. }
  378. printf("]\n");
  379. }
  380. else
  381. {
  382. for (int i = 0; i < 7; ++i) cin >> x[i];
  383. vector<Point> vc = solve6(Circle(Point(x[0], x[1]), x[2]), Circle(Point(x[3], x[4]), x[5]), x[6]);
  384. sort(vc.begin(), vc.end());
  385. printf("[");
  386. for (size_t i = 0; i < vc.size(); ++i)
  387. {
  388. printf("(%.6lf,%.6lf)", vc[i].x, vc[i].y);
  389. if (i != vc.size() - 1) printf(",");
  390. }
  391. printf("]\n");
  392. }
  393. }
  394. }

UVa12304的更多相关文章

  1. UVA-12304 2D Geometry 110 in 1! (有关圆的基本操作)

    UVA-12304 2D Geometry 110 in 1! 该问题包含以下几个子问题 CircumscribedCircle x1 y1 x2 y2 x3 y3 : 三角形外接圆 Inscribe ...

  2. UVA12304 2D Geometry 110 in 1! 计算几何

    计算几何: 堆几何模版就能够了. . .. Description Problem E 2D Geometry 110 in 1! This is a collection of 110 (in bi ...

  3. 【做题】UVA-12304——平面计算集合六合一

    可真是道恶心题-- 首先翻译一下6个任务: 给出一个三角形,求它的外界圆. 给出一个三角形,求它的内接圆. 给出一个圆和一个点,求过这个点的切线的倾斜角\(\alpha \in [0,180)\).( ...

  4. UVA-12304 Race(递推)

    题目大意:求n个人比赛的所有可能的名次种数.比如:n=2时,有A第一B第二.B第一A第二.AB并列第一三种名次. 题目解析:既然是比赛,总有第一名.第一名的人数可能是i (1≤i≤n),则剩下待定的人 ...

  5. UVa12304(计算几何中圆的基本操作)

    断断续续写了250多行的模拟,其间被其他事情打扰,总共花了一天才AC吧~ 这道题目再次让我明白,有些事情看起来很难,实际上并没有我们想象中的那么难.当然了我主要指的不是这个题的难度…… 也是初学计算几 ...

  6. UVA 12304 - 2D Geometry 110 in 1! - [平面几何基础题大集合][计算几何模板]

    题目链接:https://cn.vjudge.net/problem/UVA-12304 题意: 作为题目大合集,有以下一些要求: ①给出三角形三个点,求三角形外接圆,求外接圆的圆心和半径. ②给出三 ...

  7. How do you add?(递推)

    题意:求将n分为k个数相加的种数. 如:n=20,k=2,则可分为: 0+20=20 1+19=20 2+18=20 ....... 20 +0=20 共21种方案. 解析:令f(n,m)表示将n分为 ...

  8. 【kuangbin】计算几何部分最新模板

    二维几何部分 // `计算几何模板` ; const double inf = 1e20; const double pi = acos(-1.0); ; //`Compares a double t ...

随机推荐

  1. 转:嵌入式linux启动时运行的inittab文件

    嵌入式系统下的linux启动配置文件,不同与普通的PC linux启动配置,启动相关文件与文件的内容也要少得多.嵌入式系统下的linux启动过程一般是:     1 在bootloader中制定各种要 ...

  2. Cannot find module formidable

    今天按照例子学习安装 formidable 即使我-g全局安装,也不行.. 于是搜到了这句话.. 解决了 我也遇到这个问题,困扰了我一天,现在找到解决办法了.我在看这篇入门教程:http://node ...

  3. MFC浅析(4) CObject浅析

    MFC CObject浅析 1.CObject简要声明 2.CRuntimeClass结构 3.RUNTIME_CLASS 4.DYNAMIC支持 5.DYNCREATE支持 6.SERIAL支持 C ...

  4. POJ1062 昂贵的聘礼(最短路)

    题目链接. 分析: 一开始以为简单的DFS,直接做,MLE了. 本体应该用最短路径(Dijkstra算法)做. 此题的关键在于等级限制的处理,采用枚举,即假设酋长等级为5,等级限制为2,那么需要枚举等 ...

  5. -_-#微信内置JavaScript API WeixinJSBridge

    微信相关的 js 操作:分享.网络.菜单 微信内置JsAPI之WeixinJSBridge微信WeixinJSBridge API续

  6. 【最长下降子序列】【动态规划】【二分】XMU 1041 Sequence

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1041 题目大意: 一个二维平面,上面n(n<=1 000 000)个点.问至少选 ...

  7. vs2010旗舰版产品密钥

    Microsoft Visual Studio 2010(VS2010)正式版 CDKEY / SN: YCFHQ-9DWCY-DKV88-T2TMH-G7BHP 企业版.旗舰版都适用

  8. javascript 四舍五入

    原生 javascript 中四舍五入的函数 toFixed(n) , n为要保留的小数位数. (0<= n <=20) var num=1.0999; console.log(num.t ...

  9. 五子棋——C++

    最近在学C++,这个是照葫芦画瓢的五子棋C++版- - 依赖SDL_PingGe_1.3,很多实用的函数,类我都封装成DLL了调用起来真是舒服啊.. 不过一方面要对DLL做测试,一方面要开发,一个人还 ...

  10. 《鸟哥Linux私房菜基础学习篇》命令索引

    在学习的过程,由于很多命令平时都用不着,因此做这个索引方便需要时查找.这包括了前两部分.主要是按页码顺序. P118 date:显示日期与时间 cal:显示日历 bc:计算器 P121 [Tab]:命 ...