MCPcopy Create free account
hub / github.com/eliotbo/bevy_plot / set_bounds

Method set_bounds

src/plot/plot.rs:908–971  ·  view source on GitHub ↗

Override the default plot bounds: x axis goes from bounds.lo.x to bounds.up.x. Beware! The tick period is automatically adjusted. Changing the tick period before setting the bounds will not have the intended effect. The bounds must be set before the ticks. # Panics Panics if `lo.x >= up.x` or `lo.y >= up.y`.

(&mut self, lo: Vec2, up: Vec2)

Source from the content-addressed store, hash-verified

906 ///
907 /// Panics if `lo.x >= up.x` or `lo.y >= up.y`.
908 pub fn set_bounds(&mut self, lo: Vec2, up: Vec2) {
909 if lo.x >= up.x {
910 panic!("when using plot.set_bounds(), lo.x must be strictly less than up.x");
911 } else if lo.y >= up.y {
912 panic!("when using plot.set_bounds(), lo.y must be strictly less than up.y");
913 };
914
915 self.bounds = PlotCanvasBounds { lo, up };
916
917 let delta = up - lo;
918 let exact_tick = delta / 10.0;
919
920 // find order of magnitude of dx
921 let order_x = exact_tick.x.log10().floor();
922 let mag_x = 10_f32.powf(order_x);
923
924 let p1x = mag_x * 1.0;
925 let p2x = mag_x * 2.0;
926 let p5x = mag_x * 5.0;
927
928 let psx = [p1x, p2x, p5x];
929
930 let vx = vec![
931 (p1x - exact_tick.x).abs(),
932 (p2x - exact_tick.x).abs(),
933 (p5x - exact_tick.x).abs(),
934 ];
935
936 use std::cmp::Ordering;
937 let min_x_index = vx
938 .iter()
939 .enumerate()
940 .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(Ordering::Equal))
941 .map(|(index, _)| index);
942
943 let tick_x = psx[min_x_index.unwrap()];
944
945 let order_y = exact_tick.y.log10().floor();
946 let mag_y = 10_f32.powf(order_y);
947
948 let p1y = mag_y * 1.0;
949 let p2y = mag_y * 2.0;
950 let p5y = mag_y * 5.0;
951
952 let psy = [p1y, p2y, p5y];
953
954 let vy = vec![
955 (p1y - exact_tick.y).abs(),
956 (p2y - exact_tick.y).abs(),
957 (p5y - exact_tick.y).abs(),
958 ];
959
960 let min_y_index = vy
961 .iter()
962 .enumerate()
963 .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(Ordering::Equal))
964 .map(|(index, _)| index);
965

Callers 6

plotMethod · 0.80
plotmMethod · 0.80
setupFunction · 0.80
setupFunction · 0.80
setupFunction · 0.80
setupFunction · 0.80

Calls 1

compute_zerosMethod · 0.80

Tested by

no test coverage detected