iadept

Test

Feb 14th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.13 KB | None | 0 0
  1. import java.util.Collections;
  2. import java.util.Comparator;
  3. import java.util.LinkedList;
  4. import java.util.Random;
  5.  
  6.  
  7. public class Main {
  8.    
  9.     /**
  10.      * Один метр в градусах,
  11.      */
  12.     private final double DEG = 0.000001;
  13.    
  14.     public final class Zone {
  15.         public int id;
  16.         public double x;
  17.         public double y;
  18.         public double r;
  19.        
  20.         public Zone(int id, double x, double y, double r) {
  21.             this.id = id;
  22.             this.x = x;
  23.             this.y = y;
  24.             this.r = r;
  25.         }
  26.        
  27.        
  28.     }
  29.    
  30.     class XComparator implements Comparator {
  31.            
  32.         public int compare(Object obj1, Object obj2){
  33.             Zone zone1 = (Zone)obj1;
  34.             Zone zone2 = (Zone)obj2;
  35.             double x1 = zone1.x - zone1.r;        
  36.             double x2 = zone2.x - zone2.r;
  37.            
  38.             if(x1 > x2)
  39.                 return 1;
  40.             else if(x1 < x2)
  41.                 return -1;
  42.             else
  43.                 return -1;    
  44.         }
  45.        
  46.     }
  47.  
  48.     class BComparator implements Comparator {
  49.            
  50.         public int compare(Object obj1, Object obj2){
  51.             Zone zone = (Zone)obj1;
  52.             Zone point = (Zone)obj2;
  53.             double bx = zone.x - zone.r;        
  54.             double px = point.x ;
  55.            
  56.             if(bx < px)
  57.                 return 1;
  58.             else if(bx > px)
  59.                 return -1;
  60.             else
  61.                 return -1;    
  62.         }
  63.        
  64.     }
  65.    
  66.     private LinkedList<Zone> map_x;
  67.    
  68.     private XComparator xComparator = new XComparator();
  69.     private BComparator comparator = new BComparator();
  70.    
  71.     private double boffset_x = 0;
  72.     private double boffset_y = 0;
  73.    
  74.     private double eoffset_x = 0;
  75.     private double eoffset_y = 0;
  76.    
  77.    
  78.     public static void main(String[] args) {
  79.        
  80.         Main man = new Main();
  81.         Random randomGenerator = new Random();
  82.         long start = System.currentTimeMillis();
  83.         int i = 0;
  84.         while(i < 100000) {
  85.             man.addZone(i,randomGenerator.nextInt(359)+randomGenerator.nextDouble(), randomGenerator.nextInt(359)+randomGenerator.nextDouble(), randomGenerator.nextInt(10000));
  86.             i++;
  87.         }
  88.         start = System.currentTimeMillis() - start;
  89.         System.out.println("Gen:" + start);
  90.         start = System.currentTimeMillis();
  91.         while(i < 250) {
  92.            
  93.             man.probe(randomGenerator.nextInt(359)+randomGenerator.nextDouble(), randomGenerator.nextInt(359)+randomGenerator.nextDouble());
  94.             i++;
  95.            
  96.         }
  97.         start = System.currentTimeMillis() - start;
  98.         System.out.println("Probe:" + start);
  99.         /*
  100.         man.addZone(1, 25, 60, 15);
  101.         man.addZone(2, 15, 40, 10);
  102.         man.addZone(3, 25, 20, 20);
  103.         man.addZone(4, 35, 40, 15);
  104.         man.addZone(5, 40, 60, 10);
  105.        
  106.         man.probe(30, 30);
  107.         */
  108.     }
  109.    
  110.     public Main() {
  111.         map_x = new LinkedList<Main.Zone>();
  112.     }
  113.    
  114.     private double convert(int r) {
  115.         return r*DEG;
  116.     }
  117.  
  118.     public void addZone(int id, double x, double y, int r) {
  119.         double nr = convert(r);
  120.         Zone zone = new Zone(id,x,y,nr);
  121.         if(map_x.size() == 0) {
  122.             map_x.add(zone);
  123.         } else {
  124.             int xId = Collections.binarySearch(map_x, zone, xComparator)*-1;
  125.             map_x.add(xId-1, zone);
  126.         }
  127.         if(x-nr < 0)
  128.             boffset_x = (x-nr)*-1;
  129.         if(y-nr > 0)
  130.             boffset_y = (y-nr)*-1;
  131.        
  132.         if(x+nr > 360)
  133.             eoffset_x = 360 - x+nr;
  134.         if(y+nr > 360)
  135.             eoffset_y = 360 - y+nr;
  136.        
  137.     }
  138.      
  139.     public void probe(double x, double y) {
  140.         System.out.println("Probe: (" + x + ":" + y + ")");
  141.    
  142.         Zone point = new Zone(0,x,y,0);
  143.        
  144.         int xId = Collections.binarySearch(map_x, point, comparator)*-1;   
  145.        
  146.         for(int i = xId; i < map_x.size(); i++) {
  147.             Zone check = map_x.get(i);
  148.             if(check.x + check.r < x) break;
  149.             if((x-check.x)*(x-check.x)+(y-check.y)*(y-check.y) <= check.r*check.r) {
  150.                 //System.out.println("Goal: " + check.id);
  151.             }
  152.         }
  153.        
  154.         if( x < eoffset_x) {
  155.             x = x + 360;
  156.         }
  157.         if( y < eoffset_y) {
  158.             y = y + 360;
  159.         }
  160.         if( 360 - x < boffset_x) {
  161.             x = x + 360;
  162.         }
  163.         if( 360 -  x > boffset_x) {
  164.             x = x + 360;
  165.         }
  166.        
  167.         point = new Zone(0,x,y,0);
  168.        
  169.         xId = Collections.binarySearch(map_x, point, comparator)*-1;   
  170.        
  171.         for(int i = xId; i < map_x.size(); i++) {
  172.             Zone check = map_x.get(i);
  173.             if(check.x + check.r < x) break;
  174.             if((x-check.x)*(x-check.x)+(y-check.y)*(y-check.y) <= check.r*check.r) {
  175.                 //System.out.println("Goal: " + check.id);
  176.             }
  177.         }
  178.        
  179.     }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment