-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistanceBetweenPointAndSegmentIn3D.java
More file actions
130 lines (105 loc) · 3.13 KB
/
DistanceBetweenPointAndSegmentIn3D.java
File metadata and controls
130 lines (105 loc) · 3.13 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
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
import java.util.Scanner;
class Vector {
double length;
double x;
double y;
double z;
Vector (Point a, Point b) {
x = b.x - a.x;
y = b.y - a.y;
z = b.z - a.z;
length = DistanceBetweenPointAndSegmentIn3D.segmentLength(a, b);
}
}
class Point {
double x;
double y;
double z;
Point() {}
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
", z=" + z +
'}';
}
}
public class DistanceBetweenPointAndSegmentIn3D {
static double segmentLength(Point a, Point b) {
return Math.sqrt(
(b.x - a.x) * (b.x - a.x)
+ (b.y - a.y) * (b.y - a.y)
+ (b.z - a.z) * (b.z - a.z)
);
}
static boolean isOn(Point f, Point b, Point e) {
double BE = segmentLength(b, e);
double FE = segmentLength(f, e);
double BF = segmentLength(b, f);
return BF + FE == BE;
}
static double findCos(Vector a, Vector b) {
return (a.x * b.x + a.y * b.y + a.z * b.z) / (a.length + b.length);
}
static double dist(Point p, Point b, Point e) {
// point is on a segment
if (isOn(p, b, e)) {
return 0;
}
// begin == end
if (b.x == e.x && b.y == e.y && b.z == e.z) {
return segmentLength(p, b);
}
double cosPEB = findCos(
new Vector(e, p),
new Vector(e, b)
);
double cosPBE = findCos(
new Vector(b, p),
new Vector(b, e)
);
if (cosPBE == -1 || cosPEB == -1) {
return Math.min(
segmentLength(p, e),
segmentLength(p, b)
);
}
if (cosPBE <= 0) {
return segmentLength(p, b);
}
if (cosPEB <= 0) {
return segmentLength(p, e);
}
double ta = segmentLength(b, e);
double tb = segmentLength(b, p);
double tc = segmentLength(e, p);
double tp = (ta + tb + tc) / 2;
return 2 / ta * Math.sqrt(tp * (tp - ta) * (tp - tb) * (tp - tc));
}
public static void main(String[] args) {
Scanner inputStream = new Scanner(System.in);
Point point = new Point(
inputStream.nextDouble(),
inputStream.nextDouble(),
inputStream.nextDouble()
);
Point begin = new Point(
inputStream.nextDouble(),
inputStream.nextDouble(),
inputStream.nextDouble()
);
Point end = new Point(
inputStream.nextDouble(),
inputStream.nextDouble(),
inputStream.nextDouble()
);
double result = dist(point, begin, end);
System.out.print(result);
}
}