diff options
Diffstat (limited to 'geo.c')
| -rw-r--r-- | geo.c | 28 |
1 files changed, 27 insertions, 1 deletions
@@ -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 |