Advertisement
a333c

InArea

Dec 28th, 2017
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. static final int LOCATION_X = 0;
  2. static final int LOCATION_Z = 1;
  3. /**
  4. * fはflagのf (0はダミーエリアでfalse,1はtrue)
  5. */
  6. public boolean inArea(Location l, ArraySet<location> locs) {
  7.     return inArea(l, locs, 0)
  8. }
  9.  
  10. public boolean inArea(Location l, ArraySet<Location> locs, int f) {
  11.     if (isSquare(locs)) { // 実際はフィールドではないことに留意(SWは南西、NEは北東)
  12.     return locs.SW.getX < l.getX && locs.SW.getZ < l.getZ && locs.NE.getX > l.getX && locs.NE.getZ > l;
  13.     } elseif (isTriangle(locs)) {
  14.     return ws(locs[0], locs[1], locs[2], l);
  15.     } else { // 多角形
  16.         Location nwst = new Location (min(locs,LOCATION_X),-114514,min(loc,LOCATION_Z)
  17.         Location nest = new Location (max(locs,LOCATION_X),-114514,min(loc,LOCATION_Z)
  18.         Location swst = new Location (min(locs,LOCATION_X),-114514,max(loc,LOCATION_Z)
  19.         Location sest = new Location (man(locs,LOCATION_X),-114514,man(loc,LOCATION_Z)
  20.         boolean inDummyArea = false;
  21.         //ダミーエリアの生成
  22.         ArrayList<Location> dummysNW = new ArrayList<>()
  23.         ArrayList<Location> dummysNE = new ArrayList<>()
  24.         ArrayList<Location> dummysSW = new ArrayList<>()
  25.         ArrayList<Location> dummysSE = new ArrayList<>()
  26.         for (Location lc : locs.values) {
  27.             if (lc.getX > nwst.getX || lc.getZ > nwst.getZ) {
  28.                 dummysNW.add(lc);
  29.             }
  30.            
  31.             if (lc.getX < nest.getX || lc.getZ > nwst.getZ) {
  32.                 dummysNE.add(lc);
  33.             }
  34.             if (lc.getX < sest.getX || lc.getZ < sest.getZ) {
  35.                 dummysSE.add(lc);
  36.             }
  37.             if (lc.getX > swst.getX || lc.getZ < swst.getZ) {
  38.                 dummysSW.add(lc);
  39.             }
  40.         }
  41.         //ダミーエリアの中にいるか?
  42.         if (inArea(l, dummysNW, !f) || inArea(l, dummysNE, !f) || inArea(l, dummysSE, !f) || inArea(l, dummysSW, !f)) { // ここでダミーエリアのダミーエリアは判定したいエリア
  43.             inDummyArea = true;
  44.         }
  45.        
  46.         if (inDummyArea) {
  47.             return f;
  48.         } else {
  49.             return !f;
  50.         }
  51.     }
  52. }
  53.  
  54. public double min(ArraySet<Location> locs,int flag) {
  55.     switch (flag) {
  56.         case LOCATION_X:
  57.             double x;
  58.             for (Location l : locs.values) {
  59.                 if (x > l.getX) x = l.getX
  60.             }
  61.             return x;
  62.         case LOCATION_Z:
  63.             double z;
  64.             for (Location l : locs.values) {
  65.                 if (z > l.getZ) z = l.getZ
  66.             }
  67.             return z;
  68.         default:
  69.             throw new IllegalArgumentException();
  70.             return null;
  71.     }
  72. }
  73.  
  74. public double max(ArraySet<Location> locs,int flag) {
  75.     switch (flag) {
  76.         case LOCATION_X:
  77.             double x;
  78.             for (Location l : locs.values) {
  79.                 if (x < l.getX) x = l.getX
  80.             }
  81.             return x;
  82.         case LOCATION_Z:
  83.             double z;
  84.             for (Location l : locs.values) {
  85.                 if (z < l.getZ) z = l.getZ
  86.             }
  87.             return z;
  88.         default:
  89.             throw new IllegalArgumentException();
  90.             return null;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement