基础题,直线间关系

  1. #include <iostream>
  2. #include <math.h>
  3. #include <iomanip>
  4.  
  5. #define eps 1e-8
  6. #define zero(x) (((x)>0?(x):-(x))<eps)
  7.  
  8. #define pi acos(-1.0)
  9.  
  10. struct point
  11. {
  12. double x, y;
  13. };
  14.  
  15. struct line
  16. {
  17. point a, b;
  18. };
  19. struct point3
  20. {
  21. double x, y, z;
  22. };
  23. struct line3
  24. {
  25. point3 a, b;
  26. };
  27. struct plane3
  28. {
  29. point3 a, b, c;
  30. };
  31.  
  32. //计算cross product (P1-P0)x(P2-P0)
  33. double xmult(point p1, point p2, point p0)
  34. {
  35. return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
  36. }
  37. //计算dot product (P1-P0).(P2-P0)
  38. double dmult(point p1, point p2, point p0)
  39. {
  40. return (p1.x - p0.x)*(p2.x - p0.x) + (p1.y - p0.y)*(p2.y - p0.y);
  41. }
  42. //计算cross product U . V
  43. point3 xmult(point3 u, point3 v)
  44. {
  45. point3 ret;
  46. ret.x = u.y*v.z - v.y*u.z;
  47. ret.y = u.z*v.x - u.x*v.z;
  48. ret.z = u.x*v.y - u.y*v.x;
  49. return ret;
  50. }
  51. //计算dot product U . V
  52. double dmult(point3 u, point3 v)
  53. {
  54. return u.x*v.x + u.y*v.y + u.z*v.z;
  55. }
  56.  
  57. //两点距离
  58. double distance(point p1, point p2)
  59. {
  60. return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
  61. }
  62.  
  63. //判三点共线
  64. bool dots_inline(point p1, point p2, point p3)
  65. {
  66. return zero(xmult(p1, p2, p3));
  67. }
  68.  
  69. //判点是否在线段上,包括端点
  70. bool dot_online_in(point p, line l)
  71. {
  72. return zero(xmult(p, l.a, l.b)) && (l.a.x - p.x)*(l.b.x - p.x) < eps && (l.a.y - p.y)*(l.b.y - p.y) < eps;
  73. }
  74.  
  75. //判点是否在线段上,不包括端点
  76. bool dot_online_ex(point p, line l)
  77. {
  78. return dot_online_in(p, l) && (!zero(p.x - l.a.x) || !zero(p.y - l.a.y)) && (!zero(p.x - l.b.x) || !zero(p.y - l.b.y));
  79. }
  80.  
  81. //判两点在线段同侧,点在线段上返回0
  82. bool same_side(point p1, point p2, line l)
  83. {
  84. return xmult(l.a, p1, l.b)*xmult(l.a, p2, l.b) > eps;
  85. }
  86.  
  87. //判两点在线段异侧,点在线段上返回0
  88. bool opposite_side(point p1, point p2, line l)
  89. {
  90. return xmult(l.a, p1, l.b)*xmult(l.a, p2, l.b) < -eps;
  91. }
  92.  
  93. //判两直线平行
  94. bool parallel(line u, line v)
  95. {
  96. return zero((u.a.x - u.b.x)*(v.a.y - v.b.y) - (v.a.x - v.b.x)*(u.a.y - u.b.y));
  97. }
  98.  
  99. //判两直线垂直
  100. bool perpendicular(line u, line v)
  101. {
  102. return zero((u.a.x - u.b.x)*(v.a.x - v.b.x) + (u.a.y - u.b.y)*(v.a.y - v.b.y));
  103. }
  104.  
  105. //判两线段相交,包括端点和部分重合
  106. bool intersect_in(line u, line v)
  107. {
  108. if (!dots_inline(u.a, u.b, v.a) || !dots_inline(u.a, u.b, v.b))
  109. return !same_side(u.a, u.b, v) && !same_side(v.a, v.b, u);
  110. return dot_online_in(u.a, v) || dot_online_in(u.b, v) || dot_online_in(v.a, u) || dot_online_in(v.b, u);
  111. }
  112.  
  113. //判两线段相交,不包括端点和部分重合
  114. bool intersect_ex(line u, line v)
  115. {
  116. return opposite_side(u.a, u.b, v) && opposite_side(v.a, v.b, u);
  117. }
  118.  
  119. //计算两直线交点,注意事先判断直线是否平行!
  120. //线段交点请另外判线段相交(同时还是要判断是否平行!)
  121. point intersection(line u, line v)
  122. {
  123. point ret = u.a;
  124. double t = ((u.a.x - v.a.x)*(v.a.y - v.b.y) - (u.a.y - v.a.y)*(v.a.x - v.b.x)) / ((u.a.x - u.b.x)*(v.a.y - v.b.y) - (u.a.y - u.b.y)*(v.a.x - v.b.x));
  125. ret.x += (u.b.x - u.a.x)*t;
  126. ret.y += (u.b.y - u.a.y)*t;
  127. return ret;
  128. }
  129. point intersection(point u1, point u2, point v1, point v2)
  130. {
  131. point ret = u1;
  132. double t = ((u1.x - v1.x)*(v1.y - v2.y) - (u1.y - v1.y)*(v1.x - v2.x)) / ((u1.x - u2.x)*(v1.y - v2.y) - (u1.y - u2.y)*(v1.x - v2.x));
  133. ret.x += (u2.x - u1.x)*t;
  134. ret.y += (u2.y - u1.y)*t;
  135. return ret;
  136. }
  137. //点到直线上的最近点
  138. point ptoline(point p, line l)
  139. {
  140. point t = p;
  141. t.x += l.a.y - l.b.y, t.y += l.b.x - l.a.x;
  142. return intersection(p, t, l.a, l.b);
  143. }
  144.  
  145. //点到直线距离
  146. double disptoline(point p, line l)
  147. {
  148. return fabs(xmult(p, l.a, l.b)) / distance(l.a, l.b);
  149. }
  150.  
  151. //点到线段上的最近点
  152. point ptoseg(point p, line l)
  153. {
  154. point t = p;
  155. t.x += l.a.y - l.b.y, t.y += l.b.x - l.a.x;
  156. if (xmult(l.a, t, p)*xmult(l.b, t, p) > eps)
  157. return distance(p, l.a) < distance(p, l.b) ? l.a : l.b;
  158. return intersection(p, t, l.a, l.b);
  159. }
  160.  
  161. //点到线段距离
  162. double disptoseg(point p, line l)
  163. {
  164. point t = p;
  165. t.x += l.a.y - l.b.y, t.y += l.b.x - l.a.x;
  166. if (xmult(l.a, t, p)*xmult(l.b, t, p) > eps)
  167. return distance(p, l.a) < distance(p, l.b) ? distance(p, l.a) : distance(p, l.b);
  168. return fabs(xmult(p, l.a, l.b)) / distance(l.a, l.b);
  169. }
  170.  
  171. //矢量V 以P 为顶点逆时针旋转angle 并放大scale 倍
  172. point rotate(point v, point p, double angle, double scale)
  173. {
  174. point ret = p;
  175. v.x -= p.x, v.y -= p.y;
  176. p.x = scale*cos(angle);
  177. p.y = scale*sin(angle);
  178. ret.x += v.x*p.x - v.y*p.y;
  179. ret.y += v.x*p.y + v.y*p.x;
  180. return ret;
  181. }
  182.  
  183. //计算三角形面积,输入三顶点
  184. double area_triangle(point p1, point p2, point p3)
  185. {
  186. return fabs(xmult(p1, p2, p3)) / 2;
  187. }
  188.  
  189. //计算三角形面积,输入三边长
  190. double area_triangle(double a, double b, double c)
  191. {
  192. double s = (a + b + c) / 2;
  193. return sqrt(s*(s - a)*(s - b)*(s - c));
  194. }
  195.  
  196. //计算多边形面积,顶点按顺时针或逆时针给出
  197. double area_polygon(int n, point* p)
  198. {
  199. double s1 = 0, s2 = 0;
  200. int i;
  201. for (i = 0; i < n; i++)
  202. s1 += p[(i + 1)%n].y*p[i].x, s2 += p[(i + 1)%n].y*p[(i + 2)%n].x;
  203. return fabs(s1 - s2) / 2;
  204. }
  205.  
  206. //计算圆心角lat 表示纬度,-90<=w<=90,lng 表示经度
  207. //返回两点所在大圆劣弧对应圆心角,0<=angle<=pi
  208. double angle(double lng1, double lat1, double lng2, double lat2)
  209. {
  210. double dlng = fabs(lng1 - lng2)*pi / 180;
  211. while (dlng >= pi + pi)
  212. dlng -= pi + pi;
  213. if (dlng > pi)
  214. dlng = pi + pi - dlng;
  215. lat1 *= pi / 180, lat2 *= pi / 180;
  216. return acos(cos(lat1)*cos(lat2)*cos(dlng) + sin(lat1)*sin(lat2));
  217. }
  218.  
  219. //计算距离,r 为球半径
  220. double line_dist(double r, double lng1, double lat1, double lng2, double lat2)
  221. {
  222. double dlng = fabs(lng1 - lng2)*pi / 180;
  223. while (dlng >= pi + pi)
  224. dlng -= pi + pi;
  225. if (dlng > pi)
  226. dlng = pi + pi - dlng;
  227. lat1 *= pi / 180, lat2 *= pi / 180;
  228. return r*sqrt(2 - 2 * (cos(lat1)*cos(lat2)*cos(dlng) + sin(lat1)*sin(lat2)));
  229. }
  230.  
  231. //计算球面距离,r 为球半径
  232. inline double sphere_dist(double r, double lng1, double lat1, double lng2, double lat2)
  233. {
  234. return r*angle(lng1, lat1, lng2, lat2);
  235. }
  236.  
  237. //外心
  238. point circumcenter(point a, point b, point c)
  239. {
  240. line u, v;
  241. u.a.x = (a.x + b.x) / 2;
  242. u.a.y = (a.y + b.y) / 2;
  243. u.b.x = u.a.x - a.y + b.y;
  244. u.b.y = u.a.y + a.x - b.x;
  245. v.a.x = (a.x + c.x) / 2;
  246. v.a.y = (a.y + c.y) / 2;
  247. v.b.x = v.a.x - a.y + c.y;
  248. v.b.y = v.a.y + a.x - c.x;
  249. return intersection(u, v);
  250. }
  251.  
  252. //内心
  253. point incenter(point a, point b, point c)
  254. {
  255. line u, v;
  256. double m, n;
  257. u.a = a;
  258. m = atan2(b.y - a.y, b.x - a.x);
  259. n = atan2(c.y - a.y, c.x - a.x);
  260. u.b.x = u.a.x + cos((m + n) / 2);
  261. u.b.y = u.a.y + sin((m + n) / 2);
  262. v.a = b;
  263. m = atan2(a.y - b.y, a.x - b.x);
  264. n = atan2(c.y - b.y, c.x - b.x);
  265. v.b.x = v.a.x + cos((m + n) / 2);
  266. v.b.y = v.a.y + sin((m + n) / 2);
  267. return intersection(u, v);
  268. }
  269.  
  270. //垂心
  271. point perpencenter(point a, point b, point c)
  272. {
  273. line u, v;
  274. u.a = c;
  275. u.b.x = u.a.x - a.y + b.y;
  276. u.b.y = u.a.y + a.x - b.x;
  277. v.a = b;
  278. v.b.x = v.a.x - a.y + c.y;
  279. v.b.y = v.a.y + a.x - c.x;
  280. return intersection(u, v);
  281. }
  282. //重心
  283. //到三角形三顶点距离的平方和最小的点
  284. //三角形内到三边距离之积最大的点
  285. point barycenter(point a, point b, point c)
  286. {
  287. line u, v;
  288. u.a.x = (a.x + b.x) / 2;
  289. u.a.y = (a.y + b.y) / 2;
  290. u.b = c;
  291. v.a.x = (a.x + c.x) / 2;
  292. v.a.y = (a.y + c.y) / 2;
  293. v.b = b;
  294. return intersection(u, v);
  295. }
  296.  
  297. //费马点
  298. //到三角形三顶点距离之和最小的点
  299. point fermentpoint(point a, point b, point c)
  300. {
  301. point u, v;
  302. double step = fabs(a.x) + fabs(a.y) + fabs(b.x) + fabs(b.y) + fabs(c.x) + fabs(c.y);
  303. int i, j, k;
  304. u.x = (a.x + b.x + c.x) / 3;
  305. u.y = (a.y + b.y + c.y) / 3;
  306. while (step > 1e-10)
  307. {
  308. for (k = 0; k < 10; step /= 2, k++)
  309. {
  310. for (i = -1; i <= 1; i++)
  311. {
  312. for (j = -1; j <= 1; j++)
  313. {
  314. v.x = u.x + step*i;
  315. v.y = u.y + step*j;
  316. if (distance(u, a) + distance(u, b) + distance(u, c) > distance(v, a) + distance(v, b) + distance(v, c))
  317. {
  318. u = v;
  319. }
  320. }
  321. }
  322. }
  323. }
  324. return u;
  325. }
  326.  
  327. //矢量差 U - V
  328. point3 subt(point3 u, point3 v)
  329. {
  330. point3 ret;
  331. ret.x = u.x - v.x;
  332. ret.y = u.y - v.y;
  333. ret.z = u.z - v.z;
  334. return ret;
  335. }
  336.  
  337. ///三维///
  338. //取平面法向量
  339. point3 pvec(plane3 s)
  340. {
  341. return xmult(subt(s.a, s.b), subt(s.b, s.c));
  342. }
  343. point3 pvec(point3 s1, point3 s2, point3 s3)
  344. {
  345. return xmult(subt(s1, s2), subt(s2, s3));
  346. }
  347.  
  348. //两点距离,单参数取向量大小
  349. double distance(point3 p1, point3 p2)
  350. {
  351. return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) + (p1.z - p2.z)*(p1.z - p2.z));
  352. }
  353.  
  354. //向量大小
  355. double vlen(point3 p)
  356. {
  357. return sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
  358. }
  359.  
  360. //判三点共线
  361. bool dots_inline(point3 p1, point3 p2, point3 p3)
  362. {
  363. return vlen(xmult(subt(p1, p2), subt(p2, p3))) < eps;
  364. }
  365.  
  366. //判四点共面
  367. bool dots_onplane(point3 a, point3 b, point3 c, point3 d)
  368. {
  369. return zero(dmult(pvec(a, b, c), subt(d, a)));
  370. }
  371.  
  372. //判点是否在线段上,包括端点和共线
  373. bool dot_online_in(point3 p, line3 l)
  374. {
  375. return zero(vlen(xmult(subt(p, l.a), subt(p, l.b)))) && (l.a.x - p.x)*(l.b.x - p.x) < eps && (l.a.y - p.y)*(l.b.y - p.y) < eps && (l.a.z - p.z)*(l.b.z - p.z) < eps;
  376. }
  377.  
  378. //判点是否在线段上,不包括端点
  379. bool dot_online_ex(point3 p, line3 l)
  380. {
  381. return dot_online_in(p, l) && (!zero(p.x - l.a.x) || !zero(p.y - l.a.y) || !zero(p.z - l.a.z)) && (!zero(p.x - l.b.x) || !zero(p.y - l.b.y) || !zero(p.z - l.b.z));
  382. }
  383.  
  384. //判点是否在空间三角形上,包括边界,三点共线无意义
  385. bool dot_inplane_in(point3 p, plane3 s)
  386. {
  387. return zero(vlen(xmult(subt(s.a, s.b), subt(s.a, s.c))) - vlen(xmult(subt(p, s.a), subt(p, s.b))) - vlen(xmult(subt(p, s.b), subt(p, s.c))) - vlen(xmult(subt(p, s.c), subt(p, s.a))));
  388. }
  389.  
  390. //判点是否在空间三角形上,不包括边界,三点共线无意义
  391. bool dot_inplane_ex(point3 p, plane3 s)
  392. {
  393. return dot_inplane_in(p, s) && vlen(xmult(subt(p, s.a), subt(p, s.b))) > eps && vlen(xmult(subt(p, s.b), subt(p, s.c))) > eps && vlen(xmult(subt(p, s.c), subt(p, s.a))) > eps;
  394. }
  395.  
  396. //判两点在线段同侧,点在线段上返回0,不共面无意义
  397. bool same_side(point3 p1, point3 p2, line3 l)
  398. {
  399. return dmult(xmult(subt(l.a, l.b), subt(p1, l.b)), xmult(subt(l.a, l.b), subt(p2, l.b))) > eps;
  400. }
  401.  
  402. //判两点在线段异侧,点在线段上返回0,不共面无意义
  403. bool opposite_side(point3 p1, point3 p2, line3 l)
  404. {
  405. return dmult(xmult(subt(l.a, l.b), subt(p1, l.b)), xmult(subt(l.a, l.b), subt(p2, l.b))) < -eps;
  406. }
  407.  
  408. //判两点在平面同侧,点在平面上返回0
  409. bool same_side(point3 p1, point3 p2, plane3 s)
  410. {
  411. return dmult(pvec(s), subt(p1, s.a))*dmult(pvec(s), subt(p2, s.a)) > eps;
  412. }
  413. bool same_side(point3 p1, point3 p2, point3 s1, point3 s2, point3 s3)
  414. {
  415. return dmult(pvec(s1, s2, s3), subt(p1, s1))*dmult(pvec(s1, s2, s3), subt(p2, s1)) > eps;
  416. }
  417.  
  418. //判两点在平面异侧,点在平面上返回0
  419. bool opposite_side(point3 p1, point3 p2, plane3 s)
  420. {
  421. return dmult(pvec(s), subt(p1, s.a))*dmult(pvec(s), subt(p2, s.a)) < -eps;
  422. }
  423. bool opposite_side(point3 p1, point3 p2, point3 s1, point3 s2, point3 s3)
  424. {
  425. return dmult(pvec(s1, s2, s3), subt(p1, s1))*dmult(pvec(s1, s2, s3), subt(p2, s1)) < -eps;
  426. }
  427. //判两直线平行
  428. bool parallel(line3 u, line3 v)
  429. {
  430. return vlen(xmult(subt(u.a, u.b), subt(v.a, v.b))) < eps;
  431. }
  432.  
  433. //判两平面平行
  434. bool parallel(plane3 u, plane3 v)
  435. {
  436. return vlen(xmult(pvec(u), pvec(v))) < eps;
  437. }
  438.  
  439. //判直线与平面平行
  440. bool parallel(line3 l, plane3 s)
  441. {
  442. return zero(dmult(subt(l.a, l.b), pvec(s)));
  443. }
  444. bool parallel(point3 l1, point3 l2, point3 s1, point3 s2, point3 s3)
  445. {
  446. return zero(dmult(subt(l1, l2), pvec(s1, s2, s3)));
  447. }
  448.  
  449. //判两直线垂直
  450. bool perpendicular(line3 u, line3 v)
  451. {
  452. return zero(dmult(subt(u.a, u.b), subt(v.a, v.b)));
  453. }
  454.  
  455. //判两平面垂直
  456. bool perpendicular(plane3 u, plane3 v)
  457. {
  458. return zero(dmult(pvec(u), pvec(v)));
  459. }
  460.  
  461. //判直线与平面平行
  462. bool perpendicular(line3 l, plane3 s)
  463. {
  464. return vlen(xmult(subt(l.a, l.b), pvec(s))) < eps;
  465. }
  466.  
  467. //判两线段相交,包括端点和部分重合
  468. bool intersect_in(line3 u, line3 v)
  469. {
  470. if (!dots_onplane(u.a, u.b, v.a, v.b))
  471. return 0;
  472. if (!dots_inline(u.a, u.b, v.a) || !dots_inline(u.a, u.b, v.b))
  473. return !same_side(u.a, u.b, v) && !same_side(v.a, v.b, u);
  474. return dot_online_in(u.a, v) || dot_online_in(u.b, v) || dot_online_in(v.a, u) || dot_online_in(v.b, u);
  475. }
  476.  
  477. //判两线段相交,不包括端点和部分重合
  478. bool intersect_ex(line3 u, line3 v)
  479. {
  480. return dots_onplane(u.a, u.b, v.a, v.b) && opposite_side(u.a, u.b, v) && opposite_side(v.a, v.b, u);
  481. }
  482.  
  483. //判线段与空间三角形相交,包括交于边界和(部分)包含
  484. bool intersect_in(line3 l, plane3 s)
  485. {
  486. return !same_side(l.a, l.b, s) && !same_side(s.a, s.b, l.a, l.b, s.c) && !same_side(s.b, s.c, l.a, l.b, s.a) && !same_side(s.c, s.a, l.a, l.b, s.b);
  487. }
  488.  
  489. //判线段与空间三角形相交,不包括交于边界和(部分)包含
  490. bool intersect_ex(line3 l, plane3 s)
  491. {
  492. return opposite_side(l.a, l.b, s) && opposite_side(s.a, s.b, l.a, l.b, s.c) && opposite_side(s.b, s.c, l.a, l.b, s.a) && opposite_side(s.c, s.a, l.a, l.b, s.b);
  493. }
  494.  
  495. //计算两直线交点,注意事先判断直线是否共面和平行!
  496. //线段交点请另外判线段相交(同时还是要判断是否平行!)
  497. point3 intersection(line3 u, line3 v)
  498. {
  499. point3 ret = u.a;
  500. double t = ((u.a.x - v.a.x)*(v.a.y - v.b.y) - (u.a.y - v.a.y)*(v.a.x - v.b.x))
  501. / ((u.a.x - u.b.x)*(v.a.y - v.b.y) - (u.a.y - u.b.y)*(v.a.x - v.b.x));
  502. ret.x += (u.b.x - u.a.x)*t;
  503. ret.y += (u.b.y - u.a.y)*t;
  504. ret.z += (u.b.z - u.a.z)*t;
  505. return ret;
  506. }
  507.  
  508. //计算直线与平面交点,注意事先判断是否平行,并保证三点不共线!
  509. //线段和空间三角形交点请另外判断
  510. point3 intersection(line3 l, plane3 s)
  511. {
  512. point3 ret = pvec(s);
  513. double t = (ret.x*(s.a.x - l.a.x) + ret.y*(s.a.y - l.a.y) + ret.z*(s.a.z - l.a.z)) / (ret.x*(l.b.x - l.a.x) + ret.y*(l.b.y - l.a.y) + ret.z*(l.b.z - l.a.z));
  514. ret.x = l.a.x + (l.b.x - l.a.x)*t;
  515. ret.y = l.a.y + (l.b.y - l.a.y)*t;
  516. ret.z = l.a.z + (l.b.z - l.a.z)*t;
  517. return ret;
  518. }
  519. point3 intersection(point3 l1, point3 l2, point3 s1, point3 s2, point3 s3)
  520. {
  521. point3 ret = pvec(s1, s2, s3);
  522. double t = (ret.x*(s1.x - l1.x) + ret.y*(s1.y - l1.y) + ret.z*(s1.z - l1.z)) /
  523. (ret.x*(l2.x - l1.x) + ret.y*(l2.y - l1.y) + ret.z*(l2.z - l1.z));
  524. ret.x = l1.x + (l2.x - l1.x)*t;
  525. ret.y = l1.y + (l2.y - l1.y)*t;
  526. ret.z = l1.z + (l2.z - l1.z)*t;
  527. return ret;
  528. }
  529.  
  530. //计算两平面交线,注意事先判断是否平行,并保证三点不共线!
  531. line3 intersection(plane3 u, plane3 v)
  532. {
  533. line3 ret;
  534. ret.a = parallel(v.a, v.b, u.a, u.b, u.c) ? intersection(v.b, v.c, u.a, u.b, u.c) : intersection(v.a, v.b, u.a, u.b, u.c);
  535. ret.b = parallel(v.c, v.a, u.a, u.b, u.c) ? intersection(v.b, v.c, u.a, u.b, u.c) : intersection(v.c, v.a, u.a, u.b, u.c);
  536. return ret;
  537. }
  538. line3 intersection(point3 u1, point3 u2, point3 u3, point3 v1, point3 v2, point3 v3)
  539. {
  540. line3 ret;
  541. ret.a = parallel(v1, v2, u1, u2, u3) ? intersection(v2, v3, u1, u2, u3) : intersection(v1, v2, u1, u2, u3);
  542. ret.b = parallel(v3, v1, u1, u2, u3) ? intersection(v2, v3, u1, u2, u3) : intersection(v3, v1, u1, u2, u3);
  543. return ret;
  544. }
  545. //点到直线距离
  546. double ptoline(point3 p, line3 l)
  547. {
  548. return vlen(xmult(subt(p, l.a), subt(l.b, l.a))) / distance(l.a, l.b);
  549. }
  550.  
  551. //点到平面距离
  552. double ptoplane(point3 p, plane3 s)
  553. {
  554. return fabs(dmult(pvec(s), subt(p, s.a))) / vlen(pvec(s));
  555. }
  556.  
  557. //直线到直线距离
  558. double linetoline(line3 u, line3 v)
  559. {
  560. point3 n = xmult(subt(u.a, u.b), subt(v.a, v.b));
  561. return fabs(dmult(subt(u.a, v.a), n)) / vlen(n);
  562. }
  563.  
  564. //两直线夹角cos 值
  565. double angle_cos(line3 u, line3 v)
  566. {
  567. return dmult(subt(u.a, u.b), subt(v.a, v.b)) / vlen(subt(u.a, u.b)) / vlen(subt(v.a, v.b));
  568. }
  569.  
  570. //两平面夹角cos 值
  571. double angle_cos(plane3 u, plane3 v)
  572. {
  573. return dmult(pvec(u), pvec(v)) / vlen(pvec(u)) / vlen(pvec(v));
  574. }
  575.  
  576. //直线平面夹角sin 值
  577. double angle_sin(line3 l, plane3 s)
  578. {
  579. return dmult(subt(l.a, l.b), pvec(s)) / vlen(subt(l.a, l.b)) / vlen(pvec(s));
  580. }
  581.  
  582. int main()
  583. {
  584. int t;
  585. std::cin >> t;
  586. std::cout << "INTERSECTING LINES OUTPUT" << std::endl;
  587. while (t--)
  588. {
  589. line a, b;
  590. std::cin >> a.a.x >> a.a.y >> a.b.x >> a.b.y >> b.a.x >> b.a.y >> b.b.x >> b.b.y;
  591.  
  592. if (xmult(a.a, a.b, b.a) == 0 && xmult(a.a, a.b, b.b) == 0)
  593. {
  594. std::cout << "LINE" << std::endl;
  595. }
  596. else if (parallel(a, b))
  597. {
  598. std::cout << "NONE" << std::endl;
  599. }
  600. else
  601. {
  602. point temp;
  603. temp = intersection(a, b);
  604. std::cout << std::fixed << std::setprecision(2) << "POINT" << ' ' << temp.x << ' ' << temp.y << std::endl;
  605. }
  606. }
  607. std::cout << "END OF OUTPUT" << std::endl;
  608. }

