1
1
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

49 lines
923 B

#include "vect.h"
double vect_dot(Vect V, Vect B) {
return V.x * B.x + V.y * B.y;
}
double vect_mag(Vect v) {
double mag = sqrt((v.x * v.x) + (v.y * v.y));
return mag;
}
Vect vect_scalar(Vect V, double s) {
Vect ret;
ret.x = V.x * s;
ret.y = V.y * s;
return ret;
}
Vect vect_add(Vect v1, Vect v2) {
Vect res;
res.x = v1.x + v2.x;
res.y = v1.y + v2.y;
return res;
}
// project one onto two
Vect project_vect(Vect one, Vect two) {
// $$ a \cdot \frac {|b|} {b} $$
Vect unittwo;
unittwo.x = two.x / vect_mag(two);
unittwo.y = two.y / vect_mag(two);
Vect proj;
proj.x = unittwo.x * one.x;
proj.y = unittwo.y * one.y;
return proj;
}
// project V onto P
double vect_scalar_projection(Vect V, Vect P) {
double angle = vect_dir(V) - vect_dir(P);
return cos(angle) * vect_mag(V);
}
double vect_dir(Vect V) {
return atan2(V.y, V.x);
}