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.
 
 
 
 
 

81 lines
1.6 KiB

#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;
}
double vect_arg(Vect v) {
return atan2(v.y, v.x);
}
Vect vect_modarg(double mod, double arg) {
Vect ret;
ret.x = mod * cos(arg);
ret.y = mod * sin(arg);
return ret;
}
Vect vect_rotate(Vect v, double radians) {
Vect res;
res.x = v.x * cos(radians) - v.y * sin(radians);
res.y = v.x * sin(radians) + v.y * cos(radians);
return res;
}
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 old_vect_scalar_projection(Vect V, Vect P) {
double angle = vect_dir(V) - vect_dir(P);
return cos(angle) * vect_mag(V);
}
double vect_scalar_projection(Vect v, Vect p) {
double num = vect_dot(v, p);
return num / vect_mag(p);
}
double vect_dir(Vect V) {
return atan2(V.y, V.x);
}
// distance between two points
double vect_distance(Vect A, Vect B) {
double y = (B.y - A.y);
double x = (B.x - A.x);
y = y * y;
x = x * x;
return sqrt(x + y);
}