sector.java
来自「world wind java sdk 源码」· Java 代码 · 共 1,046 行 · 第 1/3 页
JAVA
1,046 行
* will be the maximum of the two sectors. The sectors are assumed to be normalized to +/- 90 degrees latitude and * +/- 180 degrees longitude. The result of the operation is undefined if they are not. * * @param that the sector to join with <code>this</code>. * @return A new sector formed from the extremes of the two sectors, or <code>this</code> if the incoming sector is * <code>null</code>. */ public final Sector union(Sector that) { if (that == null) return this; Angle minLat = this.minLatitude; Angle maxLat = this.maxLatitude; Angle minLon = this.minLongitude; Angle maxLon = this.maxLongitude; if (that.minLatitude.degrees < this.minLatitude.degrees) minLat = that.minLatitude; if (that.maxLatitude.degrees > this.maxLatitude.degrees) maxLat = that.maxLatitude; if (that.minLongitude.degrees < this.minLongitude.degrees) minLon = that.minLongitude; if (that.maxLongitude.degrees > this.maxLongitude.degrees) maxLon = that.maxLongitude; return new Sector(minLat, maxLat, minLon, maxLon); } public final Sector union(Angle latitude, Angle longitude) { if (latitude == null || longitude == null) return this; Angle minLat = this.minLatitude; Angle maxLat = this.maxLatitude; Angle minLon = this.minLongitude; Angle maxLon = this.maxLongitude; if (latitude.degrees < this.minLatitude.degrees) minLat = latitude; if (latitude.degrees > this.maxLatitude.degrees) maxLat = latitude; if (longitude.degrees < this.minLongitude.degrees) minLon = longitude; if (longitude.degrees > this.maxLongitude.degrees) maxLon = longitude; return new Sector(minLat, maxLat, minLon, maxLon); } public static Sector union(Sector sectorA, Sector sectorB) { if (sectorA == null || sectorB == null) { if (sectorA == sectorB) return sectorA; return sectorB == null ? sectorA : sectorB; } return sectorA.union(sectorB); } public static Sector union(Iterable<? extends Sector> sectors) { if (sectors == null) { String msg = Logging.getMessage("nullValue.SectorListIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } Angle minLat = Angle.POS90; Angle maxLat = Angle.NEG90; Angle minLon = Angle.POS180; Angle maxLon = Angle.NEG180; for (Sector s : sectors) { if (s == null) continue; for (LatLon p : s) { if (p.getLatitude().degrees < minLat.degrees) minLat = p.getLatitude(); if (p.getLatitude().degrees > maxLat.degrees) maxLat = p.getLatitude(); if (p.getLongitude().degrees < minLon.degrees) minLon = p.getLongitude(); if (p.getLongitude().degrees > maxLon.degrees) maxLon = p.getLongitude(); } } return new Sector(minLat, maxLat, minLon, maxLon); } public final Sector intersection(Sector that) { if (that == null) return this; Angle minLat, maxLat; minLat = (this.minLatitude.degrees > that.minLatitude.degrees) ? this.minLatitude : that.minLatitude; maxLat = (this.maxLatitude.degrees < that.maxLatitude.degrees) ? this.maxLatitude : that.maxLatitude; if (minLat.degrees > maxLat.degrees) return null; Angle minLon, maxLon; minLon = (this.minLongitude.degrees > that.minLongitude.degrees) ? this.minLongitude : that.minLongitude; maxLon = (this.maxLongitude.degrees < that.maxLongitude.degrees) ? this.maxLongitude : that.maxLongitude; if (minLon.degrees > maxLon.degrees) return null; return new Sector(minLat, maxLat, minLon, maxLon); } public final Sector intersection(Angle latitude, Angle longitude) { if (latitude == null || longitude == null) return this; if (!this.contains(latitude, longitude)) return null; return new Sector(latitude, latitude, longitude, longitude); } public Sector[] subdivide() { Angle midLat = Angle.average(this.minLatitude, this.maxLatitude); Angle midLon = Angle.average(this.minLongitude, this.maxLongitude); Sector[] sectors = new Sector[4]; sectors[0] = new Sector(this.minLatitude, midLat, this.minLongitude, midLon); sectors[1] = new Sector(this.minLatitude, midLat, midLon, this.maxLongitude); sectors[2] = new Sector(midLat, this.maxLatitude, this.minLongitude, midLon); sectors[3] = new Sector(midLat, this.maxLatitude, midLon, this.maxLongitude); return sectors; } public Sector[] subdivide(int div) { double dLat = this.deltaLat.degrees / div; double dLon = this.deltaLon.degrees / div; Sector[] sectors = new Sector[div * div]; int idx = 0; for (int row = 0; row < div; row++) for (int col = 0; col < div; col++) sectors[idx++] = Sector.fromDegrees( this.minLatitude.degrees + dLat * row, this.minLatitude.degrees + dLat * row + dLat, this.minLongitude.degrees + dLon * col, this.minLongitude.degrees + dLon * col + dLon); return sectors; } /** * Returns a string indicating the sector's angles. * * @return A string indicating the sector's angles. */ @Override public String toString() { java.lang.StringBuffer sb = new java.lang.StringBuffer(); sb.append("("); sb.append(this.minLatitude.toString()); sb.append(", "); sb.append(this.minLongitude.toString()); sb.append(")"); sb.append(", "); sb.append("("); sb.append(this.maxLatitude.toString()); sb.append(", "); sb.append(this.maxLongitude.toString()); sb.append(")"); return sb.toString(); } /** * Retrieve the size of this object in bytes. This implementation returns an exact value of the object's size. * * @return the size of this object in bytes */ public long getSizeInBytes() { return 4 * minLatitude.getSizeInBytes(); // 4 angles } /** * Compares this sector to a specified sector according to their minimum latitude, minimum longitude, maximum * latitude, and maximum longitude, respectively. * * @param that the <code>Sector</code> to compareTo with <code>this</code>. * @return -1 if this sector compares less than that specified, 0 if they're equal, and 1 if it compares greater. * @throws IllegalArgumentException if <code>that</code> is null */ public int compareTo(Sector that) { if (that == null) { String msg = Logging.getMessage("nullValue.SectorIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } if (this.getMinLatitude().compareTo(that.getMinLatitude()) < 0) return -1; if (this.getMinLatitude().compareTo(that.getMinLatitude()) > 0) return 1; if (this.getMinLongitude().compareTo(that.getMinLongitude()) < 0) return -1; if (this.getMinLongitude().compareTo(that.getMinLongitude()) > 0) return 1; if (this.getMaxLatitude().compareTo(that.getMaxLatitude()) < 0) return -1; if (this.getMaxLatitude().compareTo(that.getMaxLatitude()) > 0) return 1; if (this.getMaxLongitude().compareTo(that.getMaxLongitude()) < 0) return -1; if (this.getMaxLongitude().compareTo(that.getMaxLongitude()) > 0) return 1; return 0; } /** * Creates an iterator over the four corners of the sector, starting with the southwest position and continuing * counter-clockwise. * * @return an iterator for the sector. */ public Iterator<LatLon> iterator() { return new Iterator<LatLon>() { private int position = 0; public boolean hasNext() { return this.position < 4; } public LatLon next() { if (this.position > 3) throw new NoSuchElementException(); LatLon p; switch (this.position) { case 0: p = new LatLon(Sector.this.getMinLatitude(), Sector.this.getMinLongitude()); break; case 1: p = new LatLon(Sector.this.getMinLatitude(), Sector.this.getMaxLongitude()); break; case 2: p = new LatLon(Sector.this.getMaxLatitude(), Sector.this.getMaxLongitude()); break; default: p = new LatLon(Sector.this.getMaxLatitude(), Sector.this.getMinLongitude()); break; } ++this.position; return p; } public void remove() { throw new UnsupportedOperationException(); } }; } public List<LatLon> asList() { ArrayList<LatLon> list = new ArrayList<LatLon>(4); for (LatLon ll : this) list.add(ll); return list; } /** * Tests the equality of the sectors' angles. Sectors are equal if all of their corresponding angles are equal. * * @param o the sector to compareTo with <code>this</code>. * @return <code>true</code> if the four corresponding angles of each sector are equal, <code>false</code> * otherwise. */ @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final gov.nasa.worldwind.geom.Sector sector = (gov.nasa.worldwind.geom.Sector) o; if (!maxLatitude.equals(sector.maxLatitude)) return false; if (!maxLongitude.equals(sector.maxLongitude)) return false; if (!minLatitude.equals(sector.minLatitude)) return false; //noinspection RedundantIfStatement if (!minLongitude.equals(sector.minLongitude)) return false; return true; } /** * Computes a hash code from the sector's four angles. * * @return a hash code incorporating the sector's four angles. */ @Override public int hashCode() { int result; result = minLatitude.hashCode(); result = 29 * result + maxLatitude.hashCode(); result = 29 * result + minLongitude.hashCode(); result = 29 * result + maxLongitude.hashCode(); return result; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?