summaryrefslogtreecommitdiffstats
path: root/geo.c
blob: 60c0ae263eb354c56a1124a3fc216d3b30a531e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdlib.h>
#include <time.h>

#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;
}