Vector Vector_projection(Vector v, Vector w) { float length = Vector_squareLength(w); if(length > 0) { return Vector_multiply(w, Vector_dotProduct(v, w) / length); } else { return Vector_multiply(w, 0.0); } }
Vector Vector_normalize(Vector v) { Vector u; int i; float length = sqrt(Vector_dotProduct(v, v)); u.size = v.size; for(i=0; i < v.size; i++) { u.d[i] = v.d[i] / length; } return u; }
float Vector_squareLength(Vector v) { return Vector_dotProduct(v, v); }
float Vector_length(Vector v) { return sqrt(Vector_dotProduct(v, v)); }
Vector Vector_projectionPlane(Vector v, Vector n) { return Vector_sub(v, Vector_multiply(n, Vector_dotProduct(v, n))); }
#include <stdio.h> #include "Vector.h" int main() { Vector* a = newVector(3, (double []){1,2,3}); Vector* b = newVector(3, (double []){2,3,4}); Vector_show(a); Vector_multiply(a, 3); Vector_show(a); Vector_show(b); printf("%lf\n", Vector_dotProduct(a, b)); deleteVector(a); deleteVector(b); return 0; }