Skip to content

Commit

Permalink
fix: add missing abs to calculation for relative latlng vector (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt authored Sep 14, 2021
1 parent 36a81b5 commit 585f7de
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
66 changes: 56 additions & 10 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,66 @@ test.each([
{ lng: -90, lat: -45 },
{ x: -10007559.105973555, y: -5615239.936637378 },
],
[
{ lng: 0, lat: -1 },
{ x: 0, y: -111200.74693490694 },
],
])("latLngToVector3 is correct", (latLng, projected) => {
const vector = latLngToVector3(latLng);
expect(vector.x).toBeCloseTo(projected.x);
expect(vector.y).toBeCloseTo(0);
expect(vector.z).toBeCloseTo(-projected.y);
});

test("latLngToVector3Relative is correct", () => {
const relative = latLngToVector3Relative(
{ lat: 0, lng: 0 },
{ lat: 1, lng: 1 }
);

expect(relative.x).toBeCloseTo(-111195.10117748393);
expect(relative.y).toBeCloseTo(0);
expect(relative.z).toBeCloseTo(-111200.74693490766);
});
test.each([
// 0 same
{
latLng: { lat: 0, lng: 0 },
reference: { lat: 0, lng: 0 },
relative: { x: 0, y: 0 },
},
// 1 northwest of reference
{
latLng: { lat: 0, lng: 0 },
reference: { lat: -1, lng: 1 },
relative: {
x: -111195.10117748393,
y: 111200.74693490694,
},
},
// 2 northeast of reference
{
latLng: { lat: 0, lng: 2 },
reference: { lat: -1, lng: 1 },
relative: {
x: 111195.10117748393,
y: 111200.74693490694,
},
},
// 3 southeast of reference
{
latLng: { lat: -2, lng: 2 },
reference: { lat: -1, lng: 1 },
relative: {
x: 111195.10117748393,
y: -111234.63180200469,
},
},
// 4 southwest of reference
{
latLng: { lat: -2, lng: 0 },
reference: { lat: -1, lng: 1 },
relative: {
x: -111195.10117748393,
y: -111234.63180200469,
},
},
])(
"latLngToVector3Relative is correct: %# %j",
({ latLng, reference, relative }) => {
const vector = latLngToVector3Relative(latLng, reference);
expect(vector.x).toBeCloseTo(relative.x);
expect(vector.y).toBeCloseTo(0);
expect(vector.z).toBeCloseTo(-relative.y);
}
);
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ export function latLngToVector3Relative(
const p = latLngToVector3(point);
const r = latLngToVector3(reference);

target.setX((r.x - p.x) * Math.sign(p.x - r.x));
target.setY((r.y - p.y) * Math.sign(p.y - r.y));
target.setZ((r.z - p.z) * Math.sign(p.z - r.z));
target.setX(Math.abs(r.x - p.x) * Math.sign(p.x - r.x));
target.setY(Math.abs(r.y - p.y) * Math.sign(p.y - r.y));
target.setZ(Math.abs(r.z - p.z) * Math.sign(p.z - r.z));

return target;
}

0 comments on commit 585f7de

Please sign in to comment.