forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
69 lines (60 loc) · 1.4 KB
/
Point.java
File metadata and controls
69 lines (60 loc) · 1.4 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
/**
* An immutable point in two-dimensional space.
*/
public class Point {
public static final double DELTA = 0.001;
private final double x;
private final double y;
/**
* Constructs a point at the given location.
*
* @param x the X coordinate
* @param y the Y coordinate
*/
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* @return the X coordinate
*/
public double getX() {
return x;
}
/**
* @return the Y coordinate
*/
public double getY() {
return y;
}
/**
* Computes the distance between two points.
*
* @param pt the other point
* @return Euclidean distance
*/
public double distance(Point pt) {
double dx = this.x - pt.x;
double dy = this.y - pt.y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
* Determines whether or not two points are equal.
*
* @param obj the other point
* @return true if this equals obj
*/
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point pt = (Point) obj;
return distance(pt) < DELTA;
}
return super.equals(obj);
}
/**
* @return string representation of the point
*/
public String toString() {
return String.format("(%.1f, %.1f)", this.x, this.y);
}
}