-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint2D.java
127 lines (107 loc) · 3.44 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
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
/** class Point2D - models a point in 2 dimensions
@author Dr. Summa
*/
public class Point2D {
private double[] coords;
/** default constructor - this constructor calls the other one
giving default x and y coordinates of 0.0 , 0.0
@custom.ensure this.getX() == 0.0 && this.getY() == 0.0
*/
public Point2D() {
this(0.0,0.0);
}
/** overloaded constructor taking two doubles as arguments for the
x and y coordinates
@param x a double containing the initial x coordinate
@param y a double containing the initial y coordinate
@custom.ensure this.getX() == x && this.getY() == y
*/
public Point2D(double x, double y) {
coords = new double[2];
coords[0] = x;
coords[1] = y;
}
// getter or query methods to get the coordinates
/**
@return returns the current x coordinate value
*/
public double getX() {
return coords[0];
}
/**
@return returns the current y coordinate value
*/
public double getY() {
return coords[1];
}
/**
@return returns an array of double of size 2 where the first element is the x coordinate and the second element is the y coordinate
*/
public double[] getCoords() {
return coords;
}
/**
@custom.ensure this.getX() == newX
*/
public void setX(double newX) {
coords[0] = newX;
}
/**
@custom.ensure this.getY() == newY
*/
public void setY(double newY) {
coords[1] = newY;
}
/**
@custom.require coords.length == 2
@custom.ensure this.getX() == coords[0] && this.getY() == coords[1]
*/
public void setCoords(double[] coords) {
this.coords = coords;
}
/**
@custom.ensure this.getX() == x && this.getY() == y
*/
public void moveTo(double x, double y) {
this.coords[0] = x;
this.coords[1] = y;
}
/**
@custom.ensure this.getX() == old.getX() + x &&
this.getY() == old.getY() + y
*/
public void moveBy(double dx, double dy) {
this.coords[0] += dx;
this.coords[1] += dy;
}
/** calculates this distance from the current point to another point,
accessed by a reference passed in to this method.
@param other a reference to another point
@return this distance between this point and the argument point passed in
*/
public double distance(Point2D other) {
double dX = this.getX() - other.getX();
double dY = this.getY() - other.getY();
double dist = Math.sqrt( dX * dX + dY * dY );
return dist;
}
/** calculates this distance from the current point to another point,
accessed by a reference passed in to this method.
@param o a reference to another point
@return a boolean - true if the argument's coordinates are within the tolerance value of the current point
*/
@Override
public boolean equals(Object o) {
if (o instanceof Point2D) {
Point2D other = (Point2D)o;
if ( Math.abs (this.getY() - other.getY()) < 0.01 &&
Math.abs (this.getX() - other.getX()) < 0.01 )
return true;
}
return false; // if the thing passed in is not a Point2D, how can they be equal?!?!?
}
@Override
public String toString() {
return "x: " + coords[0] + " y: " + coords[1];
}
} // end class