From 20622555ee6eabb3d3c9f618d65b91736dd7097f Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Wed, 9 Oct 2024 17:00:53 -0400 Subject: refactor --- Makefile | 4 ++-- balls.cpp | 26 -------------------------- balls.h | 3 ++- point.cpp | 5 ----- rand.cpp | 31 +++++++++++++++++++++++++++++++ 5 files changed, 35 insertions(+), 34 deletions(-) create mode 100644 rand.cpp diff --git a/Makefile b/Makefile index 94a8553..1704814 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = g++ CFLAGS = -Wall -pedantic LDFLAGS = -ltbb -lglut -lGLU -lGL -balls: balls.o collision.o point.o vec.o partition.o +balls: balls.o collision.o point.o vec.o partition.o rand.o ${CC} -o $@ $^ ${LDFLAGS} @echo done @@ -10,4 +10,4 @@ balls: balls.o collision.o point.o vec.o partition.o ${CC} -c ${CFLAGS} $< clean: - rm -f ./*.o balls \ No newline at end of file + rm -f ./*.o balls diff --git a/balls.cpp b/balls.cpp index 2c8172f..b3f245d 100644 --- a/balls.cpp +++ b/balls.cpp @@ -35,7 +35,6 @@ vector noOverlapCircles(unsigned int n); double mass(double radius); double volume(double radius); void animate(int v); -Color randColor(void); const static Rectangle bounds = {{-1.5, -1.0}, {1.5, 1.0}}; @@ -217,28 +216,3 @@ 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; -} - -Color -randColor(void) { - Color color; - - color.r = randDouble(0, 1); - color.g = randDouble(0, 1); - color.b = randDouble(0, 1); - return color; -} diff --git a/balls.h b/balls.h index 27bffde..4d070c9 100644 --- a/balls.h +++ b/balls.h @@ -63,7 +63,6 @@ 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); @@ -80,3 +79,5 @@ void collideWall(Ball *b, Rectangle wall); void collideBall(Ball *b1, Ball *b2); double randDouble(double lo, double hi); +Color randColor(void); +Point randPtInRect(Rectangle r); diff --git a/point.cpp b/point.cpp index 099165d..7d73b31 100644 --- a/point.cpp +++ b/point.cpp @@ -56,8 +56,3 @@ insetRect(Rectangle r, double 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 new file mode 100644 index 0000000..7a327ee --- /dev/null +++ b/rand.cpp @@ -0,0 +1,31 @@ +#include "balls.h" + +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; +} + +Color +randColor(void) { + Color color; + + color.r = randDouble(0, 1); + color.g = randDouble(0, 1); + color.b = randDouble(0, 1); + return color; +} + +Point +randPtInRect(Rectangle r) { + return Pt(randDouble(r.min.x, r.max.x), randDouble(r.min.y, r.max.y)); +} -- cgit v1.2.3