summaryrefslogtreecommitdiffstats
path: root/balls.h
blob: 4d070c9bbbe9f940082bae2881cb7927ce146a97 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <GL/glut.h>
#include <oneapi/tbb.h>

using namespace std;
using namespace oneapi::tbb;

typedef struct {
	double x, y;
} Point;

typedef struct {
	Point min, max;
} Rectangle;

typedef struct {
	double x, y;
} Vector;

typedef struct {
	float r, g, b;
} Color;

typedef struct {
	Point p; /* position [m] */
	Vector v; /* velocity [m/s] */
	double r; /* radius [m] */
	double m; /* mass [kg] */
	Color color;
} Ball;

class Collision {
public:
	Ball *b1, *b2;

	Collision(Ball *_b1, Ball *_b2) {
		b1 = _b1;
		b2 = _b2;
	}

	friend bool operator<(const Collision& a, const Collision& b) {
		return a.b1 < b.b1 || a.b2 < b.b2;
	}

	friend bool operator==(const Collision& a, const Collision& b) {
		return a.b1 == b.b1 && a.b2 == b.b2;
	}

	friend ostream& operator<<(ostream& os, Collision const & c) {
		return os << "(" << c.b1 << ", " << c.b2 << ")";
	}
};

vector<vector<Collision>> partitionCollisions(vector<Ball *> balls);

Point addPt(Point p, Point q);
Point subPt(Point p, Point q);
Point ptAddVec(Point p, Vector v);
Point ptSubVec(Point p, Vector v);
Point ptMulS(Point p, double s);
Point ptDivS(Point p, double s);
Point Pt(double x, double y);
Rectangle insetRect(Rectangle r, double n);

Vector addVec(Vector v1, Vector v2);
Vector subVec(Vector v1, Vector v2);
Vector vecMulS(Vector v, double s);
Vector vecDivS(Vector v, double s);
double vecDot(Vector v1, Vector v2);
Vector unitNorm(Vector v);
double vecLen(Vector v);
Vector Vec(double x, double y);
Vector VecPt(Point p, Point q);

int isCollision(Point p1, double r1, Point p2, double r2);
void collideWall(Ball *b, Rectangle wall);
void collideBall(Ball *b1, Ball *b2);

double randDouble(double lo, double hi);
Color randColor(void);
Point randPtInRect(Rectangle r);