| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- public class Routeplanner
- {
- private int rotation;
- private int x;
- private int y;
- private int maxx;
- private int maxy;
- private int ex;
- private int ey;
- public String coordinaten;
- public Routeplanner(int maxx, int maxy)
- {
- this.maxx = maxx;
- this.maxy = maxy;
- rotation = 0;
- x = 0;
- y = 0;
- ex = 0;
- ey = 0;
- }
- public void turnleft()
- {
- rotation = rotation - 90;
- if (rotation < 0)
- {
- rotation = 270;
- }
- }
- public void turnright()
- {
- rotation = rotation + 90;
- if (rotation > 270)
- {
- rotation = 0;
- }
- }
- public void keren()
- {
- rotation = rotation + 180;
- if (rotation == 360)
- {
- rotation = 0;
- }
- if(rotation == 450)
- {
- rotation = 90;
- }
- }
- public void vooruit()
- {
- if (rotation == 0 && y < maxy)
- {
- y ++;
- }
- if (rotation == 90 && x < maxx)
- {
- x ++;
- }
- if (rotation == 180 && y > 0)
- {
- y --;
- }
- if (rotation == 270 && x > 0)
- {
- x --;
- }
- }
- public String geefCoordinaten()
- {
- coordinaten = "(" + x + "," + y + ")";
- return coordinaten;
- }
- public String gaNaarPunt(int eindx, int eindy)
- {
- if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
- {
- ex = eindx;
- ey = eindy;
- }
- while(ex != x)
- {
- if(x < ex)
- {
- if(rotation == 90)
- {
- vooruit();
- System.out.println("vooruit");
- }
- else
- {
- turnleft();
- System.out.println("turnleft");
- }
- }
- else
- {
- if(x > ex)
- {
- if(rotation == 270)
- {
- vooruit();
- System.out.println("vooruit");
- }
- else
- {
- turnleft();
- System.out.println("turnleft");
- }
- }
- }
- }
- while(ey != y)
- {
- if(y < ey)
- {
- if(rotation == 0)
- {
- vooruit();
- System.out.println("vooruit");
- }
- else
- {
- turnright();
- System.out.println("turnright");
- }
- }
- else
- {
- if(y > ey)
- {
- if(rotation == 180)
- {
- vooruit();
- System.out.println("vooruit");
- }
- else
- {
- turnright();
- System.out.println("turnright");
- }
- }
- }
- }
- return "bestemming bereikt " + geefCoordinaten();
- }
- public String[] berekenRoute(int eindx, int eindy, int beginx, int beginy, int rotatie)
- {
- String[] route = new String[40];
- int teller = 0;
- int links = 0;
- x = beginx;
- y = beginy;
- rotation = rotatie;
- if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
- {
- ex = eindx;
- ey = eindy;
- }else{
- return route;
- }
- if(beginx <= maxx && beginy <= maxy && beginx >= 0 && beginy >= 0)
- {
- x = beginx;
- y = beginy;
- }else{
- return route;
- }
- while(ex != x)
- {
- if(x < ex)
- {
- if(rotation == 90)
- {
- vooruit();
- route[teller] = "vooruit";
- teller ++;
- }
- if(rotation == 0)
- {
- turnright();
- vooruit();
- route[teller] = "rechtsaf";
- teller ++;
- }
- if(rotation == 270)
- {
- keren();
- vooruit();
- route[teller] = "keren";
- teller ++;
- }
- if(rotation == 180)
- {
- turnleft();
- vooruit();
- route[teller] = "linksaf";
- teller ++;
- }
- }
- else
- {
- if(x > ex)
- {
- if(rotation == 270)
- {
- vooruit();
- route[teller] = "vooruit";
- teller ++;
- }
- if(rotation == 0)
- {
- turnright();
- vooruit();
- route[teller] = "linksaf";
- teller ++;
- }
- if(rotation == 90)
- {
- keren();
- vooruit();
- route[teller] = "keren";
- teller ++;
- }
- if(rotation == 180)
- {
- turnleft();
- vooruit();
- route[teller] = "rechtsaf";
- teller ++;
- }
- }
- }
- }
- while(ey != y)
- {
- if(y < ey)
- {
- if(rotation == 0)
- {
- vooruit();
- route[teller] = "vooruit";
- teller ++;
- }
- if(rotation == 270)
- {
- turnright();
- vooruit();
- route[teller] = "rechtsaf";
- teller ++;
- }
- if(rotation == 180)
- {
- keren();
- vooruit();
- route[teller] = "keren";
- teller ++;
- }
- if(rotation == 90)
- {
- turnleft();
- vooruit();
- route[teller] = "linksaf";
- teller ++;
- }
- }
- else
- {
- if(y > ey)
- {
- if(rotation == 180)
- {
- vooruit();
- route[teller] = "vooruit";
- teller ++;
- }
- if(rotation == 90)
- {
- turnright();
- vooruit();
- route[teller] = "rechtsaf";
- teller ++;
- }
- if(rotation == 0)
- {
- keren();
- vooruit();
- route[teller] = "keren";
- teller ++;
- }
- if(rotation == 270)
- {
- turnleft();
- vooruit();
- route[teller] = "linksaf";
- teller ++;
- }
- }
- }
- }
- System.out.println("bestemming bereikt " + geefCoordinaten());
- return route;
- }
- }
|