summaryrefslogtreecommitdiffstats
path: root/geometry.cpp
blob: 542c08dce9d5336c1cc366cbe2ba8e55739f654d (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <math.h>

#include "balls.h"

Point
addPt(Point p, Point q) {
	p.x += q.x;
	p.y += q.y;
	return p;
}

Point
subPt(Point p, Point q) {
	p.x -= q.x;
	p.y -=q.y;
	return p;
}

Point
ptAddVec(Point p, Vector v) {
	p.x += v.x;
	p.y += v.y;
	return p;
}

Point
ptSubVec(Point p, Vector v) {
	p.x -= v.x;
	p.y -= v.y;
	return p;
}

Point
ptMulS(Point p, double s) {
	p.x *= s;
	p.y *= s;
	return p;
}

Point
ptDivS(Point p, double s) {
	p.x /= s;
	p.y /=s;
	return p;
}

Point
Pt(double x, double y) {
	Point p = {x, y};
	return p;
}

Vector
addVec(Vector v1, Vector v2) {
	v1.x += v2.x;
	v1.y += v2.y;
	return v1;
}

Vector
subVec(Vector v1, Vector v2) {
	v1.x -= v2.x;
	v1.y -= v2.y;
	return v1;
}

Vector
vecMulS(Vector v, double s) {
	v.x *= s;
	v.y *= s;
	return v;
}

Vector
vecDivS(Vector v, double s) {
	v.x /= s;
	v.y /= s;
	return v;
}

double
vecDot(Vector v1, Vector v2) {
	return v1.x*v2.x + v1.y*v2.y;
}

Vector
unitNorm(Vector v) {
	return vecDivS(v, vecLen(v));
}

double
vecLen(Vector v) {
	return sqrt(v.x*v.x + v.y*v.y);
}

Vector
Vec(double x, double y) {
	Vector v = {x, y};
	return v;
}

Vector
VecPt(Point p, Point q) {
	double dx, dy;

	dx = q.x - p.x;
	dy = q.y - p.y;
	return Vec(dx, dy);
}

Rectangle
insetRect(Rectangle r, double n) {
	r.min.x += n;
	r.min.y += n;
	r.max.x -= n;
	r.max.y -= n;
	return r;
}

Point
randPtInRect(Rectangle r) {
	return Pt(randDouble(r.min.x, r.max.x), randDouble(r.min.y, r.max.y));
}