-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPoint.java
More file actions
87 lines (76 loc) · 1.73 KB
/
Copy pathPoint.java
File metadata and controls
87 lines (76 loc) · 1.73 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
// CS108 HW1 -- provided simple immutable Point 2-d point class
// that encapsulates a double x/y pair.
// Could also use the java.awt.Point2D class, but its
// interface is more messy.
public class Point {
private double x;
private double y;
/**
* Constructs a new point.
*/
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Copy constructor -- copies the given point.
* @param other
*/
public Point(Point other) {
this.x = other.x;
this.y = other.y;
}
/**
* Gets the x value.
* @return x
*/
public double getX() {
return x;
}
/**
* Gets the y value.
* @return y
*/
public double getY() {
return y;
}
/**
* Returns a new point which is dx/dy shifted
* from this point.
* @param dx
* @param dy
* @return new point dx/dy shifted from this point
*/
public Point shiftedPoint(double dx, double dy) {
return new Point(x+dx, y+dy);
}
/**
* Returns the distance between this point an another.
* @param other
* @return distance to other point
*/
public double distance(Point other) {
double x2 = Math.abs(x - other.x);
double y2 = Math.abs(y - other.y);
return Math.sqrt(x2*x2 + y2*y2);
}
/**
* Returns a "x y" string representation of the point.
* @return string representation
*/
public String toString() {
return x + " " + y;
}
/**
* Compares two points. Note: uses == on x and y
* double values, which is a questionable practice.
* Consider using distance() for a more
* flexible way to compare two points.
*/
public boolean equals(Object object) {
if (! (object instanceof Point)) return false;
Point other = (Point)object;
// Note: here we == compare doubles, which is not a good practice
return (other.x==x && other.y==y);
}
}