/
Point2D.java
70 lines (51 loc) · 1.65 KB
/
Point2D.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
package com.kyson.chapter1.section2;
import java.util.ArrayList;
/**
*
* 1.2.1编写一个Point2D的用例,从命令行接受一个整数N。
* 在单位正方形内生成N个随机点,然后计算两点之间的最近距离
*
* 1.2.1 Write a Point2D client that takes an integer
* value N from the command line, generates N random
* points in the unit square, and computes the distance
* separating the closest pair of points.
*/
public class Point2D {
public double x;
public double y;
//初始化函数
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
//计算两点之间的距离
public double distTo(Point2D that){
double xDistance = this.x - that.x;
double yDistance = this.y - that.y;
return Math.sqrt(xDistance * xDistance + yDistance * yDistance);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int N = 10;
ArrayList<Point2D> points = new ArrayList<Point2D>();
//创建N个元素的数组
for (int i = 0; i < N; i++) {
Point2D point2d = new Point2D(Math.random(), Math.random());
points.add(point2d);
}
//计算第一个点到第二个点的距离
double resultDistince = points.get(0).distTo(points.get(1));
for (int i = 0; i < N; i++) {
Point2D point2d = points.get(i);
for (int j = i + 1; j < N; j++) {
//计算第i个点和第j个点之间的距离
double distince = point2d.distTo(points.get(j));
//如果距离比resultDistince小,就赋值给resultDistince
if (distince < resultDistince) {
resultDistince = distince;
}
}
}
System.out.println("resultDistince:" + resultDistince);
}
}