Routeplanner.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. public class Routeplanner
  2. {
  3. private int rotation;
  4. private int x;
  5. private int y;
  6. private int maxx;
  7. private int maxy;
  8. private int ex;
  9. private int ey;
  10. public String coordinaten;
  11. public Routeplanner(int maxx, int maxy)
  12. {
  13. this.maxx = maxx;
  14. this.maxy = maxy;
  15. rotation = 0;
  16. x = 0;
  17. y = 0;
  18. ex = 0;
  19. ey = 0;
  20. }
  21. public void turnleft()
  22. {
  23. rotation = rotation - 90;
  24. if (rotation < 0)
  25. {
  26. rotation = 270;
  27. }
  28. }
  29. public void turnright()
  30. {
  31. rotation = rotation + 90;
  32. if (rotation > 270)
  33. {
  34. rotation = 0;
  35. }
  36. }
  37. public void keren()
  38. {
  39. rotation = rotation + 180;
  40. if (rotation == 360)
  41. {
  42. rotation = 0;
  43. }
  44. if(rotation == 450)
  45. {
  46. rotation = 90;
  47. }
  48. }
  49. public void vooruit()
  50. {
  51. if (rotation == 0 && y < maxy)
  52. {
  53. y ++;
  54. }
  55. if (rotation == 90 && x < maxx)
  56. {
  57. x ++;
  58. }
  59. if (rotation == 180 && y > 0)
  60. {
  61. y --;
  62. }
  63. if (rotation == 270 && x > 0)
  64. {
  65. x --;
  66. }
  67. }
  68. public String geefCoordinaten()
  69. {
  70. coordinaten = "(" + x + "," + y + ")";
  71. return coordinaten;
  72. }
  73. public String gaNaarPunt(int eindx, int eindy)
  74. {
  75. if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
  76. {
  77. ex = eindx;
  78. ey = eindy;
  79. }
  80. while(ex != x)
  81. {
  82. if(x < ex)
  83. {
  84. if(rotation == 90)
  85. {
  86. vooruit();
  87. System.out.println("vooruit");
  88. }
  89. else
  90. {
  91. turnleft();
  92. System.out.println("turnleft");
  93. }
  94. }
  95. else
  96. {
  97. if(x > ex)
  98. {
  99. if(rotation == 270)
  100. {
  101. vooruit();
  102. System.out.println("vooruit");
  103. }
  104. else
  105. {
  106. turnleft();
  107. System.out.println("turnleft");
  108. }
  109. }
  110. }
  111. }
  112. while(ey != y)
  113. {
  114. if(y < ey)
  115. {
  116. if(rotation == 0)
  117. {
  118. vooruit();
  119. System.out.println("vooruit");
  120. }
  121. else
  122. {
  123. turnright();
  124. System.out.println("turnright");
  125. }
  126. }
  127. else
  128. {
  129. if(y > ey)
  130. {
  131. if(rotation == 180)
  132. {
  133. vooruit();
  134. System.out.println("vooruit");
  135. }
  136. else
  137. {
  138. turnright();
  139. System.out.println("turnright");
  140. }
  141. }
  142. }
  143. }
  144. return "bestemming bereikt " + geefCoordinaten();
  145. }
  146. public String[] berekenRoute(int eindx, int eindy, int beginx, int beginy, int rotatie)
  147. {
  148. String[] route = new String[40];
  149. int teller = 0;
  150. int links = 0;
  151. x = beginx;
  152. y = beginy;
  153. rotation = rotatie;
  154. if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
  155. {
  156. ex = eindx;
  157. ey = eindy;
  158. }else{
  159. return route;
  160. }
  161. if(beginx <= maxx && beginy <= maxy && beginx >= 0 && beginy >= 0)
  162. {
  163. x = beginx;
  164. y = beginy;
  165. }else{
  166. return route;
  167. }
  168. while(ex != x)
  169. {
  170. if(x < ex)
  171. {
  172. if(rotation == 90)
  173. {
  174. vooruit();
  175. route[teller] = "vooruit";
  176. teller ++;
  177. }
  178. if(rotation == 0)
  179. {
  180. turnright();
  181. vooruit();
  182. route[teller] = "rechtsaf";
  183. teller ++;
  184. }
  185. if(rotation == 270)
  186. {
  187. keren();
  188. vooruit();
  189. route[teller] = "keren";
  190. teller ++;
  191. }
  192. if(rotation == 180)
  193. {
  194. turnleft();
  195. vooruit();
  196. route[teller] = "linksaf";
  197. teller ++;
  198. }
  199. }
  200. else
  201. {
  202. if(x > ex)
  203. {
  204. if(rotation == 270)
  205. {
  206. vooruit();
  207. route[teller] = "vooruit";
  208. teller ++;
  209. }
  210. if(rotation == 0)
  211. {
  212. turnright();
  213. vooruit();
  214. route[teller] = "linksaf";
  215. teller ++;
  216. }
  217. if(rotation == 90)
  218. {
  219. keren();
  220. vooruit();
  221. route[teller] = "keren";
  222. teller ++;
  223. }
  224. if(rotation == 180)
  225. {
  226. turnleft();
  227. vooruit();
  228. route[teller] = "rechtsaf";
  229. teller ++;
  230. }
  231. }
  232. }
  233. }
  234. while(ey != y)
  235. {
  236. if(y < ey)
  237. {
  238. if(rotation == 0)
  239. {
  240. vooruit();
  241. route[teller] = "vooruit";
  242. teller ++;
  243. }
  244. if(rotation == 270)
  245. {
  246. turnright();
  247. vooruit();
  248. route[teller] = "rechtsaf";
  249. teller ++;
  250. }
  251. if(rotation == 180)
  252. {
  253. keren();
  254. vooruit();
  255. route[teller] = "keren";
  256. teller ++;
  257. }
  258. if(rotation == 90)
  259. {
  260. turnleft();
  261. vooruit();
  262. route[teller] = "linksaf";
  263. teller ++;
  264. }
  265. }
  266. else
  267. {
  268. if(y > ey)
  269. {
  270. if(rotation == 180)
  271. {
  272. vooruit();
  273. route[teller] = "vooruit";
  274. teller ++;
  275. }
  276. if(rotation == 90)
  277. {
  278. turnright();
  279. vooruit();
  280. route[teller] = "rechtsaf";
  281. teller ++;
  282. }
  283. if(rotation == 0)
  284. {
  285. keren();
  286. vooruit();
  287. route[teller] = "keren";
  288. teller ++;
  289. }
  290. if(rotation == 270)
  291. {
  292. turnleft();
  293. vooruit();
  294. route[teller] = "linksaf";
  295. teller ++;
  296. }
  297. }
  298. }
  299. }
  300. System.out.println("bestemming bereikt " + geefCoordinaten());
  301. return route;
  302. }
  303. }