Skip to content

Commit c41d0f1

Browse files
2.1.1 Point Fractions via Integer arithmetic
1 parent b79ac43 commit c41d0f1

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/main/java/com/mapcode/Point.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ public double getLonDeg() {
100100
/**
101101
* Returns "fractions", which is a whole number of 1/MICROLON_MAX_PRECISION_FACTORth degrees versus the millionths of degrees
102102
*/
103-
public double LonFractions() {
103+
public int getLonFractions() {
104104
assert defined;
105105
return fraclon;
106106
}
107107
/**
108108
* Returns "fractions", which is a whole number of 1/MICROLAT_MAX_PRECISION_FACTORth degrees versus the millionths of degrees
109109
*/
110-
public double LatFractions() {
110+
public int getLatFractions() {
111111
assert defined;
112112
return fraclat;
113113
}
@@ -195,7 +195,8 @@ public static double metersToDegreesLonAtLat(final double eastMeters, final doub
195195
@Nonnull
196196
@Override
197197
public String toString() {
198-
return defined ? ("(" + (lat32/1000000.0)+"|"+(fraclat/810000.0) + ", " + (lon32/1000000.0)+"|"+(fraclon/3240000.0) + ')') : "undefined";
198+
return defined ? ("(" + (lat32/MICRODEG_TO_DEG_FACTOR) + "|" + (fraclat/FRACLAT_PRECISION_FACTOR) +
199+
", " + (lon32/MICRODEG_TO_DEG_FACTOR) + "|" + (fraclon/FRACLON_PRECISION_FACTOR) + ')') : "undefined";
199200
}
200201

201202
@SuppressWarnings("NonFinalFieldReferencedInHashCode")
@@ -216,31 +217,31 @@ public boolean equals(final Object obj) {
216217
final Point that = (Point) obj;
217218
return (this.lat32 == that.lat32) &&
218219
(this.lon32 == that.lon32) &&
219-
(Double.compare(this.fraclat, that.fraclat) == 0) &&
220-
(Double.compare(this.fraclon, that.fraclon) == 0) &&
220+
(this.fraclat == that.fraclat) &&
221+
(this.fraclon == that.fraclon) &&
221222
(this.defined == that.defined);
222223
}
223224

224225
/**
225226
* Private data.
226227
*/
227-
private int lat32; // whole nr of MICRODEG_TO_DEG_FACTOR
228-
private int lon32; // whole nr of MICRODEG_TO_DEG_FACTOR
229-
private double fraclat; // whole nr of MICROLAT_MAX_PRECISION_FACTOR, relative to lat32
230-
private double fraclon; // whole nr of MICROLON_MAX_PRECISION_FACTOR, relative to lon32
228+
private int lat32; // whole nr of MICRODEG_TO_DEG_FACTOR
229+
private int lon32; // whole nr of MICRODEG_TO_DEG_FACTOR
230+
private int fraclat; // whole nr of MICROLAT_MAX_PRECISION_FACTOR, relative to lat32
231+
private int fraclon; // whole nr of MICROLON_MAX_PRECISION_FACTOR, relative to lon32
231232

232233
public void setMaxLatToMicroDeg(final int maxMicroLat) {
233234
if (lat32 >= maxMicroLat) {
234235
lat32 = maxMicroLat-1;
235-
fraclat = FRACLAT_PRECISION_FACTOR - 1;
236+
fraclat = (int) FRACLAT_PRECISION_FACTOR - 1;
236237
}
237238
}
238239

239240
public void setMaxLonToMicroDeg(final int maxMicroLon) {
240241
int max = (maxMicroLon < 0 && lon32 > 0) ? maxMicroLon + 360000000 : maxMicroLon;
241242
if (lon32 >= max) {
242243
lon32 = max - 1;
243-
fraclon = FRACLON_PRECISION_FACTOR - 1;
244+
fraclon = (int) FRACLON_PRECISION_FACTOR - 1;
244245
}
245246
}
246247

@@ -274,22 +275,23 @@ private Point() {
274275

275276
private Point(final double latDeg, final double lonDeg) {
276277

278+
double frac;
277279
double lat = latDeg + 90;
278280
if (lat < 0) { lat = 0; } else if (lat > 180) { lat = 180; }
279281
// lat now [0..180]
280282
lat *= MICROLAT_MAX_PRECISION_FACTOR;
281-
fraclat = Math.floor(lat + 0.1);
282-
double f = fraclat / FRACLAT_PRECISION_FACTOR;
283-
lat32 = (int) f;
284-
fraclat -= ((double) lat32 * FRACLAT_PRECISION_FACTOR);
283+
frac = Math.floor(lat + 0.1);
284+
lat32 = (int) (frac / FRACLAT_PRECISION_FACTOR);
285+
frac -= ((double) lat32 * FRACLAT_PRECISION_FACTOR);
286+
fraclat = (int) frac;
285287
lat32 -= 90000000;
286288

287289
double lon = lonDeg - (360.0 * Math.floor(lonDeg / 360)); // lon now in [0..360>
288290
lon *= MICROLON_MAX_PRECISION_FACTOR;
289-
fraclon = Math.floor(lon + 0.1);
290-
f = fraclon / FRACLON_PRECISION_FACTOR;
291-
lon32 = (int)f;
292-
fraclon -= ((double) lon32 * FRACLON_PRECISION_FACTOR);
291+
frac = Math.floor(lon + 0.1);
292+
lon32 = (int) (frac / FRACLON_PRECISION_FACTOR);
293+
frac -= ((double) lon32 * FRACLON_PRECISION_FACTOR);
294+
fraclon = (int) frac;
293295
if (lon32 >= 180000000) { lon32 -= 360000000; }
294296

295297
defined = true;

0 commit comments

Comments
 (0)