summaryrefslogtreecommitdiffstats
path: root/geo.c
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-10-31 19:56:31 -0400
committerSam Anthony <sam@samanthony.xyz>2024-10-31 19:56:31 -0400
commitaedd626997abe949126b92db7d4536933319c461 (patch)
treeeadeef543f7af02c7351afe96fe6423c465c7cd1 /geo.c
parent111013b9ecb8b4208bfb1109e0f8ce8291fc90df (diff)
downloadballs-aedd626997abe949126b92db7d4536933319c461.zip
move noOverlapPositions to geo
Diffstat (limited to 'geo.c')
-rw-r--r--geo.c28
1 files changed, 27 insertions, 1 deletions
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