From cee36c13f21eb2c802d14c817cf3c038b69f6eaf Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Tue, 3 Dec 2024 20:34:17 -0500 Subject: [PATCH] add vector_perp --- vectors.scad | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/vectors.scad b/vectors.scad index b2502c4..6bbe63f 100644 --- a/vectors.scad +++ b/vectors.scad @@ -343,6 +343,26 @@ function vector_bisect(v1,v2) = ) v3; +// Function: vector_perp() +// Synopsis: Returns component of a vector perpendicular to a second vector +// Topics: Vectors, Math +// Usage: +// perp = vector_perp(v,w); +// Description: +// Returns the component of vector w that is perpendicular to vector v. Vectors must have the same length. +// Arguments: +// v = reference vector +// w = vector whose perpendicular component is returned +// Example(2D): We extract the component of the red vector that is perpendicular to the yellow vector. That component appears in blue. +// v = [12,6]; +// w = [13,22]; +// stroke([[0,0],v],endcap2="arrow2"); +// stroke([[0,0],w],endcap2="arrow2",color="red"); +// stroke([[0,0],vector_perp(v,w)], endcap2="arrow2", color="blue"); +function vector_perp(v,w) = + assert(is_vector(v) && is_vector(w) && len(v)==len(w), "Invalid or mismatched inputs") + w - w*v*v/(v*v); + // Section: Vector Searching