Routeplanner.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. package gui;
  2. public class Routeplanner
  3. {
  4. private int rotation;
  5. private int x;
  6. private int y;
  7. private int maxx;
  8. private int maxy;
  9. private int ex;
  10. private int ey;
  11. public String coordinaten;
  12. private int lengte;
  13. public Routeplanner(int maxx, int maxy)
  14. {
  15. this.maxx = maxx;
  16. this.maxy = maxy;
  17. rotation = 0;
  18. x = 0;
  19. y = 0;
  20. ex = 0;
  21. ey = 0;
  22. }
  23. public void turnleft()
  24. {
  25. rotation = rotation - 90;
  26. if (rotation < 0)
  27. {
  28. rotation = 270;
  29. }
  30. }
  31. public void turnright()
  32. {
  33. rotation = rotation + 90;
  34. if (rotation > 270)
  35. {
  36. rotation = 0;
  37. }
  38. }
  39. public void keren()
  40. {
  41. rotation = rotation + 180;
  42. if (rotation == 360)
  43. {
  44. rotation = 0;
  45. }
  46. if(rotation == 450)
  47. {
  48. rotation = 90;
  49. }
  50. }
  51. public void vooruit()
  52. {
  53. if (rotation == 0 && y < maxy)
  54. {
  55. y ++;
  56. }
  57. if (rotation == 90 && x < maxx)
  58. {
  59. x ++;
  60. }
  61. if (rotation == 180 && y > 0)
  62. {
  63. y --;
  64. }
  65. if (rotation == 270 && x > 0)
  66. {
  67. x --;
  68. }
  69. }
  70. public String geefCoordinaten()
  71. {
  72. coordinaten = "(" + x + "," + y + ")";
  73. return coordinaten;
  74. }
  75. public String[] berekenRoute(int eindx, int eindy, int beginx, int beginy, int rotatie)
  76. {
  77. String[] route = new String[40];
  78. int teller = 0;
  79. int links = 0;
  80. rotation = rotatie;
  81. lengte = 0;
  82. if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
  83. {
  84. ex = eindx;
  85. ey = eindy;
  86. }else{
  87. return route;
  88. }
  89. if(beginx <= maxx && beginy <= maxy && beginx >= 0 && beginy >= 0)
  90. {
  91. x = beginx;
  92. y = beginy;
  93. }else{
  94. return route;
  95. }
  96. while(ex != x)
  97. {
  98. if(x < ex)
  99. {
  100. if(rotation == 90)
  101. {
  102. vooruit();
  103. route[teller] = "vooruit";
  104. teller ++;
  105. }
  106. if(rotation == 0)
  107. {
  108. turnright();
  109. vooruit();
  110. route[teller] = "rechtsaf";
  111. teller ++;
  112. }
  113. if(rotation == 270)
  114. {
  115. keren();
  116. vooruit();
  117. route[teller] = "keren";
  118. teller ++;
  119. }
  120. if(rotation == 180)
  121. {
  122. turnleft();
  123. vooruit();
  124. route[teller] = "linksaf";
  125. teller ++;
  126. }
  127. }
  128. else
  129. {
  130. if(x > ex)
  131. {
  132. if(rotation == 270)
  133. {
  134. vooruit();
  135. route[teller] = "vooruit";
  136. teller ++;
  137. }
  138. if(rotation == 0)
  139. {
  140. turnleft();
  141. vooruit();
  142. route[teller] = "linksaf";
  143. teller ++;
  144. }
  145. if(rotation == 90)
  146. {
  147. keren();
  148. vooruit();
  149. route[teller] = "keren";
  150. teller ++;
  151. }
  152. if(rotation == 180)
  153. {
  154. turnright();
  155. vooruit();
  156. route[teller] = "rechtsaf";
  157. teller ++;
  158. }
  159. }
  160. }
  161. }
  162. while(ey != y)
  163. {
  164. if(y < ey)
  165. {
  166. if(rotation == 0)
  167. {
  168. vooruit();
  169. route[teller] = "vooruit";
  170. teller ++;
  171. }
  172. if(rotation == 270)
  173. {
  174. turnright();
  175. vooruit();
  176. route[teller] = "rechtsaf";
  177. teller ++;
  178. }
  179. if(rotation == 180)
  180. {
  181. keren();
  182. vooruit();
  183. route[teller] = "keren";
  184. teller ++;
  185. }
  186. if(rotation == 90)
  187. {
  188. turnleft();
  189. vooruit();
  190. route[teller] = "linksaf";
  191. teller ++;
  192. }
  193. }
  194. else
  195. {
  196. if(y > ey)
  197. {
  198. if(rotation == 180)
  199. {
  200. vooruit();
  201. route[teller] = "vooruit";
  202. teller ++;
  203. }
  204. if(rotation == 90)
  205. {
  206. turnright();
  207. vooruit();
  208. route[teller] = "rechtsaf";
  209. teller ++;
  210. }
  211. if(rotation == 0)
  212. {
  213. keren();
  214. vooruit();
  215. route[teller] = "keren";
  216. teller ++;
  217. }
  218. if(rotation == 270)
  219. {
  220. turnleft();
  221. vooruit();
  222. route[teller] = "linksaf";
  223. teller ++;
  224. }
  225. }
  226. }
  227. }
  228. lengte = teller;
  229. teller = 0;
  230. while(teller < lengte)
  231. {
  232. System.out.println(route[teller]);
  233. teller ++;
  234. }
  235. System.out.println("Planning is met succes gemaakt" + geefCoordinaten());
  236. return route;
  237. }
  238. public int geefLengte()
  239. {
  240. return lengte;
  241. }
  242. public String[] ontwijk(int xx, int xy, int x, int y, int rotation)
  243. {
  244. this.x = x;
  245. this.y = y;
  246. String route[] = new String[4];
  247. int teller = 0;
  248. boolean ontwijk = false;
  249. if ((ey > xy && x < maxx && x == ex) || (ex < xx && y < maxy) && ontwijk == false)
  250. {
  251. turnright();
  252. vooruit();
  253. route[teller] = "rechtsaf";
  254. teller ++;
  255. turnleft();
  256. vooruit();
  257. route[teller] = "linksaf";
  258. teller ++;
  259. vooruit();
  260. route[teller] = "vooruit";
  261. teller ++;
  262. turnleft();
  263. vooruit();
  264. route[teller] = "linksaf";
  265. teller ++;
  266. ontwijk = true;
  267. }
  268. if (((ex > xx && y < maxy) || (ey < xy && x < maxx)) && ontwijk == false)
  269. {
  270. turnleft();
  271. vooruit();
  272. route[teller] = "linksaf";
  273. teller ++;
  274. turnright();
  275. vooruit();
  276. route[teller] = "rechtsaf";
  277. teller ++;
  278. vooruit();
  279. route[teller] = "vooruit";
  280. teller ++;
  281. turnright();
  282. vooruit();
  283. route[teller] = "rechtsaf";
  284. teller ++;
  285. ontwijk = true;
  286. }
  287. if (ex == xx && ex == xy)
  288. {
  289. System.out.println("ERROR: obstakel op eindstation");
  290. }
  291. lengte = teller;
  292. teller = 0;
  293. while(teller < lengte)
  294. {
  295. System.out.println(route[teller]);
  296. teller ++;
  297. }
  298. return route;
  299. }
  300. public int getRotation() {
  301. return rotation;
  302. }
  303. public char[] ontwijkBoebot(int xx, int xy, int x, int y, int rotation)
  304. {
  305. this.x = x;
  306. this.y = y;
  307. char route[] = new char[4];
  308. int teller = 0;
  309. boolean ontwijk = false;
  310. if ((ey > xy && x < maxx && x == ex) || (ex < xx && y < maxy) && ontwijk == false)
  311. {
  312. turnright();
  313. vooruit();
  314. route[teller] = 'r';
  315. teller ++;
  316. turnleft();
  317. vooruit();
  318. route[teller] = 'l';
  319. teller ++;
  320. turnleft();
  321. vooruit();
  322. route[teller] = 'l';
  323. teller ++;
  324. ontwijk = true;
  325. }
  326. if (((ex > xx && y < maxy) || (ey < xy && x < maxx)) && ontwijk == false)
  327. {
  328. turnleft();
  329. vooruit();
  330. route[teller] = 'l';
  331. teller ++;
  332. turnright();
  333. vooruit();
  334. route[teller] = 'r';
  335. teller ++;
  336. turnright();
  337. vooruit();
  338. route[teller] = 'r';
  339. teller ++;
  340. ontwijk = true;
  341. }
  342. if (ex == xx && ex == xy)
  343. {
  344. System.out.println("ERROR: obstakel op eindstation");
  345. }
  346. teller = 0;
  347. return route;
  348. }
  349. public char[] berekenRouteBoebot(int eindx, int eindy, int beginx, int beginy, int rotatie)
  350. {
  351. char[] route = new char[40];
  352. int teller = 0;
  353. int links = 0;
  354. rotation = rotatie;
  355. lengte = 0;
  356. if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
  357. {
  358. ex = eindx;
  359. ey = eindy;
  360. }else{
  361. return route;
  362. }
  363. if(beginx <= maxx && beginy <= maxy && beginx >= 0 && beginy >= 0)
  364. {
  365. x = beginx;
  366. y = beginy;
  367. }else{
  368. return route;
  369. }
  370. while(ex != x)
  371. {
  372. if(x < ex)
  373. {
  374. if(rotation == 90)
  375. {
  376. vooruit();
  377. route[teller] = 'v';
  378. teller ++;
  379. }
  380. if(rotation == 0)
  381. {
  382. turnright();
  383. vooruit();
  384. route[teller] = 'r';
  385. teller ++;
  386. }
  387. if(rotation == 270)
  388. {
  389. keren();
  390. vooruit();
  391. route[teller] = 'k';
  392. teller ++;
  393. }
  394. if(rotation == 180)
  395. {
  396. turnleft();
  397. vooruit();
  398. route[teller] = 'l';
  399. teller ++;
  400. }
  401. }
  402. else
  403. {
  404. if(x > ex)
  405. {
  406. if(rotation == 270)
  407. {
  408. vooruit();
  409. route[teller] = 'v';
  410. teller ++;
  411. }
  412. if(rotation == 0)
  413. {
  414. turnleft();
  415. vooruit();
  416. route[teller] = 'l';
  417. teller ++;
  418. }
  419. if(rotation == 90)
  420. {
  421. keren();
  422. vooruit();
  423. route[teller] = 'k';
  424. teller ++;
  425. }
  426. if(rotation == 180)
  427. {
  428. turnright();
  429. vooruit();
  430. route[teller] = 'r';
  431. teller ++;
  432. }
  433. }
  434. }
  435. }
  436. while(ey != y)
  437. {
  438. if(y < ey)
  439. {
  440. if(rotation == 0)
  441. {
  442. vooruit();
  443. route[teller] = 'v';
  444. teller ++;
  445. }
  446. if(rotation == 270)
  447. {
  448. turnright();
  449. vooruit();
  450. route[teller] = 'r';
  451. teller ++;
  452. }
  453. if(rotation == 180)
  454. {
  455. keren();
  456. vooruit();
  457. route[teller] = 'k';
  458. teller ++;
  459. }
  460. if(rotation == 90)
  461. {
  462. turnleft();
  463. vooruit();
  464. route[teller] = 'l';
  465. teller ++;
  466. }
  467. }
  468. else
  469. {
  470. if(y > ey)
  471. {
  472. if(rotation == 180)
  473. {
  474. vooruit();
  475. route[teller] = 'v';
  476. teller ++;
  477. }
  478. if(rotation == 90)
  479. {
  480. turnright();
  481. vooruit();
  482. route[teller] = 'r';
  483. teller ++;
  484. }
  485. if(rotation == 0)
  486. {
  487. keren();
  488. vooruit();
  489. route[teller] = 'k';
  490. teller ++;
  491. }
  492. if(rotation == 270)
  493. {
  494. turnleft();
  495. vooruit();
  496. route[teller] = 'l';
  497. teller ++;
  498. }
  499. }
  500. }
  501. }
  502. lengte = teller;
  503. teller = 0;
  504. while(teller < lengte)
  505. {
  506. System.out.println(route[teller]);
  507. teller ++;
  508. }
  509. System.out.println("Planning is met succes gemaakt" + geefCoordinaten());
  510. return route;
  511. }
  512. }