Das Programmierspiel von der Gulasch-Programmier-Nacht 11

(Latest mirror + merged latest fork by qr4 on Lua 5.3)

Entropia info page https://entropia.de/GPN11:Programmierspiel (dead links)
Origin Gitlab
https://code.nerd2nerd.org/n2n/WeltraumProgrammierNacht

route.c 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "route.h"
  5. #include "globals.h"
  6. // Abstand zwischen zwei Punkten
  7. double dist(pixel_t* A, pixel_t* B) {
  8. return hypotf(B->x - A->x, B->y - A->y);
  9. }
  10. // Wo auf der Linie zwischen A und B sitzt der Fußpunkt des Lots von C auf AB?
  11. // Werte kleiner Null bedeuten vor A entlang AB
  12. // Werte zwischen 0 und 1 geben an nach welchem Bruchteil von AB der Fußpunkt kommt
  13. // Werte größer 1 bedeuten daß der Fußpunkt auf der Verlängerung von AB hinter B liegt
  14. // Wert den Code mit A=B aufruf ist doof und verdient daß alles platzt
  15. double dividing_ratio(pixel_t* A, pixel_t* B, pixel_t* C) {
  16. return ((C->x - A->x)*(B->x - A->x) + (C->y - A->y)*(B->y - A->y))/pow(dist(A, B),2);
  17. }
  18. // Was ist der Minimal-Abstand von C zum Linien AB?
  19. // Achtung dies liefert den vorzeichenbehafteten Abstand
  20. // Werte kleiner Null bedeuten das C "links" der Verbindungslinie liegt wenn man von A Richtung B schaut
  21. // Werte größer Null dementsprechend recht, exakt null auf der Verbindungslinie
  22. double dist_to_line(pixel_t* A, pixel_t* B, pixel_t* C) {
  23. //return ((A->y - B->y)*C->x + (A->x - B->x)*C->y + (A->x * B->y - A->y * B->x))/dist(A,B);
  24. //return ((B->y - A->y)*C->x + (A->x - B->x)* C->y - ((B->y - A->y)* A->x + (A->x - B->x)* A->y))/dist(A,B);
  25. return ((A->y - B->y)*C->x - (A->x - B->x)* C->y + ((B->y - A->y)* A->x + (A->x - B->x)* A->y))/dist(A,B);
  26. }
  27. // Was ist der Minimal-Abstand von C zum Liniensegment AB?
  28. // Die ist der Abstand zwischen C und dem Fußpunkt des Lots auf AB fall dieser zwischen A und B fällt
  29. // Ansonsten der Abstand zu A btw B
  30. double dist_to_seg(pixel_t* A, pixel_t* B, pixel_t* C) {
  31. double r = dividing_ratio(A, B, C);
  32. if(r <= 0) {
  33. return dist(A, C);
  34. } else if (r >= 1) {
  35. return dist(B, C);
  36. } else {
  37. return fabs(dist_to_line(A, B, C));
  38. }
  39. }
  40. waypoint_t* go_around(pixel_t* A, pixel_t* B, pixel_t* C) {
  41. double r = dividing_ratio(A, B, C);
  42. pixel_t X = { A->x + r*(B->x - A->x), A->y + r*(B->y - A->y) };
  43. double d = dist(&X, C);
  44. pixel_t W = {C->x + 15*sqrt(2)*(X.x - C->x) / d, C->y + 15*sqrt(2)*(X.y - C->y) / d};
  45. waypoint_t* wp = malloc(sizeof(waypoint_t));
  46. wp->next = NULL;
  47. wp->point.x = W.x;
  48. wp->point.y = W.y;
  49. printf("Suggest you go via (%f,%f) to avoid (%f,%f)\n", W.x, W.y, C->x, C->y);
  50. return wp;
  51. }
  52. waypoint_t* route(pixel_t* start, pixel_t* stop, pixel_t* points, int n_points) {
  53. printf("Running route\n");
  54. /*
  55. pixel_t A = {0, 0};
  56. pixel_t B = {1000, 0};
  57. pixel_t C1 = {500, 200};
  58. printf("A = (0,0), B=(1000,0), C1=(500,200)\n");
  59. printf("dist(A,B) = %f\n", dist(&A, &B));
  60. printf("r(AB, C1) = %f\n", dividing_ratio(&A, &B, &C1));
  61. printf("dist_to_seg(A, B, C1) = %f\n", dist_to_seg(&A, &B, &C1));
  62. pixel_t C2 = {-100, 200};
  63. printf("C2 = (-100,200)\n");
  64. printf("r(AB, C2) = %f\n", dividing_ratio(&A, &B, &C2));
  65. printf("dist_to_seg(A, B, C2) = %f\n", dist_to_seg(&A, &B, &C2));
  66. pixel_t C3 = {200, -400};
  67. printf("C3 = (200,-400)\n");
  68. printf("r(AB, C3) = %f\n", dividing_ratio(&A, &B, &C2));
  69. printf("dist_to_seg(A, B, C3) = %f\n", dist_to_seg(&A, &B, &C3));
  70. */
  71. int i;
  72. int i_min = -1;
  73. double r_min = 1;
  74. for(i = 0; i < n_points; i++) {
  75. double r = dividing_ratio(start, stop, &(points[i]));
  76. if (r > 0 && r < 1) {
  77. double d = dist_to_line(start, stop, &(points[i]));
  78. if (fabs(d) < 15) {
  79. //printf("Point #%d at (%f,%f) is an obstacle %f pixel away from the course\n", i, points[i].x, points[i].y, d);
  80. if(r < r_min) {
  81. i_min = i;
  82. r_min = r;
  83. }
  84. }
  85. }
  86. }
  87. if(i_min >= 0) {
  88. printf("Point #%d at (%f,%f) is the first obstacle %f pixel down the course\n", i_min, points[i_min].x, points[i_min].y, r_min * dist(start, stop));
  89. waypoint_t* wp = go_around(start, stop, &(points[i_min]));
  90. waypoint_t* part1 = route(start, &(wp->point), points, n_points);
  91. if(part1 == NULL) {
  92. part1 = wp;
  93. } else {
  94. waypoint_t* t = part1;
  95. while(t->next != NULL) {
  96. t = t->next;
  97. }
  98. t->next = wp;
  99. }
  100. waypoint_t* part2 = route(&(wp->point), stop, points, n_points);
  101. if(part2 != 0) {
  102. waypoint_t* t = part1;
  103. while(t->next != NULL) {
  104. t = t->next;
  105. }
  106. t->next = part2;
  107. }
  108. return part1;
  109. } else {
  110. return NULL;
  111. }
  112. }
  113. waypoint_t* plotCourse(pixel_t* start, pixel_t* stop, pixel_t* points, int n) {
  114. int n_points = n*n;
  115. waypoint_t* wp_start = malloc(sizeof(waypoint_t));
  116. waypoint_t* wp_stop = malloc(sizeof(waypoint_t));
  117. wp_start->point.x = start->x;
  118. wp_start->point.y = start->y;
  119. wp_stop->point.x = stop->x;
  120. wp_stop->point.y = stop->y;
  121. wp_start->next = route(start, stop, points, n_points);
  122. waypoint_t* t = wp_start;
  123. while (t->next != NULL) {
  124. t = t->next;
  125. }
  126. t->next = wp_stop;
  127. return wp_start;
  128. }
  129. void get_surrounding_points(pixel_t *surrounding_points, pixel_t *points, int n, int face_x, int face_y) {
  130. surrounding_points[0] = points[(face_y - 1) * n + (face_x - 1)];
  131. surrounding_points[1] = points[(face_y) * n + (face_x - 1)];
  132. surrounding_points[2] = points[(face_y) * n + (face_x)];
  133. surrounding_points[3] = points[(face_y - 1) * n + (face_x)];
  134. }
  135. int get_line_intersection(pixel_t *P0, pixel_t *P1, pixel_t *P2, pixel_t *P3, pixel_t *result)
  136. {
  137. float s1_x, s1_y, s2_x, s2_y;
  138. s1_x = P1->x - P0->x;
  139. s1_y = P1->y - P0->y;
  140. s2_x = P3->x - P2->x;
  141. s2_y = P3->y - P2->y;
  142. float s, t;
  143. s = (-s1_y * (P0->x - P2->x) + s1_x * (P0->y - P2->y)) / (-s2_x * s1_y + s1_x * s2_y);
  144. t = ( s2_x * (P0->y - P2->y) - s2_y * (P0->x - P2->x)) / (-s2_x * s1_y + s1_x * s2_y);
  145. if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
  146. {
  147. if (result) {
  148. result->x = P0->x + (t * s1_x);
  149. result->y = P0->y + (t * s1_y);
  150. }
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. void find_face(pixel_t *p, pixel_t *points, int n, int *x, int *y) {
  156. int face_x = p->x / (GLOBALS.WIDTH / (n + 1));
  157. int face_y = p->y / (GLOBALS.HEIGHT / (n + 1));
  158. int edge;
  159. int retry = 1;
  160. int count = 0;
  161. pixel_t surrounding[4];
  162. while (retry && count < 4) {
  163. retry = 0;
  164. get_surrounding_points(surrounding, points, n, face_x, face_y);
  165. for (edge = 0; edge < 4; edge++) {
  166. double r = dist_to_line(surrounding + edge, surrounding + (edge + 1) % 4, p);
  167. if (r > 0) {
  168. printf("edge: %d, r %f, x %d, y %d\n", edge, r, face_x, face_y);
  169. retry = 1;
  170. switch (edge) {
  171. case 0 :
  172. face_x--;
  173. break;
  174. case 1 :
  175. face_y++;
  176. break;
  177. case 2 :
  178. face_x++;
  179. break;
  180. case 3 :
  181. face_y--;
  182. break;
  183. }
  184. break;
  185. }
  186. }
  187. }
  188. *x = face_x;
  189. *y = face_y;
  190. }
  191. waypoint_t *smooth(waypoint_t *way, int res) {
  192. waypoint_t *end = way;
  193. waypoint_t *working_start;
  194. waypoint_t *working;
  195. pixel_t startp;
  196. pixel_t midp;
  197. pixel_t endp;
  198. pixel_t t1, t2;
  199. pixel_t v1, v2;
  200. pixel_t s;
  201. int i;
  202. working_start = malloc(sizeof(waypoint_t));
  203. *working_start = *way;
  204. working_start->next = NULL;
  205. working = working_start;
  206. if (end) {
  207. midp = end->point;
  208. end = end->next;
  209. } else return NULL;
  210. if (end) {
  211. endp = end->point;
  212. end = end->next;
  213. } else return NULL;
  214. while (end != NULL) {
  215. startp = midp;
  216. midp = endp;
  217. endp = end->point;
  218. t1.x = (startp.x + midp.x) / 2.;
  219. t1.y = (startp.y + midp.y) / 2.;
  220. t2 = midp;
  221. v1.x = (midp.x - startp.x) / 2. / res;
  222. v1.y = (midp.y - startp.y) / 2. / res;
  223. v2.x = (endp.x - midp.x) / 2. / res;
  224. v2.y = (endp.y - midp.y) / 2. / res;
  225. for (i = 0; i < res; i++) {
  226. s.x = t1.x + ((t2.x - t1.x) * i) / res;
  227. s.y = t1.y + ((t2.y - t1.y) * i) / res;
  228. t1.x += v1.x;
  229. t1.y += v1.y;
  230. t2.x += v2.x;
  231. t2.y += v2.y;
  232. working->next = malloc (sizeof(waypoint_t));
  233. working = working->next;
  234. working->point = s;
  235. }
  236. working->next = end;
  237. end = end->next;
  238. }
  239. return working_start;
  240. }
  241. waypoint_t* plotCourse_gridbased(pixel_t *start, pixel_t *stop, pixel_t *points, int n) {
  242. int face_x, face_y, face_s_x, face_s_y;
  243. int edge;
  244. int last_edge = -1;
  245. find_face(start, points, n, &face_x, &face_y);
  246. find_face(stop, points, n, &face_s_x, &face_s_y);
  247. pixel_t surrounding[4];
  248. waypoint_t *wp_start = malloc (sizeof(waypoint_t));
  249. wp_start->point = *start;
  250. wp_start->next = NULL;
  251. waypoint_t *working = wp_start;
  252. while (face_x != face_s_x || face_y != face_s_y) {
  253. pixel_t c;
  254. get_surrounding_points(surrounding, points, n, face_x, face_y);
  255. for (edge = 0; edge < 4; edge++) {
  256. if (edge == last_edge) continue;
  257. c = working->point;
  258. if (get_line_intersection(surrounding + edge, surrounding + (edge + 1)%4, &c, stop, NULL)) {
  259. c.x = (surrounding[edge].x + surrounding[(edge + 1) % 4].x) / 2;
  260. c.y = (surrounding[edge].y + surrounding[(edge + 1) % 4].y) / 2;
  261. break;
  262. }
  263. }
  264. working->next = malloc(sizeof(waypoint_t));
  265. working = working->next;
  266. working->point = c;
  267. working->next = NULL;
  268. switch (edge) {
  269. case 0 :
  270. face_x--;
  271. break;
  272. case 1 :
  273. face_y++;
  274. break;
  275. case 2 :
  276. face_x++;
  277. break;
  278. case 3 :
  279. face_y--;
  280. break;
  281. default:
  282. return wp_start;
  283. }
  284. last_edge = (edge + 2) % 4;
  285. }
  286. working->next = malloc(sizeof(waypoint_t));
  287. working = working->next;
  288. working->point = *stop;
  289. working->next = NULL;
  290. return wp_start;
  291. }