summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--balls.c31
-rw-r--r--balls.h1
-rw-r--r--geo.c28
3 files changed, 29 insertions, 31 deletions
diff --git a/balls.c b/balls.c
index c342b40..20d4ff2 100644
--- a/balls.c
+++ b/balls.c
@@ -59,7 +59,6 @@ void freeGL(void);
void initShaders(void);
char *readFile(const char *filename, size_t *size);
void compileShader(GLint shader);
-Vector *noOverlapPositions(int n);
void frameCount(void);
void drawString(const char *str);
@@ -219,7 +218,7 @@ setPositions(void) {
int err;
/* Generate initial ball positions. */
- hostPositions = noOverlapPositions(nBalls);
+ hostPositions = noOverlapPositions(nBalls, bounds, RMAX);
hostPositionBuf = flatten(hostPositions);
free(hostPositions);
@@ -590,34 +589,6 @@ compileShader(GLint shader) {
}
}
-/*
- * Generate n circle coordinates such that none overlap even if all circles
- * have the maximum radius.
- */
-Vector *
-noOverlapPositions(int n) {
- Vector *ps;
- Rectangle r;
- int i, j;
-
- if ((ps = malloc(n*sizeof(Vector))) == NULL)
- sysfatal("Failed to allocate position array.\n");
-
- r = insetRect(bounds, RMAX);
- for (i = 0; i < n; i++) {
- ps[i] = randPtInRect(r);
- for (j = 0; j < i; j++)
- if (isCollision(ps[j], RMAX, ps[i], RMAX))
- break;
- if (j < i) { /* Overlapping. */
- i--;
- continue;
- }
- }
-
- return ps;
-}
-
void
frameCount(void) {
static int fps = 0;
diff --git a/balls.h b/balls.h
index aaf90e2..ef2589b 100644
--- a/balls.h
+++ b/balls.h
@@ -25,6 +25,7 @@ void printPartition(Partition part);
int isCollision(Vector p1, float r1, Vector p2, float r2);
Rectangle insetRect(Rectangle r, float n);
+Vector *noOverlapPositions(int n, Rectangle bounds, float radius);
float randFloat(float lo, float hi);
Vector randPtInRect(Rectangle r);
diff --git a/geo.c b/geo.c
index 5bf7e39..0f70eaf 100644
--- a/geo.c
+++ b/geo.c
@@ -1,5 +1,6 @@
-#include <stddef.h>
+#include <stdlib.h>
+#include "sysfatal.h"
#include "balls.h"
int
@@ -22,3 +23,28 @@ insetRect(Rectangle r, float n) {
return r;
}
+
+/* Generate n circle coordinates within bounds such that no circles overlap. */
+Vector *
+noOverlapPositions(int n, Rectangle bounds, float radius) {
+ Vector *ps;
+ Rectangle r;
+ int i, j;
+
+ if ((ps = malloc(n*sizeof(Vector))) == NULL)
+ sysfatal("Failed to allocate position array.\n");
+
+ r = insetRect(bounds, radius);
+ for (i = 0; i < n; i++) {
+ ps[i] = randPtInRect(r);
+ for (j = 0; j < i; j++)
+ if (isCollision(ps[j], radius, ps[i], radius))
+ break;
+ if (j < i) { /* Overlapping. */
+ i--;
+ continue;
+ }
+ }
+
+ return ps;
+} \ No newline at end of file