赞
踩
题目来源:码蹄集
https://matiji.net/exam/brushquestion/595/778/B3FCFEC101BD05189BB74D522E019504
参考程序:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
-
- typedef struct tagPOINT { //点
- int x, y; //坐标值x和y
- } POINT;
- typedef struct tagLINE { //线
- POINT s, e; //线的两端
- } LINE;
- typedef struct tagRECT { //矩形
- POINT lt, rb; //矩形的左上角和右下角
- } RECT;
-
- float distance_pl(POINT p, LINE l) {
- float d = (l.s.y - l.e.y)*p.x + (l.e.x - l.s.x)*p.y + (l.s.x*l.e.y - l.e.x*l.s.y);
- d = fabs(d) / sqrt((l.e.x - l.s.x)*(l.e.x - l.s.x) + (l.e.y - l.s.y)*(l.e.y - l.s.y));
- return d;
- }
-
- float distance_pp(POINT p, POINT l) {
- float d = sqrt((p.x - l.x)*(p.x - l.x) + (p.y - p.y)*(p.y - p.y));
- return d;
- }
-
- int main()
- {
- RECT rect;
- POINT p;
- float d, k1, k2;
- int ret;
- ret = scanf("%d %d", &rect.lt.x, &rect.lt.y);
- ret = scanf("%d %d", &rect.rb.x, &rect.rb.y);
- ret = scanf("%d %d", &p.x, &p.y);
- if (rect.lt.x <= p.x&&p.x <= rect.rb.x && rect.rb.y <= p.y&&p.y <= rect.lt.y) {
- printf("0");
- }
- else {
- LINE l1, l2; float dist_min; POINT tmp;
- //矩形x范围内上方或下方
- if (rect.lt.x <= p.x&&p.x <= rect.rb.x) {
- l1.s = rect.lt;
- l1.e = l1.s; l1.e.x = rect.rb.x;
- dist_min = distance_pl(p, l1);
- l2.s = rect.rb;
- l2.e = l2.s; l1.e.x = rect.lt.x;
- dist_min = fmin(dist_min, distance_pl(p, l2));
- printf("%d", (int)(floor(dist_min)));
- return 0;
- }
-
- //矩形y范围内左侧或右侧
- if (rect.lt.x <= p.x&&p.x <= rect.rb.x) {
- l1.s = rect.lt;
- l1.e = l1.s; l1.e.y = rect.rb.y;
- dist_min = distance_pl(p, l1);
- l2.s = rect.rb;
- l2.e = l2.s; l1.e.x = rect.lt.x;
- dist_min = fmin(dist_min, distance_pl(p, l2));
- printf("%d", (int)(floor(dist_min)));
- return 0;
- }
- //其他情况,取最近的顶点距离作为距离
- dist_min = distance_pp(p, rect.lt);
- dist_min = fmin(dist_min, distance_pp(p, rect.rb));
- tmp = rect.lt; tmp.x = rect.rb.x;
- dist_min = fmin(dist_min, distance_pp(p, tmp));
- tmp = rect.lt; tmp.y = rect.rb.y;
- dist_min = fmin(dist_min, distance_pp(p, tmp));
- printf("%d", (int)(floor(dist_min)));
-
- }
-
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。