diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-10-02 16:31:31 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-10-02 16:31:31 -0400 |
| commit | baef318193316fc365ba4b0a559c387d96b4305a (patch) | |
| tree | 771d8d32b5e3a85c418b0d23307bd3d14094c12c /vec.cpp | |
| parent | 26ce765feb702c6d40f89a8cf50274869cdc35b4 (diff) | |
| download | balls-baef318193316fc365ba4b0a559c387d96b4305a.zip | |
refactor
Diffstat (limited to 'vec.cpp')
| -rw-r--r-- | vec.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
@@ -0,0 +1,61 @@ +#include <math.h> + +#include "balls.h" + +Vector +addVec(Vector v1, Vector v2) { + v1.x += v2.x; + v1.y += v2.y; + return v1; +} + +Vector +subVec(Vector v1, Vector v2) { + v1.x -= v2.x; + v1.y -= v2.y; + return v1; +} + +Vector +vecMulS(Vector v, double s) { + v.x *= s; + v.y *= s; + return v; +} + +Vector +vecDivS(Vector v, double s) { + v.x /= s; + v.y /= s; + return v; +} + +double +vecDot(Vector v1, Vector v2) { + return v1.x*v2.x + v1.y*v2.y; +} + +Vector +unitNorm(Vector v) { + return vecDivS(v, vecLen(v)); +} + +double +vecLen(Vector v) { + return sqrt(v.x*v.x + v.y*v.y); +} + +Vector +Vec(double x, double y) { + Vector v = {x, y}; + return v; +} + +Vector +VecPt(Point p, Point q) { + double dx, dy; + + dx = q.x - p.x; + dy = q.y - p.y; + return Vec(dx, dy); +} |