-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.java
137 lines (105 loc) · 3.56 KB
/
main.java
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package gsh;
import java.util.Arrays;
import java.util.Random;
import edu.princeton.cs.algs4.StdDraw;
//主函数
public class main {
//计算平面内点对的最近距离
public static double closestPair(final points[] fuck)
{
double result;
double midresult=Double.MAX_VALUE;
double d;
points []mid=new points[fuck.length];
if(fuck.length==2)
{
return Math.sqrt(points.distancePow2(fuck[0], fuck[1]));
}
if(fuck.length==3)
{
result=points.distancePow2(fuck[0], fuck[1])>points.distancePow2(fuck[0], fuck[2])?points.distancePow2(fuck[0],fuck[2]):points.distancePow2(fuck[0], fuck[1]);
result=result>points.distancePow2(fuck[1], fuck[2])?points.distancePow2(fuck[1], fuck[2]):result;
return Math.sqrt(result);
}
else {
if(fuck.length%2==0)
{
double lresult;
double rresult;
points[]right =new points[fuck.length/2];
points[]left =new points[fuck.length/2];
for(int i=0;i<fuck.length/2;i++)
{
left[i]=new points(fuck[i].getX(),fuck[i].getY());
right[i]=new points(fuck[fuck.length/2+i].getX(),fuck[fuck.length/2+i].getY());
}
lresult=closestPair(left);
rresult=closestPair(right);
d= lresult>rresult?rresult:lresult;
}
else{
double lresult;
double rresult;
points[]left =new points[fuck.length/2];
points[]right =new points[fuck.length/2+1];
for(int i=0;i<fuck.length/2;i++)
{
left[i]=new points(fuck[i].getX(),fuck[i].getY());
right[i]=new points(fuck[fuck.length/2+i].getX(),fuck[fuck.length/2+i].getY());
}
right[fuck.length/2]=new points(fuck[fuck.length-1].getX(),fuck[fuck.length-1].getY());
lresult=closestPair(left);
rresult=closestPair(right);
d= lresult>rresult?rresult:lresult;
}
//局部过程变量
for(int i1=0;i1<fuck.length;i1++)
System.out.println(fuck[i1]);
System.out.println(fuck.length);
int midx =fuck[fuck.length/2-1].getX();
System.out.println("midx "+midx);
int i;
int j=0;
for(i=0;i<fuck.length;i++)
{
if(fuck[i].getX()>midx-d&&fuck[i].getX()<midx+d)
{
mid[j++]=new points(fuck[i].getX(),fuck[i].getY());
}
}
result=d;
for(int p=0;p<mid.length;p++)
{
for (int q=p+1;q<mid.length;q++)
{
midresult=Math.sqrt(points.distancePow2(mid[p],mid[q]));
if(midresult<d)
{
result=midresult;
}
}
}
return result;
}
}
public static void main(String[] args) {
int N=10;
StdDraw.setPenRadius(0.01);
StdDraw.setXscale(0,10);
StdDraw.setYscale(0,10);
points[] fuck=new points[5];
Random gsh=new Random();
for(int i=0;i<5;i++)
fuck[i]=new points(gsh.nextInt(10),gsh.nextInt(10));
Arrays.sort(fuck);
for(int i=0;i<5;i++)
{
StdDraw.point(fuck[i].getX(), fuck[i].getY());
System.out.println(fuck[i]);
}
System.out.println(fuck.length);
double bitch=closestPair(fuck);
System.out.println("closest_pair: "+bitch);
//System.out.println(4%2);
}
}