From 5b560599f951cec1d88f86824912bc5241fe6d08 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Mon, 28 Oct 2024 10:34:43 -0400 Subject: set initial positions --- geo.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 geo.c (limited to 'geo.c') diff --git a/geo.c b/geo.c new file mode 100644 index 0000000..60c0ae2 --- /dev/null +++ b/geo.c @@ -0,0 +1,47 @@ +#include +#include + +#include "balls.h" + +static float randFloat(float lo, float hi); + +int +isCollision(float2 p1, float r1, float2 p2, float r2) { + float2 dist; + float rhs; + + dist = p1 - p2; + rhs = r1 + r2; + return (dist[0]*dist[0] + dist[1]*dist[1]) <= rhs*rhs; +} + +Rectangle +insetRect(Rectangle r, float n) { + r.min += n; + r.max -= n; + return r; +} + +float2 +randPtInRect(Rectangle r) { + float2 pt = { + randFloat(r.min[0], r.max[0]), + randFloat(r.min[1], r.max[1]) + }; + return pt; +} + +static float +randFloat(float lo, float hi) { + float r, diff; + static int isInitialized = 0; + + if (!isInitialized) { /* First call. */ + srand(time(0)); + isInitialized = 1; + } + + r = (float) rand() / RAND_MAX; + diff = hi - lo; + return lo + r*diff; +} -- cgit v1.2.3