diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-10-31 19:56:31 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-10-31 19:56:31 -0400 |
| commit | aedd626997abe949126b92db7d4536933319c461 (patch) | |
| tree | eadeef543f7af02c7351afe96fe6423c465c7cd1 /geo.c | |
| parent | 111013b9ecb8b4208bfb1109e0f8ce8291fc90df (diff) | |
| download | balls-aedd626997abe949126b92db7d4536933319c461.zip | |
move noOverlapPositions to geo
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 |