From baef318193316fc365ba4b0a559c387d96b4305a Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Wed, 2 Oct 2024 16:31:31 -0400 Subject: refactor --- Makefile | 2 +- balls.cpp | 18 ++++++++- balls.h | 7 +--- geometry.cpp | 123 ----------------------------------------------------------- point.cpp | 63 ++++++++++++++++++++++++++++++ rand.cpp | 15 -------- vec.cpp | 61 +++++++++++++++++++++++++++++ 7 files changed, 143 insertions(+), 146 deletions(-) delete mode 100644 geometry.cpp create mode 100644 point.cpp delete mode 100644 rand.cpp create mode 100644 vec.cpp diff --git a/Makefile b/Makefile index ed96b3c..aac8508 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = g++ CFLAGS = -Wall -pedantic LDFLAGS = -ltbb -lglut -lGLU -lGL -balls: balls.o collision.o geometry.o rand.o +balls: balls.o collision.o point.o vec.o ${CC} -o $@ $^ ${LDFLAGS} @echo done diff --git a/balls.cpp b/balls.cpp index a939053..fa52f0e 100644 --- a/balls.cpp +++ b/balls.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -131,7 +132,6 @@ makeBalls(unsigned int n) { vector ps = noOverlapCircles(n); unsigned int i; - srand(time(0)); for (i = 0; i < n; i++) { cout << "Creating ball " << i << "\n"; balls[i].p = ps[i]; @@ -153,7 +153,6 @@ noOverlapCircles(unsigned int n) { Rectangle r; unsigned int i, j; - srand(time(0)); r = insetRect(bounds, RMAX); for (i = 0; i < n; i++) { cout << "Create non-overlapping circle " << i << "\n"; @@ -196,3 +195,18 @@ animate(int v) { display(); glutTimerFunc(FRAME_TIME_MS, animate, 0); } + +double +randDouble(double lo, double hi) { + double r, diff; + static int isInitialized = 0; + + if (!isInitialized) { /* first call */ + srand(time(0)); + isInitialized = 1; + } + + r = (double) rand() / (double) RAND_MAX; + diff = hi - lo; + return lo + r*diff; +} diff --git a/balls.h b/balls.h index 1a0038b..2cabd6e 100644 --- a/balls.h +++ b/balls.h @@ -1,5 +1,3 @@ -#include - typedef struct { double x, y; } Point; @@ -26,6 +24,8 @@ Point ptSubVec(Point p, Vector v); Point ptMulS(Point p, double s); Point ptDivS(Point p, double s); Point Pt(double x, double y); +Rectangle insetRect(Rectangle r, double n); +Point randPtInRect(Rectangle r); Vector addVec(Vector v1, Vector v2); Vector subVec(Vector v1, Vector v2); @@ -37,9 +37,6 @@ double vecLen(Vector v); Vector Vec(double x, double y); Vector VecPt(Point p, Point q); -Rectangle insetRect(Rectangle r, double n); -Point randPtInRect(Rectangle r); - int isCollision(Point p1, double r1, Point p2, double r2); void collideWall(Ball *b, Rectangle wall); void collideBall(Ball *b1, Ball *b2); diff --git a/geometry.cpp b/geometry.cpp deleted file mode 100644 index 542c08d..0000000 --- a/geometry.cpp +++ /dev/null @@ -1,123 +0,0 @@ -#include - -#include "balls.h" - -Point -addPt(Point p, Point q) { - p.x += q.x; - p.y += q.y; - return p; -} - -Point -subPt(Point p, Point q) { - p.x -= q.x; - p.y -=q.y; - return p; -} - -Point -ptAddVec(Point p, Vector v) { - p.x += v.x; - p.y += v.y; - return p; -} - -Point -ptSubVec(Point p, Vector v) { - p.x -= v.x; - p.y -= v.y; - return p; -} - -Point -ptMulS(Point p, double s) { - p.x *= s; - p.y *= s; - return p; -} - -Point -ptDivS(Point p, double s) { - p.x /= s; - p.y /=s; - return p; -} - -Point -Pt(double x, double y) { - Point p = {x, y}; - return p; -} - -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); -} - -Rectangle -insetRect(Rectangle r, double n) { - r.min.x += n; - r.min.y += n; - r.max.x -= n; - r.max.y -= n; - return r; -} - -Point -randPtInRect(Rectangle r) { - return Pt(randDouble(r.min.x, r.max.x), randDouble(r.min.y, r.max.y)); -} diff --git a/point.cpp b/point.cpp new file mode 100644 index 0000000..099165d --- /dev/null +++ b/point.cpp @@ -0,0 +1,63 @@ +#include "balls.h" + +Point +addPt(Point p, Point q) { + p.x += q.x; + p.y += q.y; + return p; +} + +Point +subPt(Point p, Point q) { + p.x -= q.x; + p.y -=q.y; + return p; +} + +Point +ptAddVec(Point p, Vector v) { + p.x += v.x; + p.y += v.y; + return p; +} + +Point +ptSubVec(Point p, Vector v) { + p.x -= v.x; + p.y -= v.y; + return p; +} + +Point +ptMulS(Point p, double s) { + p.x *= s; + p.y *= s; + return p; +} + +Point +ptDivS(Point p, double s) { + p.x /= s; + p.y /=s; + return p; +} + +Point +Pt(double x, double y) { + Point p = {x, y}; + return p; +} + +Rectangle +insetRect(Rectangle r, double n) { + r.min.x += n; + r.min.y += n; + r.max.x -= n; + r.max.y -= n; + return r; +} + +Point +randPtInRect(Rectangle r) { + return Pt(randDouble(r.min.x, r.max.x), randDouble(r.min.y, r.max.y)); +} diff --git a/rand.cpp b/rand.cpp deleted file mode 100644 index 6ffe897..0000000 --- a/rand.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "balls.h" - -int -randInt(int lo, int hi) { - return (rand() % (hi-lo)) + lo; -} - -double -randDouble(double lo, double hi) { - double r, diff; - - r = (double) rand() / (double) RAND_MAX; - diff = hi - lo; - return lo + r*diff; -} diff --git a/vec.cpp b/vec.cpp new file mode 100644 index 0000000..a9b78e9 --- /dev/null +++ b/vec.cpp @@ -0,0 +1,61 @@ +#include + +#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); +} -- cgit v1.2.3