summaryrefslogtreecommitdiff
path: root/testsat.cpp
blob: 45f38919dbf8ccab6a8b5c708a14d6b7f4478559 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <SDL2/SDL.h>

#include <assert.h>
#include <stdlib.h>

#include "maths.cpp"

struct Shape {
	v2f* points;
	int count;

	Shape(std::initializer_list<v2f> in) {
		int i;
		points = (v2f*)malloc(in.size() * sizeof *points);
		count = in.size();
		for (i = 0; i < count; i++)
			points[i] = *(in.begin() + i);
	}

	Shape(const Shape& other) {
		int s = other.count * sizeof *points;
		count = other.count;
		points = (v2f*)malloc(s);
		memcpy(points, other.points, s);
	}

	~Shape() {
		free(points);
	}

	void render(SDL_Renderer* ren) {
		int i, c = count;
		for (i = 0; i < c; i++) {
			v2f a = points[i];
			v2f b = points[(i + 1) % c];
			SDL_RenderDrawLine(
				ren,
				(int)a.x,
				(int)a.y,
				(int)b.x,
				(int)b.y
			);
		}
	}

	std::pair<v2f*, int> normals() const {
		int c = (count >> 1) + 1, i;
		v2f* normals = (v2f*)malloc(c * sizeof *normals);
		for (i = 0; i < count; i++) {
			v2f a = points[i];
			v2f b = points[(i + 1) % count];
			v2f e = a - b;
			v2f n = v2f::normalised(v2f(e.y, -e.x));
			normals[i] = n;
		}
		return { normals, i };
	}
};

bool intersect(Shape& a, Shape& b) {
	bool r = true;
	int i;
	auto[fn, fnc] = a.normals();
	auto[sn, snc] = b.normals();
	auto project = [](const Shape& shape, v2f axis) {
		int i;
		float mini =  INFINITY;
		float maxi = -INFINITY;
		for (i = 0; i < shape.count; i++) {
			v2f p = shape.points[i];
			float d = v2f::dot(p, axis);
			if (d < mini) mini = d;
			if (d > maxi) maxi = d;
		}
		return std::pair<float, float>{ mini, maxi };
	};
	for (i = 0; i < fnc; i++) {
		v2f axis = fn[i];
		auto [fp1, fp2] = project(a, axis);
		auto [sp1, sp2] = project(b, axis);
		if (fp2 > sp1 && fp1 > sp2) {
			r = false;
			goto end;
		}
	}
	for (i = 0; i < snc; i++) {
		v2f axis = sn[i];
		auto [fp1, fp2] = project(a, axis);
		auto [sp1, sp2] = project(b, axis);
		if (fp2 > sp1 && fp1 > sp2) {
			r = false;
			goto end;
		}
	}
end:
	free(fn);
	free(sn);
	return r;
}

int main(int argc, const char** argv) {
	int r = 1;
	SDL_Window* window;
	SDL_Renderer* ren;
	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);
	SDL_CreateWindowAndRenderer(
		800,
		800,
		0,
		&window,
		&ren
	);
	Shape a({
		v2f(300.0f, 300.0f),
		v2f(400.0f, 400.0f),
		v2f(250.0f, 500.0f),
	});
	Shape tri({
		v2f(0.0f, 0.0f),
		v2f(50.0f, 200.0f),
		v2f(200.0f, 100.0f),
	});
	while (r) {
		int i;
		SDL_Event e;
		while (SDL_PollEvent(&e)) {
			switch (e.type) {
				case SDL_QUIT:
					r = 0;
					break;
			}
		}
		int mx, my;
		SDL_GetMouseState(&mx, &my);
		Shape b(tri);
		for (i = 0; i < b.count; i++) {
			v2f& p = b.points[i];
			p.x += mx;
			p.y += my;
		}
		SDL_SetRenderDrawColor(ren, 0x00, 0x00, 0x00, 0x00);
		SDL_RenderClear(ren);
		SDL_SetRenderDrawColor(ren, 0xff, 0x00, 0xff, 0xff);
		a.render(ren);
		if (intersect(a, b)) {
			SDL_SetRenderDrawColor(ren, 0xff, 0x00, 0x00, 0xff);
		}
		b.render(ren);
		SDL_RenderPresent(ren);
	}
	SDL_DestroyRenderer(ren);
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}