Skip to content

Commit

Permalink
plot zoom out max (#4695)
Browse files Browse the repository at this point in the history
* Closes <emilk/egui#3462>


#3462 panics when when hit the bound like `f64::MAX` 

This PR "blocks" zoom if to big


Linked to
- comment in
emilk/egui#4637 (comment)
- #3462
  • Loading branch information
Its-Just-Nans authored Jun 26, 2024
1 parent b946474 commit 8a88a65
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions crates/egui_plot/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,37 @@ impl PlotBounds {
self.max[1] = self.max[1].max(y);
}

#[inline]
fn clamp_to_finite(&mut self) {
for d in 0..2 {
self.min[d] = self.min[d].clamp(f64::MIN, f64::MAX);
if self.min[d].is_nan() {
self.min[d] = 0.0;
}

self.max[d] = self.max[d].clamp(f64::MIN, f64::MAX);
if self.max[d].is_nan() {
self.max[d] = 0.0;
}
}
}

#[inline]
pub fn expand_x(&mut self, pad: f64) {
self.min[0] -= pad;
self.max[0] += pad;
if pad.is_finite() {
self.min[0] -= pad;
self.max[0] += pad;
self.clamp_to_finite();
}
}

#[inline]
pub fn expand_y(&mut self, pad: f64) {
self.min[1] -= pad;
self.max[1] += pad;
if pad.is_finite() {
self.min[1] -= pad;
self.max[1] += pad;
self.clamp_to_finite();
}
}

#[inline]
Expand Down Expand Up @@ -173,14 +194,20 @@ impl PlotBounds {

#[inline]
pub fn translate_x(&mut self, delta: f64) {
self.min[0] += delta;
self.max[0] += delta;
if delta.is_finite() {
self.min[0] += delta;
self.max[0] += delta;
self.clamp_to_finite();
}
}

#[inline]
pub fn translate_y(&mut self, delta: f64) {
self.min[1] += delta;
self.max[1] += delta;
if delta.is_finite() {
self.min[1] += delta;
self.max[1] += delta;
self.clamp_to_finite();
}
}

#[inline]
Expand Down Expand Up @@ -374,12 +401,12 @@ impl PlotTransform {
let x = remap(
pos.x as f64,
(self.frame.left() as f64)..=(self.frame.right() as f64),
self.bounds.min[0]..=self.bounds.max[0],
self.bounds.range_x(),
);
let y = remap(
pos.y as f64,
(self.frame.bottom() as f64)..=(self.frame.top() as f64), // negated y axis!
self.bounds.min[1]..=self.bounds.max[1],
self.bounds.range_y(),
);
PlotPoint::new(x, y)
}
Expand Down

0 comments on commit 8a88a65

Please sign in to comment.