poj1269的更多相关文章

  1. POJ1269:Intersecting Lines(判断两条直线的关系)

    题目:POJ1269 题意:给你两条直线的坐标,判断两条直线是否共线.平行.相交,若相交,求出交点. 思路:直线相交判断.如果相交求交点. 首先先判断是否共线,之后判断是否平行,如果都不是就直接求交点 ...

  2. poj1269 (叉积求直线的交点)

    题目链接:https://vjudge.net/problem/POJ-1269 题意:给出4个顶点,表示两条直线,求这两条直线的相交情况,重合输出LINE,平行输出NONE,相交于一点输出该点的距离 ...

  3. POJ1269+直线相交

    求相交点 /* 线段相交模板:判相交.求交点 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...

  4. poj1269(直线交点)

    传送门:Intersecting Lines 题意:给出N组直线,每组2条直线,求出直线是否相交.如果共线则输出LINE,相交则输入点坐标,否则输出NONE. 分析:模板裸题,直接上模板... #in ...

  5. poj1269计算几何直线和直线的关系

    We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a p ...

  6. POJ1269 Intersecting Lines[线段相交 交点]

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15145   Accepted: 66 ...

  7. POJ1269(KB13-D 计算几何)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16681   Accepted: 71 ...

  8. poj1269 intersecting lines【计算几何】

    We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a p ...

  9. [poj1269]Intersecting Lines

    题目大意:求两条直线的交点坐标. 解题关键:叉积的运用. 证明: 直线的一般方程为$F(x) = ax + by + c = 0$.既然我们已经知道直线的两个点,假设为$(x_0,y_0), (x_1 ...

随机推荐

  1. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  2. kprobe 内核模块

    代码来自于linux内核sample/kprobe kprobe_example.c /* * NOTE: This example is works on x86 and powerpc. * He ...

  3. C++程序中应增加STL、运算和字符串的头文件

    #include <complex> //模板类complex的标准头文件 #include <valarray> //模板类valarray的标准头文件 #include & ...

  4. [C#]设置或取消开机启动(注册表形式)

    原文:[C#]设置或取消开机启动(注册表形式) 使用代码: 代码效果:

  5. 使用Jenkins来构建Docker容器

    使用Jenkins来构建Docker容器(Ubuntu 14.04) 当开发更新了代码,提交到Gitlab上,然后由测试人员触发Jenkins,于是一个应用的新版本就被构建了.听起来貌似很简单,dua ...

  6. CentOS 使用yum命令安装Java SDK(openjdk)

    CentOS 6.X 和 5.X 自带有OpenJDK runtime environment  (openjdk).它是一个在linux上实现开源的java 平台.CentOS  yum 命令 安装 ...

  7. LeetCode——Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  8. ASP.NET 5 Identity

    ASP.NET 5 Identity   “跌倒了”指的是这一篇博文:爱与恨的抉择:ASP.NET 5+EntityFramework 7 如果想了解 ASP.NET Identity 的“历史”及“ ...

  9. IE8下div中2个button仅仅显示一个

    IE8下div中2个button仅仅显示一个,代码例如以下: <div id="adviceType" style="display: none;" &g ...

  10. html5 Canvas画图3:1px线条模糊问题

    点击查看原文地址: html5 Canvas画图3:1px线条模糊问题 本文属于<html5 Canvas画图系列教程> 接上一篇canvas画线条教程 上次我们讲到,canvas有时候会 ...