Skip to content

Commit 1d12a48

Browse files
Fix issue with solution line drawing.
1 parent 32699a8 commit 1d12a48

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

console_backend/src/solution_tab.rs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ pub enum LatLonUnits {
2323
}
2424

2525
impl LatLonUnits {
26+
pub fn convert_lat(&self, lat: f64, sf: f64, offset: f64) -> f64 {
27+
match self {
28+
LatLonUnits::Degrees => {
29+
lat / sf + offset
30+
},
31+
LatLonUnits::Meters => {
32+
(lat - offset) * sf
33+
},
34+
}
35+
}
36+
37+
pub fn convert_lon(&self, lon: f64, sf: f64, offset: f64) -> f64 {
38+
match self {
39+
LatLonUnits::Degrees => {
40+
lon / sf + offset
41+
},
42+
LatLonUnits::Meters => {
43+
(lon - offset) * sf
44+
},
45+
}
46+
}
47+
2648
/// Retrieve the velocity unit as string slice.
2749
pub fn as_str(&self) -> &'static str {
2850
match self {
@@ -695,7 +717,7 @@ impl SolutionTab {
695717
if let Some(lats) = self.slns.get_mut(lat_str.as_str()) {
696718
let lats_counts = lats.iter().filter(|&x| !x.is_nan()).count();
697719
let lat_offset =
698-
lats.iter().fold(0.0, |acc, x| acc + x) / lats_counts as f64;
720+
lats.iter().filter(|&x| !x.is_nan()).fold(0.0, |acc, x| acc + x) / lats_counts as f64;
699721
let (lat_sf, lon_sf) = self.unit.get_sig_figs(lat_offset);
700722
(lat_offset, lat_sf, lon_sf)
701723
} else {
@@ -706,36 +728,37 @@ impl SolutionTab {
706728
let lon_str = format!("lon_{}", mode_string);
707729
let lon_offset = if let Some(lons) = self.slns.get_mut(lon_str.as_str()) {
708730
let lons_counts = lons.iter().filter(|&x| !x.is_nan()).count();
709-
lons.iter().fold(0.0, |acc, x| acc + x) / lons_counts as f64
731+
lons.iter().filter(|&x| !x.is_nan()).fold(0.0, |acc, x| acc + x) / lons_counts as f64
710732
} else {
711733
0.0
712734
};
713735

714736
(lat_offset, lat_sf, lon_offset, lon_sf)
715737
};
738+
let (lat_sf_temp, lat_offset_temp, lon_sf_temp, lon_offset_temp) = if matches!(&self.unit, &LatLonUnits::Degrees) {
739+
(old_lat_sf, old_lat_offset, old_lon_sf, old_lon_offset)
740+
} else {
741+
(lat_sf, lat_offset, lon_sf, lon_offset)
742+
};
743+
let (slns, unit) = (&mut self.slns, &self.unit);
716744
for mode in self.mode_strings.iter() {
717745
let lat_str = format!("lat_{}", mode);
718746
let lon_str = format!("lon_{}", mode);
719-
if matches!(&self.unit, &LatLonUnits::Degrees) {
720-
if let Some(lats) = self.slns.get_mut(lat_str.as_str()) {
747+
slns.get_mut(lat_str.as_str())
748+
.map(|lats| {
721749
lats.iter_mut()
722-
.for_each(|x| *x = *x / old_lat_sf + old_lat_offset);
723-
}
724-
if let Some(lons) = self.slns.get_mut(lon_str.as_str()) {
750+
.for_each(|x| *x = unit.convert_lat(*x, lat_sf_temp, lat_offset_temp));
751+
});
752+
slns.get_mut(lon_str.as_str())
753+
.map(|lons| {
725754
lons.iter_mut()
726-
.for_each(|x| *x = *x / old_lon_sf + old_lon_offset);
727-
}
728-
} else {
729-
if let Some(lats) = self.slns.get_mut(lat_str.as_str()) {
730-
lats.iter_mut()
731-
.for_each(|x| *x = (*x - lat_offset) * lat_sf);
732-
}
733-
if let Some(lons) = self.slns.get_mut(lon_str.as_str()) {
734-
lons.iter_mut()
735-
.for_each(|x| *x = (*x - lon_offset) * lon_sf);
736-
}
737-
}
755+
.for_each(|x| *x = unit.convert_lon(*x, lon_sf_temp, lon_offset_temp));
756+
});
738757
}
758+
self.sln_line.iter_mut().for_each(|(lon, lat)| {
759+
*lon = unit.convert_lon(*lon, lon_sf_temp, lon_offset_temp);
760+
*lat = unit.convert_lat(*lat, lat_sf_temp, lat_offset_temp);
761+
});
739762
self.lat_sf = lat_sf;
740763
self.lon_sf = lon_sf;
741764
self.lat_offset = lat_offset;

0 commit comments

Comments
 (0)