/** * Checks whether this <code>Envelope</code> is empty. * * @return true iff this instance is empty (the empty set). */ public boolean isEmpty() { return Double.isNaN(getMinC0()) || Double.isNaN(getMinC1()) || Double.isNaN(getMaxC0()) || Double.isNaN(getMaxC1()); }
/** * Creates an <code>Envelope</code> that is the set-theoretic union of this {@code Envelope} with the specified {@code Envelope} * * @param other other Envelope * @return an <code>Envelope</code> that encompasses both operands. * @throws IllegalArgumentException when the operand <code>Envelope</code>s don't have the same coordinate reference system. */ public Envelope<P> union(Envelope<P> other) { if (other == null || other.isEmpty()) return this; if (!this.getCoordinateReferenceSystem().equals(other.getCoordinateReferenceSystem())) throw new IllegalArgumentException("Envelopes have different CRS."); double minC0 = Math.min(getMinC0(), other.getMinC0()); double minC1 = Math.min(getMinC1(), other.getMinC1()); double maxC0 = Math.max(getMaxC0(), other.getMaxC0()); double maxC1 = Math.max(getMaxC1(), other.getMaxC1()); return new Envelope<P>(minC0, minC1, maxC0, maxC1, this.getCoordinateReferenceSystem()); }
/** * Checks whether this <code>Envelope</code> intersects the specifies <code>Envelope</code>. * <p/> * <p>Two instances intersect when their set-theoretic intersection is non-empty.</p> * * @param other the other <code>Envelope</code> * @return true iff this instance intersects with the other <code>Envelope</code> * @throws IllegalArgumentException when the specified <code>Envelope</code> doesn't have the same coordinate reference system as this instance. */ public boolean intersects(Envelope<P> other) { return !(isEmpty() || other.isEmpty()) && !(this.getMaxC0() < other.getMinC0() || this.getMinC0() > other.getMaxC0() || this.getMaxC1() < other.getMinC1() || this.getMinC1() > other.getMaxC1()); }
/** * Intersects the specified <code>Envelope</code> with this <code>Envelope</code> and returns the result. * * @param other the Envelope to intersect with this instance * @return the set-theoretic intersection of this Envelope and the specified Envelope. * @throws IllegalArgumentException when the specified <code>Envelope</code> doesn't have the same coordinate reference system as this instance. */ public Envelope<P> intersect(Envelope<P> other) { if (this.isEmpty() || other.isEmpty()) return mkEmpty(); if (!this.getCoordinateReferenceSystem().equals(other.getCoordinateReferenceSystem())) throw new IllegalArgumentException("Envelopes have different CRS."); double minC0 = Math.max(getMinC0(), other.getMinC0()); double minC1 = Math.max(getMinC1(), other.getMinC1()); double maxC0 = Math.min(getMaxC0(), other.getMaxC0()); double maxC1 = Math.min(getMaxC1(), other.getMaxC1()); if (minC0 > maxC0 || minC1 > maxC1) return mkEmpty(); return new Envelope<P>(minC0, minC1, maxC0, maxC1, this.getCoordinateReferenceSystem()); }
/** * Checks whether this <code>Envelope</code> contains the specifies <code>Envelope</code>. * * @param p the <code>Point</code> * @return true iff this instance contains the specified <code>Point</code> * @throws IllegalArgumentException when the specified <code>Point</code> doesn't have the same coordinate reference system as this instance. */ public boolean contains(P p) { if (!p.getClass().equals(this.getCoordinateReferenceSystem().getPositionClass())) throw new IllegalArgumentException("Position and envelope of different types"); if (isEmpty()) return false; return getMinC0() <= p.getCoordinate(0) && getMaxC0() >= p.getCoordinate(0) && getMinC1() <= p.getCoordinate(1) && getMaxC1() >= p.getCoordinate(1); }
/** * Checks whether this <code>Envelope</code> is contained within the specified <code>Envelope</code> * * @param other the other <code>Envelope</code> * @return true iff this instance is contained within the specified <code>Envelope</code> * @throws IllegalArgumentException when the specified <code>Envelope</code> doesn't have the same coordinate reference system as this instance. */ public boolean within(Envelope<P> other) { if (isEmpty()) return true; if (other.isEmpty()) return false; if (!this.getCoordinateReferenceSystem().equals(other.getCoordinateReferenceSystem())) throw new IllegalArgumentException("Envelopes have different CRS."); return other.getMinC0() <= this.getMinC0() && other.getMaxC0() >= this.getMaxC0() && other.getMinC1() <= this.getMinC1() && other.getMaxC1() >= this.getMaxC1(); }