-
Notifications
You must be signed in to change notification settings - Fork 0
/
最接近点对.c
104 lines (92 loc) · 1.64 KB
/
最接近点对.c
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
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define N 4
struct point{
float x;
float y;
};
float distance(struct point *,int n1);
float min(double d1,double d2);
int main()
{
float result;
struct point p1[N];
for(int i=0;i<N;i++)
{
printf("key in %d x and y\n",i);
scanf("%f %f",&p1[i].x,&p1[i].y);
}
result=distance(p1,N);
printf("result is %f\n",result);
//printf("helloworld\n");
return 0;
}
float min(double d1,double d2)
{
if(d1>=d2)
return d2;
else
return d1;
}
float distance(struct point p[],int n1)
{
int n=n1;
if(n=2)
return sqrt(pow((p[1].x-p[0].x),2)+pow((p[1].y-p[0].y),2));
else if(n==1)
return 10000;
int pa,q;
float m,sum,d1,d2,d,tempd,tempdd;//求出所有点的x平均值m
for(int i=0;i<n;i++)
{
sum+=p[i].x;
}
m=(float)sum/n;
//printf("%f",m);
//将点分为两组,大小分别为pa,q,分别放置于ps,py中
for(int i=0;i<n;i++)
{
if(p[i].x<=m)
pa++;
else
q++;
}
struct point ps[pa],py[q];
for(int i=0;i<n;i++)
{
if(i<pa)
ps[i]=p[i];
else
py[i-pa]=p[i];
}
d1=distance(ps,pa);
d2=distance(py,q);
d=min(d1,d2);
tempd=d;
tempdd=d;
//对于ps所有距离m小于d的点,检索py里面距离m小于d且与ps的点纵坐标差不超过2d的点计算距离,
//如果小于d则会保留在tempdd中,最后判断返回值
for(int i=0;i<pa;i++)
{
if((m-ps[i].x)<=d)
{
for(int j=0;i<q;i++)
{
if((py[j].x-m)<=d&&fabs(py[j].y-ps[i].y)<=d)
{
tempd=sqrt(pow((py[j].x-ps[i].x),2)+pow((py[j].y-ps[i].y),2));
if(tempd<d)
{
if(tempdd<tempd)
tempdd=tempd;
}
}
}
}
}
if(tempdd<d)
return tempdd;
else
return d;
}