Skip to content

Commit b57059d

Browse files
committed
This commit is deliberately borked to show why clamping is impractical
1 parent 6d4859e commit b57059d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

geo/src/algorithm/rhumb/intermediate.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use super::RhumbCalculations;
88
pub trait RhumbIntermediate<T: CoordFloat> {
99
/// Returns a new Point along a [rhumb line] between two existing points.
1010
///
11+
/// Start and end points are clamped to the range [-90.0, +90.0].
12+
///
1113
/// # Examples
1214
///
1315
/// ```rust
@@ -140,4 +142,17 @@ mod test {
140142
let route = p1.rhumb_intermediate_fill(&p2, max_dist, include_ends);
141143
assert_eq!(route, vec![p1, i25, i50, i75, p2]);
142144
}
145+
146+
#[test]
147+
fn clamp_on_both_ends() {
148+
let p1 = Point::new(30.0, -100.0);
149+
let p2 = Point::new(40.0, 100.0);
150+
let max_dist = 12000000.0; // meters
151+
let include_ends = true;
152+
let i_start = Point::new(30.0, -90.0);
153+
let i_half = Point::new(35.0, 0.0); // Vertical symmetry across equator
154+
let i_end = Point::new(40.0, 90.0);
155+
let route = p1.rhumb_intermediate_fill(&p2, max_dist, include_ends);
156+
assert_eq!(route, vec![i_start, i_half, i_end]);
157+
}
143158
}

geo/src/algorithm/rhumb/mod.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,29 @@ struct RhumbCalculations<T: CoordFloat + FromPrimitive> {
3535

3636
impl<T: CoordFloat + FromPrimitive> RhumbCalculations<T> {
3737
fn new(from: &Point<T>, to: &Point<T>) -> Self {
38+
let ninety = T::from(90.0).unwrap();
3839
let pi = T::from(std::f64::consts::PI).unwrap();
3940
let two = T::one() + T::one();
4041
let four = two + two;
4142

42-
let phi1 = from.y().to_radians();
43-
let phi2 = to.y().to_radians();
43+
let phi1 = if from.y() < -ninety {
44+
-ninety
45+
} else if from.y() > ninety {
46+
ninety
47+
} else {
48+
from.y()
49+
};
50+
let phi2 = if to.y() < -ninety {
51+
-ninety
52+
} else if to.y() > ninety {
53+
ninety
54+
} else {
55+
to.y()
56+
};
57+
let from = Point::new(from.x(), phi1);
58+
let to = Point::new(to.x(), phi2);
59+
let phi1 = phi1.to_radians();
60+
let phi2 = phi2.to_radians();
4461
let mut delta_lambda = (to.x() - from.x()).to_radians();
4562
// if delta_lambda is over 180° take shorter rhumb line across the anti-meridian:
4663
if delta_lambda > pi {
@@ -54,8 +71,8 @@ impl<T: CoordFloat + FromPrimitive> RhumbCalculations<T> {
5471
let delta_phi = phi2 - phi1;
5572

5673
Self {
57-
from: *from,
58-
to: *to,
74+
from: from,
75+
to: to,
5976
phi1,
6077
delta_lambda,
6178
delta_phi,

0 commit comments

Comments
 (0)