Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Position {
- final int startDAY = 28800; //Sekunden seit Mitternacht um 8:00(Dienstbeginn)
- private int sec; //Sekunden seit Mitternacht eines Eintrages
- private double longitude; //geographische Länge (Dezimalgrad 15.0-17.0)
- private double latitude; //geographische Breite (Dezimalgrad 47.0-49.0)
- public Position(double longitude, double latitude){
- this.sec = startDAY;
- this.longitude = longitude;
- this.latitude = latitude;
- }
- private static double min(double a, double b){
- if(a <= b){
- return a;
- }else{
- return b;
- }
- }
- private static double max(double a, double b){
- if(a >= b){
- return a;
- }else{
- return b;
- }
- }
- private static double pow(double val){
- return val*val;
- }
- // Returns true if (and only if) longitude and latitude of this position (the current object) differ from
- // longitude and latitude of x by at most eps.
- public boolean isClose(Position x, double eps) {
- //eps^2 = a^2(lon) + b^2(lat)
- double aa = pow( max(x.longitude, this.longitude) - min(x.longitude, this.longitude));
- double bb = pow( max(x.latitude, this.latitude) - min(x.latitude, this.latitude));
- System.out.println("dist: " + Math.sqrt(aa+bb) + ", eps: " + eps + ", maxLon: " + max(x.longitude, this.longitude) + ", maxLat: " + max(x.latitude, this.latitude));
- return ( pow(eps) >= aa + bb );
- }
- // Returns true if (and only if) longitude and latitude of this position (the current object) differ from
- // longitude and latitude of any element in xs by at most eps.
- public boolean isCloseToPath(Position[] xs, double eps) {
- for(Position pos : xs){
- if(!this.isClose(pos, eps)){
- return false;
- }
- }
- return true;
- }
- // Parameter xs contains the positions of a tour. A tour is regarded as being stopped at a position in the array
- // if this position is close (difference at most eps) to each of the following 4 positions in the array.
- // The method returns the time (in seconds since midnight) of the first stop (lowest index in the array),
- // or -1 if there is no stop.
- public static int isStopped(Position[] xs, double eps) {
- for(int i=1; i<xs.length; i++){
- int count = 1;
- if( xs[i-1].isClose(xs[i], eps) && ((i-1)+4) < xs.length ){
- for(int x=1; x<=4; x++){
- if(xs[i-1].isClose(xs[i+x], eps)){
- count++;
- }
- if(count == 4){
- return xs[i-1].sec;
- }
- }
- }
- }
- return -1;
- }
- // Returns true if (and only if) the time of each position in xs is higher than that of each other position at a
- // lower index.
- public static boolean chronologic(Position[] xs) {
- for(int i=1; i<xs.length; i++){
- if(xs[i-1].sec > xs[i].sec){
- return false;
- }
- }
- return true;
- }
- // Just for testing ...
- public static void main(String[] args) {
- //**isClose**
- System.out.println("**isClose**");
- Position one = new Position(4,5);
- Position two_1 = new Position(2,3);
- Position two_2 = new Position(2,7);
- Position two_3 = new Position(6,3);
- Position two_4 = new Position(6,7);
- //square(8) = 2.828427....
- //kreis(viertel)
- System.out.println("#1:\t" + one.isClose(two_1, 2.829)); //true
- System.out.println("#2:\t" + one.isClose(two_2, 2.829)); //true
- System.out.println("#3:\t" + one.isClose(two_3, 2.829)); //true
- System.out.println("#4:\t" + one.isClose(two_4, 2.829)); //true
- System.out.println();
- System.out.println("------------");
- System.out.println();
- //**isCloseToPath**
- System.out.println("**isCloseToPath**");
- Position[] xs_1 = new Position[4];
- Position[] xs_2 = new Position[5];
- Position two_5 = new Position(6,8);
- xs_1[0] = two_1;
- xs_1[1] = two_2;
- xs_1[2] = two_3;
- xs_1[3] = two_4;
- System.out.println("#1:\t" + one.isCloseToPath(xs_1, 2.829)); //true
- xs_2[0] = two_1;
- xs_2[1] = two_2;
- xs_2[2] = two_3;
- xs_2[3] = two_4;
- xs_2[4] = two_5;
- System.out.println("#2:\t" + one.isCloseToPath(xs_2, 2.829)); //false
- System.out.println();
- System.out.println("------------");
- System.out.println();
- //**isStopped**
- System.out.println("**isStopped**");
- Position[] test1 = new Position[7];
- Position[] test2 = new Position[2];
- Position p1 = new Position(1, 1.5);
- p1.sec = 1;
- Position p2 = new Position(1, 2);
- p2.sec = 2;
- Position p3 = new Position(1, 2.1);
- p3.sec = 3;
- Position p4 = new Position(1, 2.2);
- p4.sec = 4;
- Position p5 = new Position(1, 2.3);
- p5.sec = 5;
- Position p6 = new Position(1, 2.4);
- p6.sec = 6;
- Position p7 = new Position(3, 4);
- p7.sec = 7;
- test2[0] = p2;
- test2[1] = p1;
- test1[0] = p1;
- test1[1] = p2;
- test1[2] = p3;
- test1[3] = p4;
- test1[4] = p5;
- test1[5] = p6;
- test1[6] = p7;
- System.out.println("#1:\t" + isStopped(test1, 0.5)); // 2
- System.out.println("#2:\t" + isStopped(test2, 0.5)); //-1
- System.out.println();
- System.out.println("------------");
- System.out.println();
- //**chronologic**
- System.out.println("**chronologic**");
- System.out.println("#1:\t" + chronologic(test1)); // true
- System.out.println("#2:\t" + chronologic(test2)); // false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement