From b45a78b60136c3887430d309d2f78ed972c1fbc8 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Wed, 2 Oct 2024 16:47:32 -0400 Subject: ball colors --- balls.cpp | 21 +++++++++++++++++---- balls.h | 5 +++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/balls.cpp b/balls.cpp index fa52f0e..37383ed 100644 --- a/balls.cpp +++ b/balls.cpp @@ -33,13 +33,14 @@ enum { void keyboard(unsigned char key, int x, int y); void display(void); void drawBackground(void); -void drawCircle(double radius, Point p); +void drawCircle(double radius, Point p, Color color); void reshape(int w, int h); vector makeBalls(unsigned int n); 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}}; @@ -77,7 +78,7 @@ display(void) { glClear(GL_COLOR_BUFFER_BIT); drawBackground(); for (Ball b : balls) - drawCircle(b.r, b.p); + drawCircle(b.r, b.p, b.color); glutSwapBuffers(); } @@ -94,11 +95,11 @@ drawBackground(void) { } void -drawCircle(double radius, Point p) { +drawCircle(double radius, Point p, Color color) { int i; double theta, x, y; - glColor3f(0.1, 0.6, 0.8); + glColor3f(color.r, color.g, color.b); glBegin(GL_TRIANGLE_FAN); glVertex2f(p.x, p.y); for (i = 0; i <= CIRCLE_SEGS; i++) { @@ -142,6 +143,8 @@ makeBalls(unsigned int n) { balls[i].r = randDouble(RMIN, RMAX); balls[i].m = mass(balls[i].r); + + balls[i].color = randColor(); } return balls; } @@ -210,3 +213,13 @@ randDouble(double lo, double hi) { 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 73ab14c..3d0202c 100644 --- a/balls.h +++ b/balls.h @@ -10,11 +10,16 @@ typedef struct { double x, y; } Vector; +typedef struct { + float r, g, b; +} Color; + typedef struct { Point p; /* position [m] */ Vector v; /* velocity [m/s] */ double r; /* radius [m] */ double m; /* mass [kg] */ + Color color; } Ball; Point addPt(Point p, Point q); -- cgit v1.2.3