-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise08_07.java
More file actions
50 lines (39 loc) · 1.76 KB
/
Copy pathExercise08_07.java
File metadata and controls
50 lines (39 loc) · 1.76 KB
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
import javafx.geometry.Point3D;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author jorda
*/
public class Exercise08_07 {
public static void main(String[] args) {
double[][] points = {{-1, 0, 3}, {-1, -1, -1}, {4, 1, 1}, {2, 0.5, 9}, {3.5, 2, -1}, {3, 1.5, 3}, {-1.5, 4, 2}, {5.5, 4, -0.5}};
double shortestdistance = Double.MAX_VALUE;
int smallestdistanceIndex1 = -1;
int smallestdistanceIndex2 = -1;
for (int i = 0; i < points.length; i++) {
for (int j = 0; j < points.length; j++) {
if(i != j)
if(distance3D(points[i], points[j]) < shortestdistance){
shortestdistance = distance3D(points[i], points[j]);
smallestdistanceIndex1 = i;
smallestdistanceIndex2 = j;
}
}
}
System.out.println("The closest two points are ("
+ points[smallestdistanceIndex1][0] + ", " + points[smallestdistanceIndex1][1] + ", " + points[smallestdistanceIndex1][2] +
") and (" + points[smallestdistanceIndex2][0] + ", " + points[smallestdistanceIndex2][1] + ", " + points[smallestdistanceIndex2][2] + ")");
}
public static double distance3D (double[] pointA, double[] pointB){
return Math.sqrt((pointB[0] - pointA[0])*(pointB[0] - pointA[0])
+ (pointB[1] - pointA[1])*(pointB[1] - pointA[1])
+ (pointB[2] - pointA[2])*(pointB[2] - pointA[2]));
}
}
// 0 1 2
// pointA x y z
// pointB x y z