-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoordinate.java
More file actions
133 lines (115 loc) · 3.25 KB
/
Copy pathCoordinate.java
File metadata and controls
133 lines (115 loc) · 3.25 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
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
128
129
130
131
132
133
package primitives;
public class Coordinate {
private double _coordinate;
private static final int ACCURCY = -20;
public static final Coordinate ZERO = new Coordinate(0);
// ***************** Constructors ********************** //
public Coordinate(double coord) {
_coordinate = (getExp(coord) < ACCURCY) ? 0.0 : coord;
}
public Coordinate(Coordinate other) {
_coordinate = other._coordinate;
}
// ***************** Getters/Setters ********************** //
public double coordinate() {
return _coordinate;
}
// ***************** Operations ******************** //
private static int getExp(double num) {
return (int) ((Double.doubleToRawLongBits(num) >> 52) & 0x7FFL) - 1023;
}
public static boolean isZero(double number) {
return getExp(number) < ACCURCY;
}
/**
* subtracts a number from the coordinate
*
* @param other
* - another number to subtract
* @return double the subtraction between the coordinate and other
*/
private double subtract(double other) {
double result = _coordinate - other;
int resultExp = getExp(result);
if (resultExp < ACCURCY)
return 0.0;
int thisExp = getExp(_coordinate);
if ((resultExp - thisExp) < ACCURCY)
return _coordinate;
return result;
}
/**
* adds a number to the coordinate
*
* @param other
* - another number to add
* @return double - the sum of coordinate and other
*/
private double add(double other) {
double result = _coordinate + other;
int resultExp = (int) ((Double.doubleToRawLongBits(result) >> 52) & 0x7FFL);
if (Math.abs(resultExp) < ACCURCY)
return 0.0;
int thisExp = (int) ((Double.doubleToRawLongBits(_coordinate) >> 52) & 0x7FFL);
if (Math.abs(thisExp - resultExp) < ACCURCY)
return _coordinate;
return result;
}
/**
* using subtract with double
*
* @param other
* - another coordinate to subtract
* @return coordinate - the subtraction between coordinate and other
*/
public Coordinate subtract(Coordinate other) {
return new Coordinate(subtract(other._coordinate));
}
/**
* using add with double
*
* @param other
* - another coordinate to subtract
* @return coordinate - the sum of coordinate and other
*/
public Coordinate add(Coordinate other) {
return new Coordinate(add(other._coordinate));
}
/**
* scale coordinate with another number
*
* @param scalar
* - number to scale
* @return coordinate scaled
*/
public Coordinate scale(double scalar) {
return new Coordinate(_coordinate * scalar);
}
/**
* using scale
*
* @param other
* another coordinate to multiply
* @return coordinate multiplied
*/
public Coordinate multi(Coordinate other) {
return scale(other._coordinate);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (obj instanceof Double)
return subtract((double) obj) == 0.0;
if (!(obj instanceof Coordinate))
return false;
Coordinate other = (Coordinate) obj;
return (subtract(other._coordinate) == 0.0);
}
@Override
public String toString() {
return "" + _coordinate;
}
}