-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
74 lines (68 loc) · 1.56 KB
/
Point.java
File metadata and controls
74 lines (68 loc) · 1.56 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
import edu.rit.gpu.Struct;
import java.nio.ByteBuffer;
/**
* Class Point encapsulates a 3-D point.
*
* @author Zach Glassner
* @version 26-Oct-2016
*/
public class Point extends Struct {
public double x; // X coordinate
public double y; // Y coordinate
public double z; // Z coordinate
public Point() {}
/**
* Constructor for a point.
* @param x x coordinate value.
* @param y y coordinate value.
* @param z z coordinate value.
*/
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Returns the size in bytes of the C struct.
*/
public static long sizeof() {
return 24;
}
/**
* Write this Java object to the given byte buffer as a C struct.
* @param buf Buffer to write to.
*/
public void toStruct(ByteBuffer buf) {
buf.putDouble (this.x);
buf.putDouble (this.y);
buf.putDouble (this.z);
}
/**
* Read this Java object from the given byte buffer as a C struct.
* @param buf Buffer to read from to.
*/
public void fromStruct(ByteBuffer buf) {
this.x = buf.getDouble();
this.y = buf.getDouble();
this.z = buf.getDouble();
}
public String toString(){
return "("+this.x+", "+this.y+", "+this.z+")";
}
// city-block distance
public double distance(Point p) {
double dX = this.x - p.x;
if (dX < 0) {
dX *= -1;
}
double dY = this.y - p.y;
if (dY < 0) {
dY *= -1;
}
double dZ = this.z - p.z;
if (dZ < 0) {
dZ *= -1;
}
return dX + dY + dZ;
}
}