Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.04 KB | None | 0 0
  1. public class Position {
  2.  
  3.     final   int startDAY = 28800; //Sekunden seit Mitternacht um 8:00(Dienstbeginn)
  4.     private int sec;              //Sekunden seit Mitternacht eines Eintrages
  5.     private double longitude;     //geographische Länge (Dezimalgrad 15.0-17.0)
  6.     private double latitude;      //geographische Breite (Dezimalgrad 47.0-49.0)
  7.  
  8.     public Position(double longitude, double latitude){
  9.         this.sec       = startDAY;
  10.         this.longitude = longitude;
  11.         this.latitude  = latitude;
  12.     }
  13.  
  14.  
  15.     private static double min(double a, double b){
  16.         if(a <= b){
  17.             return a;
  18.         }else{
  19.             return b;
  20.         }
  21.     }
  22.  
  23.  
  24.     private static double max(double a, double b){
  25.         if(a >= b){
  26.             return a;
  27.         }else{
  28.             return b;
  29.         }
  30.     }
  31.  
  32.  
  33.     private static double pow(double val){
  34.         return val*val;
  35.     }
  36.  
  37.  
  38.     // Returns true if (and only if) longitude and latitude of this position (the current object) differ from
  39.     // longitude and latitude of x by at most eps.
  40.     public boolean isClose(Position x, double eps) {
  41.         //eps^2 = a^2(lon) + b^2(lat)
  42.         double aa = pow( max(x.longitude, this.longitude) - min(x.longitude, this.longitude));
  43.         double bb = pow( max(x.latitude, this.latitude) - min(x.latitude, this.latitude));
  44.         System.out.println("dist: " + Math.sqrt(aa+bb) + ", eps: " + eps + ", maxLon: " + max(x.longitude, this.longitude) + ", maxLat: " + max(x.latitude, this.latitude));
  45.         return ( pow(eps) >= aa + bb );
  46.     }
  47.  
  48.     // Returns true if (and only if) longitude and latitude of this position (the current object) differ from
  49.     // longitude and latitude of any element in xs by at most eps.
  50.     public boolean isCloseToPath(Position[] xs, double eps) {
  51.         for(Position pos : xs){
  52.             if(!this.isClose(pos, eps)){
  53.                 return false;
  54.             }
  55.         }
  56.         return true;
  57.     }
  58.  
  59.     // Parameter xs contains the positions of a tour. A tour is regarded as being stopped at a position in the array
  60.     // if this position is close (difference at most eps) to each of the following 4 positions in the array.
  61.     // The method returns the time (in seconds since midnight) of the first stop (lowest index in the array),
  62.     // or -1 if there is no stop.
  63.     public static int isStopped(Position[] xs, double eps) {
  64.         for(int i=1; i<xs.length; i++){
  65.             int count = 1;
  66.             if( xs[i-1].isClose(xs[i], eps) && ((i-1)+4) < xs.length ){
  67.                 for(int x=1; x<=4; x++){
  68.                     if(xs[i-1].isClose(xs[i+x], eps)){
  69.                         count++;
  70.                     }
  71.                     if(count == 4){
  72.                         return xs[i-1].sec;
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.         return -1;
  78.     }
  79.  
  80.     // Returns true if (and only if) the time of each position in xs is higher than that of each other position at a
  81.     // lower index.
  82.     public static boolean chronologic(Position[] xs) {
  83.         for(int i=1; i<xs.length; i++){
  84.             if(xs[i-1].sec > xs[i].sec){
  85.                 return false;
  86.             }
  87.         }
  88.         return true;
  89.     }
  90.  
  91.  
  92.     // Just for testing ...
  93.     public static void main(String[] args) {
  94.         //**isClose**
  95.         System.out.println("**isClose**");
  96.         Position one = new Position(4,5);
  97.         Position two_1 = new Position(2,3);
  98.         Position two_2 = new Position(2,7);
  99.         Position two_3 = new Position(6,3);
  100.         Position two_4 = new Position(6,7);
  101.  
  102.         //square(8) = 2.828427....
  103.         //kreis(viertel)
  104.         System.out.println("#1:\t" + one.isClose(two_1, 2.829));        //true
  105.         System.out.println("#2:\t" + one.isClose(two_2, 2.829));        //true
  106.         System.out.println("#3:\t" + one.isClose(two_3, 2.829));        //true
  107.         System.out.println("#4:\t" + one.isClose(two_4, 2.829));        //true
  108.  
  109.         System.out.println();
  110.         System.out.println("------------");
  111.         System.out.println();
  112.  
  113.         //**isCloseToPath**
  114.         System.out.println("**isCloseToPath**");
  115.         Position[] xs_1 = new Position[4];
  116.         Position[] xs_2 = new Position[5];
  117.         Position two_5  = new Position(6,8);
  118.  
  119.         xs_1[0] = two_1;
  120.         xs_1[1] = two_2;
  121.         xs_1[2] = two_3;
  122.         xs_1[3] = two_4;
  123.  
  124.         System.out.println("#1:\t" + one.isCloseToPath(xs_1, 2.829));   //true
  125.  
  126.         xs_2[0] = two_1;
  127.         xs_2[1] = two_2;
  128.         xs_2[2] = two_3;
  129.         xs_2[3] = two_4;
  130.         xs_2[4] = two_5;
  131.  
  132.         System.out.println("#2:\t" + one.isCloseToPath(xs_2, 2.829));   //false
  133.  
  134.         System.out.println();
  135.         System.out.println("------------");
  136.         System.out.println();
  137.  
  138.         //**isStopped**
  139.         System.out.println("**isStopped**");
  140.         Position[] test1 = new Position[7];
  141.         Position[] test2 = new Position[2];
  142.         Position p1 = new Position(1, 1.5);
  143.         p1.sec = 1;
  144.         Position p2 = new Position(1, 2);
  145.         p2.sec = 2;
  146.         Position p3 = new Position(1, 2.1);
  147.         p3.sec = 3;
  148.         Position p4 = new Position(1, 2.2);
  149.         p4.sec = 4;
  150.         Position p5 = new Position(1, 2.3);
  151.         p5.sec = 5;
  152.         Position p6 = new Position(1, 2.4);
  153.         p6.sec = 6;
  154.         Position p7 = new Position(3, 4);
  155.         p7.sec = 7;
  156.  
  157.         test2[0] = p2;
  158.         test2[1] = p1;
  159.  
  160.         test1[0] = p1;
  161.         test1[1] = p2;
  162.         test1[2] = p3;
  163.         test1[3] = p4;
  164.         test1[4] = p5;
  165.         test1[5] = p6;
  166.         test1[6] = p7;
  167.  
  168.         System.out.println("#1:\t" + isStopped(test1, 0.5));    // 2
  169.         System.out.println("#2:\t" + isStopped(test2, 0.5));    //-1
  170.  
  171.         System.out.println();
  172.         System.out.println("------------");
  173.         System.out.println();
  174.  
  175.         //**chronologic**
  176.         System.out.println("**chronologic**");
  177.         System.out.println("#1:\t" + chronologic(test1));       // true
  178.         System.out.println("#2:\t" + chronologic(test2));       // false
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement