pub trait Curve<T> {
// Required methods
fn domain(&self) -> Interval;
fn sample_unchecked(&self, t: f32) -> T;
// Provided methods
fn sample(&self, t: f32) -> Option<T> { ... }
fn sample_clamped(&self, t: f32) -> T { ... }
}
Expand description
A trait for a type that can represent values of type T
parametrized over a fixed interval.
Typical examples of this are actual geometric curves where T: VectorSpace
, but other kinds
of output data can be represented as well. See the module-level documentation for details.
Required Methods§
Sourcefn domain(&self) -> Interval
fn domain(&self) -> Interval
The interval over which this curve is parametrized.
This is the range of values of t
where we can sample the curve and receive valid output.
Sourcefn sample_unchecked(&self, t: f32) -> T
fn sample_unchecked(&self, t: f32) -> T
Sample a point on this curve at the parameter value t
, extracting the associated value.
This is the unchecked version of sampling, which should only be used if the sample time t
is already known to lie within the curve’s domain.
Values sampled from outside of a curve’s domain are generally considered invalid; data which is nonsensical or otherwise useless may be returned in such a circumstance, and extrapolation beyond a curve’s domain should not be relied upon.
Provided Methods§
Sourcefn sample(&self, t: f32) -> Option<T>
fn sample(&self, t: f32) -> Option<T>
Sample a point on this curve at the parameter value t
, returning None
if the point is
outside of the curve’s domain.
Examples found in repository?
129fn display_curves(
130 mut gizmos: Gizmos,
131 ease_functions: Query<(&EaseFunctionPlot, &Transform, &Children)>,
132 mut transforms: Query<&mut Transform, Without<EaseFunctionPlot>>,
133 mut ui_text: Single<&mut Text>,
134 time: Res<Time>,
135) {
136 let samples = 100;
137 let duration = 2.5;
138 let time_margin = 0.5;
139
140 let now = ((time.elapsed_secs() % (duration + time_margin * 2.0) - time_margin) / duration)
141 .clamp(0.0, 1.0);
142
143 ui_text.0 = format!("Progress: {:.2}", now);
144
145 for (EaseFunctionPlot(function, color), transform, children) in &ease_functions {
146 let center = transform.translation.xy();
147 let half_size = PLOT_SIZE / 2.0;
148
149 // Draw a box around the curve
150 gizmos.linestrip_2d(
151 [
152 center + half_size,
153 center + half_size * Vec2::new(-1., 1.),
154 center + half_size * Vec2::new(-1., -1.),
155 center + half_size * Vec2::new(1., -1.),
156 center + half_size,
157 ],
158 color.darker(0.4),
159 );
160
161 // Draw the curve
162 let f = EasingCurve::new(0.0, 1.0, *function);
163 let drawn_curve = f
164 .by_ref()
165 .graph()
166 .map(|(x, y)| center - half_size + Vec2::new(x, y) * PLOT_SIZE);
167 gizmos.curve_2d(
168 &drawn_curve,
169 drawn_curve.domain().spaced_points(samples).unwrap(),
170 *color,
171 );
172
173 // Show progress along the curve for the current time
174 let y = f.sample(now).unwrap() * PLOT_SIZE.y;
175 transforms.get_mut(children[0]).unwrap().translation.y = -half_size.y + y;
176 transforms.get_mut(children[1]).unwrap().translation =
177 -half_size.extend(0.0) + Vec3::new(now * PLOT_SIZE.x, y, 0.0);
178
179 // Show horizontal bar at y value
180 gizmos.linestrip_2d(
181 [
182 center - half_size + Vec2::Y * y,
183 center - half_size + Vec2::new(PLOT_SIZE.x, y),
184 ],
185 color.darker(0.2),
186 );
187 }
188}
Sourcefn sample_clamped(&self, t: f32) -> T
fn sample_clamped(&self, t: f32) -> T
Sample a point on this curve at the parameter value t
, clamping t
to lie inside the
domain of the curve.