mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-10-02 17:38:12 +08:00
Add Normalize and Length nodes (#2837)
* Add vector length node * Add normalize node * Add comments * Make normalize node safer for the user
This commit is contained in:
@@ -612,6 +612,20 @@ fn dot_product(_: impl Ctx, vector_a: DVec2, vector_b: DVec2) -> f64 {
|
|||||||
vector_a.dot(vector_b)
|
vector_a.dot(vector_b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the length or magnitude of a vector.
|
||||||
|
#[node_macro::node(category("Math: Vector"))]
|
||||||
|
fn length(_: impl Ctx, vector: DVec2) -> f64 {
|
||||||
|
vector.length()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scales the input vector to unit length while preserving it's direction. This is equivalent to dividing the input vector by it's own magnitude.
|
||||||
|
///
|
||||||
|
/// Returns zero when the input vector is zero.
|
||||||
|
#[node_macro::node(category("Math: Vector"))]
|
||||||
|
fn normalize(_: impl Ctx, vector: DVec2) -> DVec2 {
|
||||||
|
vector.normalize_or_zero()
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -625,6 +639,12 @@ mod test {
|
|||||||
assert_eq!(dot_product((), vector_a, vector_b), 11.);
|
assert_eq!(dot_product((), vector_a, vector_b), 11.);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn length_function() {
|
||||||
|
let vector = DVec2::new(3., 4.);
|
||||||
|
assert_eq!(length((), vector), 5.);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_basic_expression() {
|
fn test_basic_expression() {
|
||||||
let result = math((), 0., "2 + 2".to_string(), 0.);
|
let result = math((), 0., "2 + 2".to_string(), 0.);
|
||||||
|
|||||||
Reference in New Issue
Block a user