Game.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <chrono>
  6. #include <fstream>
  7. #include <cmath>
  8. #include "Game.h"
  9. #include "utils/Random.h"
  10. constexpr float EPS = 0.00000001f;
  11. bool compare(float a, float b) {
  12. return std::abs(a - b) < EPS;
  13. }
  14. bool Point::operator<(const Point& other) const {
  15. if(x == other.x) {
  16. return y < other.y;
  17. }
  18. return x < other.x;
  19. }
  20. float Point::distance(const Point& other) const {
  21. return (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y);
  22. }
  23. Game::Game(Keys& keys, const char* file, bool steps) : keys(keys), keyNext(keys.add(GLFW_KEY_N)), active(0),
  24. state(SPLIT) {
  25. // try getting number
  26. int amount = atoi(file);
  27. if(amount > 0) {
  28. Random r;
  29. for(int i = 0; i < amount; i++) {
  30. data.push_back({r.nextFloat(), r.nextFloat()});
  31. }
  32. } else {
  33. std::ifstream in;
  34. in.open(file);
  35. if(!in.good()) {
  36. std::cout << "cannot read '" << file << "'\n";
  37. return;
  38. }
  39. int count;
  40. in >> count;
  41. while(in.get() != '\n');
  42. for(int i = 0; i < count; i++) {
  43. float x;
  44. float y;
  45. in >> x;
  46. in.get();
  47. in >> y;
  48. while(!in.eof() && in.get() != '\n');
  49. data.push_back({x, y});
  50. }
  51. }
  52. if(data.size() > 0) {
  53. minX = data[0].x;
  54. maxX = data[0].x;
  55. minY = data[0].y;
  56. maxY = data[0].y;
  57. for(const Point& p : data) {
  58. if(p.x < minX) {
  59. minX = p.x;
  60. }
  61. if(p.x > maxX) {
  62. maxX = p.x;
  63. }
  64. if(p.y < minY) {
  65. minY = p.y;
  66. }
  67. if(p.y > maxY) {
  68. maxY = p.y;
  69. }
  70. }
  71. }
  72. if(!steps) {
  73. skip();
  74. }
  75. }
  76. bool Game::merge() {
  77. auto start = groups[active].begin();
  78. auto end = groups[active].end();
  79. if(start == end) {
  80. if(groups[1 - active].begin() == groups[1 - active].end() ||
  81. groups[1 - active].begin() + 1 == groups[1 - active].end()) {
  82. return false;
  83. }
  84. active = 1 - active;
  85. } else if(start + 1 == end) {
  86. std::vector<Point> group = groups[active].front();
  87. groups[active].pop_front();
  88. groups[1 - active].push_back(group);
  89. return merge();
  90. }
  91. hullA = groups[active].front();
  92. groups[active].pop_front();
  93. hullB = groups[active].front();
  94. groups[active].pop_front();
  95. lowerIndexA = findMaxX(hullA);
  96. lowerIndexB = findMinX(hullB);
  97. upperIndexA = lowerIndexA;
  98. upperIndexB = lowerIndexB;
  99. return true;
  100. }
  101. void Game::finalizeMerge() {
  102. std::vector<Point> mergedHull;
  103. mergedHull.reserve(hullA.size() + hullB.size());
  104. while(lowerIndexA != upperIndexA) {
  105. mergedHull.push_back(hullA[lowerIndexA]);
  106. lowerIndexA = (lowerIndexA + 1) % hullA.size();
  107. }
  108. mergedHull.push_back(hullA[upperIndexA]);
  109. while(upperIndexB != lowerIndexB) {
  110. mergedHull.push_back(hullB[upperIndexB]);
  111. upperIndexB = (upperIndexB + 1) % hullB.size();
  112. }
  113. mergedHull.push_back(hullB[lowerIndexB]);
  114. groups[1 - active].push_back(mergedHull);
  115. }
  116. void Game::split() {
  117. std::sort(data.begin(), data.end());
  118. int index = 0;
  119. while(index + 2 < static_cast<int> (data.size())) {
  120. groups[active].push_back(std::vector<Point>());
  121. std::vector<Point>& v = groups[active].back();
  122. Point& a = data[index];
  123. Point& b = data[index + 1];
  124. Point& c = data[index + 2];
  125. float f = det(a, b, c);
  126. if(compare(f, 0.0f)) {
  127. v.push_back({std::min(a.x, std::min(b.x, c.x)), std::min(a.y, std::min(b.y, c.y))});
  128. v.push_back({std::max(a.x, std::max(b.x, c.x)), std::max(a.y, std::max(b.y, c.y))});
  129. } else if(f > 0.0f) {
  130. v.push_back(a);
  131. v.push_back(c);
  132. v.push_back(b);
  133. } else {
  134. v.push_back(a);
  135. v.push_back(b);
  136. v.push_back(c);
  137. }
  138. index += 3;
  139. }
  140. int diff = data.size() - index;
  141. if(diff > 0) {
  142. groups[active].push_back(std::vector<Point>());
  143. std::vector<Point>& v = groups[active].back();
  144. while(index < static_cast<int> (data.size())) {
  145. v.push_back(data[index++]);
  146. }
  147. }
  148. }
  149. bool Game::isLowerTangent(const std::vector<Point>& hull) const {
  150. for(const Point& p : hull) {
  151. float f = det(hullA[lowerIndexA], hullB[lowerIndexB], p);
  152. if(f < -EPS) {
  153. return false;
  154. }
  155. }
  156. return true;
  157. }
  158. bool Game::isUpperTangent(const std::vector<Point>& hull) const {
  159. for(const Point& p : hull) {
  160. float f = det(hullA[upperIndexA], hullB[upperIndexB], p);
  161. if(f > EPS) {
  162. return false;
  163. }
  164. }
  165. return true;
  166. }
  167. bool Game::findLowerTangent() {
  168. if(!isLowerTangent(hullA)) {
  169. lowerIndexA = (lowerIndexA + 1) % hullA.size();
  170. } else if(!isLowerTangent(hullB)) {
  171. lowerIndexB = (lowerIndexB == 0) ? hullB.size() - 1 : lowerIndexB - 1;
  172. }
  173. if(!isLowerTangent(hullA) || !isLowerTangent(hullB)) {
  174. return true;
  175. }
  176. for(int i = 0; i < static_cast<int> (hullA.size()); i++) {
  177. if(i != lowerIndexA && compare(det(hullA[lowerIndexA], hullB[lowerIndexB], hullA[i]), 0.0f)) {
  178. if(hullB[lowerIndexB].distance(hullA[i]) > hullB[lowerIndexB].distance(hullA[lowerIndexA])) {
  179. lowerIndexA = i;
  180. }
  181. }
  182. }
  183. for(int i = 0; i < static_cast<int> (hullB.size()); i++) {
  184. if(i != lowerIndexB && compare(det(hullA[lowerIndexA], hullB[lowerIndexB], hullB[i]), 0.0f)) {
  185. if(hullA[lowerIndexA].distance(hullB[i]) > hullA[lowerIndexA].distance(hullB[lowerIndexB])) {
  186. lowerIndexB = i;
  187. }
  188. }
  189. }
  190. return false;
  191. }
  192. bool Game::findUpperTangent() {
  193. if(!isUpperTangent(hullA)) {
  194. upperIndexA = (upperIndexA == 0) ? hullA.size() - 1 : upperIndexA - 1;
  195. } else if(!isUpperTangent(hullB)) {
  196. upperIndexB = (upperIndexB + 1) % hullB.size();
  197. }
  198. if(!isUpperTangent(hullA) || !isUpperTangent(hullB)) {
  199. return true;
  200. }
  201. for(int i = 0; i < static_cast<int> (hullA.size()); i++) {
  202. if(i != upperIndexA && compare(det(hullA[upperIndexA], hullB[upperIndexB], hullA[i]), 0.0f)) {
  203. if(hullB[upperIndexB].distance(hullA[i]) > hullB[upperIndexB].distance(hullA[upperIndexA])) {
  204. upperIndexA = i;
  205. }
  206. }
  207. }
  208. for(int i = 0; i < static_cast<int> (hullB.size()); i++) {
  209. if(i != upperIndexB && compare(det(hullA[upperIndexA], hullB[upperIndexB], hullB[i]), 0.0f)) {
  210. if(hullA[upperIndexA].distance(hullB[i]) > hullA[upperIndexA].distance(hullB[upperIndexB])) {
  211. upperIndexB = i;
  212. }
  213. }
  214. }
  215. return false;
  216. }
  217. int Game::findMaxX(const std::vector<Point>& hull) const {
  218. if(hull.size() == 0) {
  219. return -1;
  220. }
  221. int index = 0;
  222. for(int i = 1; i < static_cast<int> (hull.size()); i++) {
  223. if(hull[i].x > hull[index].x) {
  224. index = i;
  225. }
  226. }
  227. return index;
  228. }
  229. int Game::findMinX(const std::vector<Point>& hull) const {
  230. if(hull.size() == 0) {
  231. return -1;
  232. }
  233. int index = 0;
  234. for(int i = 1; i < static_cast<int> (hull.size()); i++) {
  235. if(hull[i].x < hull[index].x) {
  236. index = i;
  237. }
  238. }
  239. return index;
  240. }
  241. void Game::tick() {
  242. if(keys.getDownTime(keyNext) == 1) {
  243. next();
  244. }
  245. if(keys.getDownTime(keyNext) >= 60) {
  246. for(int i = 0; i < 100; i++) {
  247. next();
  248. }
  249. }
  250. }
  251. void Game::next() {
  252. switch(state) {
  253. case SPLIT:
  254. split();
  255. state = MERGE;
  256. break;
  257. case MERGE:
  258. if(merge()) {
  259. state = FIND_LOWER_TANGENT;
  260. } else {
  261. state = END;
  262. hullA.clear();
  263. hullB.clear();
  264. }
  265. break;
  266. case FIND_LOWER_TANGENT:
  267. if(!findLowerTangent()) {
  268. state = FIND_UPPER_TANGENT;
  269. }
  270. break;
  271. case FIND_UPPER_TANGENT:
  272. if(!findUpperTangent()) {
  273. state = FINALIZE_MERGE;
  274. }
  275. break;
  276. case FINALIZE_MERGE:
  277. finalizeMerge();
  278. state = MERGE;
  279. break;
  280. default:
  281. break;
  282. }
  283. }
  284. void Game::render(float lag, Renderer& renderer) const {
  285. (void) lag;
  286. renderer.setPointSize(7);
  287. for(const Point& p : data) {
  288. renderer.drawPoint(normalizeX(p.x), normalizeY(p.y), 0x707070);
  289. }
  290. for(int k = 0; k < 2; k++) {
  291. for(const std::vector<Point> group : groups[k]) {
  292. renderGroup(renderer, group, 0xFFFFFF);
  293. }
  294. }
  295. renderGroup(renderer, hullA, 0xFF00FF);
  296. renderGroup(renderer, hullB, 0xFFFF00);
  297. if(state == FIND_LOWER_TANGENT || state == FIND_UPPER_TANGENT || state == FINALIZE_MERGE) {
  298. renderer.drawLine(
  299. normalizeX(hullA[lowerIndexA].x), normalizeY(hullA[lowerIndexA].y),
  300. normalizeX(hullB[lowerIndexB].x), normalizeY(hullB[lowerIndexB].y),
  301. 0x00FFFF);
  302. renderer.drawLine(
  303. normalizeX(hullA[upperIndexA].x), normalizeY(hullA[upperIndexA].y),
  304. normalizeX(hullB[upperIndexB].x), normalizeY(hullB[upperIndexB].y),
  305. 0x00FFFF);
  306. }
  307. }
  308. bool Game::isRunning() const {
  309. return true;
  310. }
  311. float Game::det(const Point& a, const Point& b, const Point& c) const {
  312. return (a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y);
  313. }
  314. void Game::renderGroup(Renderer& renderer, const std::vector<Point>& group, uint color) const {
  315. static unsigned int colorMap[] = {
  316. 0xFF0000, 0x00FF00, 0x0000FF
  317. };
  318. for(int i = 0; i < static_cast<int> (group.size()); i++) {
  319. int next = (i + 1) % group.size();
  320. renderer.drawLine(
  321. normalizeX(group[i].x),
  322. normalizeY(group[i].y),
  323. normalizeX(group[next].x),
  324. normalizeY(group[next].y),
  325. color);
  326. }
  327. for(int i = 0; i < static_cast<int> (group.size()); i++) {
  328. renderer.drawPoint(normalizeX(group[i].x), normalizeY(group[i].y), colorMap[i % 3]);
  329. }
  330. }
  331. void Game::skip() {
  332. long time = std::chrono::high_resolution_clock::now().time_since_epoch().count();
  333. split();
  334. while(merge()) {
  335. while(findLowerTangent());
  336. while(findUpperTangent());
  337. finalizeMerge();
  338. }
  339. time = std::chrono::high_resolution_clock::now().time_since_epoch().count() - time;
  340. std::cout << (time / 1000000.0) << " ms\n";
  341. state = END;
  342. hullA.clear();
  343. hullB.clear();
  344. }
  345. float Game::normalizeX(float f) const {
  346. if(compare(maxX, minX)) {
  347. return 0.0f;
  348. }
  349. f = (f - minX) / (maxX - minX);
  350. return f * 1.90f - 0.95f;
  351. }
  352. float Game::normalizeY(float f) const {
  353. if(compare(maxY, minY)) {
  354. return 0.0f;
  355. }
  356. f = (f - minY) / (maxY - minY);
  357. return f * 1.90f - 0.95f;
  358. }