mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
New nodes: Sum, Average, Minimum, Maximum, Any, All (#4344)
Co-authored-by: Dennis Kobert <dennis@kobert.dev>
This commit is contained in:
committed by
Dennis Kobert
parent
04d6c0d5cf
commit
20eb5ccb8a
@@ -639,6 +639,45 @@ fn binary_gcd<T: num_traits::int::PrimInt + std::ops::ShrAssign<i32> + std::ops:
|
||||
a << shift
|
||||
}
|
||||
|
||||
/// Adds together all the numbers in the input list, producing their total.
|
||||
#[node_macro::node(category("Math: Numeric"))]
|
||||
pub fn sum(_: impl Ctx, values: IList<f64>) -> f64 {
|
||||
values.iter().sum()
|
||||
}
|
||||
|
||||
/// Averages all the numbers in the input list. An empty list gives 0.
|
||||
#[node_macro::node(category("Math: Numeric"))]
|
||||
fn average(_: impl Ctx, values: IList<f64>) -> f64 {
|
||||
let count = values.len();
|
||||
let average = if count == 0 { 0. } else { values.iter().sum::<f64>() / count as f64 };
|
||||
|
||||
average
|
||||
}
|
||||
|
||||
/// Gives the smallest number in the input list. An empty list gives 0.
|
||||
#[node_macro::node(category("Math: Numeric"))]
|
||||
fn minimum(_: impl Ctx, values: IList<f64>) -> f64 {
|
||||
values.iter().reduce(f64::min).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Gives the largest number in the input list. An empty list gives 0.
|
||||
#[node_macro::node(category("Math: Numeric"))]
|
||||
fn maximum(_: impl Ctx, values: IList<f64>) -> f64 {
|
||||
values.iter().reduce(f64::max).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Outputs true if at least one value in the input list is true. An empty list gives false.
|
||||
#[node_macro::node(category("Math: Logic"))]
|
||||
fn any(_: impl Ctx, values: IList<bool>) -> bool {
|
||||
values.iter().any(|value| value)
|
||||
}
|
||||
|
||||
/// Outputs true only if every value in the input list is true. An empty list gives true.
|
||||
#[node_macro::node(category("Math: Logic"))]
|
||||
fn all(_: impl Ctx, values: IList<bool>) -> bool {
|
||||
values.iter().all(|value| value)
|
||||
}
|
||||
|
||||
/// The less-than operation (`<`) compares two values and returns true if the first value is less than the second, or false if it is not.
|
||||
/// If enabled with *Or Equal*, the less-than-or-equal operation (`<=`) is used instead.
|
||||
#[node_macro::node(category("Math: Logic"))]
|
||||
|
||||
Reference in New Issue
Block a user