From aedd626997abe949126b92db7d4536933319c461 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 31 Oct 2024 19:56:31 -0400 Subject: move noOverlapPositions to geo --- geo.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'geo.c') diff --git a/geo.c b/geo.c index 5bf7e39..0f70eaf 100644 --- a/geo.c +++ b/geo.c @@ -1,5 +1,6 @@ -#include +#include +#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 -- cgit v1.2.3