UVa 10263
来源:互联网 发布:javascript是基于对象 编辑:程序博客网 时间:2024/06/01 18:38
题目:已知一个点M,以及一个由N条线段构成的曲线(N+1个点),求曲线上到M最近的点坐标。
分析:计算几何。点到线段距离。
点到线短距离:1.求点到直线距离;
2.判断垂足是否在线段上:在则为答案;不在则端点为答案;
枚举线段,找到最小距离对应的点即可;
说明:雅思考了6.0,还点再考~~~~(>_<)~~~~。
#include <stdio.h>#include <stdlib.h>#include <math.h>const double eps = 1e-6;typedef struct _point{double x, y;}point;point M, P1, P2;point new_point(double x, double y){point ans;ans.x = x;ans.y = y;return ans;}double distance(point p, point q){return sqrt((p.x-q.x)*(p.x-q.x) + (p.y-q.y)*(p.y-q.y));}point foot_point(point m, point p, point q) // Ax + by + C = 0{double A1 = p.x-q.x, B1 = p.y-q.y, C1 = (q.y-p.y)*m.y+(q.x-p.x)*m.x;double A2 = q.y-p.y, B2 = p.x-q.x, C2 = (q.x-p.x)*p.y+(p.y-q.y)*p.x;double x = (C2*B1-C1*B2)/(A1*B2-A2*B1); double y = (C2*A1-C1*A2)/(A2*B1-A1*B2);return new_point(x, y);}bool on_line(point m, point p, point q){return (p.x-m.x)*(q.x-m.x) <= eps && (p.y-m.y)*(q.y-m.y) <= eps;}point closest_point_on_segement(point m, point p, point q){point ans = foot_point(m, p, q);if (!on_line(ans, p, q)) {if (distance(m, p) - distance(m, q) > eps) {return q;}else {return p;}}else {return ans;}}int main(){int n;while (~scanf("%lf%lf", &M.x, &M.y)) {scanf("%d%lf%lf", &n, &P1.x, &P1.y);point ans = P1;double min_dist = distance(M, P1);while (n --) {scanf("%lf%lf", &P2.x, &P2.y);point Q = closest_point_on_segement(M, P1, P2);if (distance(M, ans) - distance(M, Q) > eps) {ans = Q;}P1 = P2;}printf("%.4lf\n%.4lf\n", ans.x, ans.y);}return 0;}
阅读全文
0 0
- UVa 10263
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- leetcode设计类题目
- C++字符串连接,输入和输出
- 欢迎使用CSDN-markdown编辑器
- 自动化测试:behave
- Git设置本地理
- UVa 10263
- leetcode 101. Symmetric Tree BFS广度优先遍历
- 类之间关系
- node.js中render和send
- Externalizable
- 基于sse的消息单向推送机制
- Summing up Powers LightOJ
- PAT 1119. Pre- and Post-order Traversals (30)
- 化学快录3.0 免费版下载附使用手册