-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint2d.java
More file actions
executable file
·89 lines (71 loc) · 2.01 KB
/
Copy pathPoint2d.java
File metadata and controls
executable file
·89 lines (71 loc) · 2.01 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
class Point2d {
/* The X and Y coordinates of the point--instance variables */
private double x;
private double y;
private boolean debug; // A trick to help with debugging
public Point2d (double px, double py) { // Constructor
x = px;
y = py;
debug = false; // turn off debugging
}
public Point2d () { // Default constructor
this (0.0, 0.0); // Invokes 2 parameter Point2D constructor
}
// Note that a this() invocation must be the BEGINNING of
// statement body of constructor
public Point2d (Point2d pt) { // Another consructor
x = pt.getX();
y = pt.getY();
// a better method would be to replace the above code with
// this (pt.getX(), pt.getY());
// especially since the above code does not initialize the
// variable debug. This way we are reusing code that is already
// working.
}
public void dprint (String s) {
// print the debugging string only if the "debug"
// data member is true
if (debug)
System.out.println("Debug: " + s);
}
public void setDebug (boolean b) {
debug = b;
}
public void setX(double px) {
dprint ("setX(): Changing value of X from " + x + " to " + px );
x = px;
}
public double getX() {
return x;
}
public void setY(double py) {
dprint ("setY(): Changing value of Y from " + y + " to " + py );
y = py;
}
public double getY() {
return y;
}
public void setXY(double px, double py) {
setX(px);
setY(py);
}
public double distanceFrom (Point2d pt) {
double dx = Math.abs(x - pt.getX());
double dy = Math.abs(y - pt.getY());
// check out the use of dprint()
dprint ("distanceFrom(): deltaX = " + dx);
dprint ("distanceFrom(): deltaY = " + dy);
return Math.sqrt((dx * dx) + (dy * dy));
}
public double distanceFromOrigin () {
return distanceFrom (new Point2d ( ));
}
public String toStringForXY() {
String str = "(" + x + ", " + y;
return str;
}
public String toString() {
String str = toStringForXY() + ")";
return str;
}
}