#region Disclaimer / License // Copyright (C) 2014, Jackie Ng // http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #endregion Disclaimer / License using System; using System.ComponentModel; namespace OSGeo.MapGuide.ObjectModels.Common { /// /// Represents a rectangular bounding box /// public interface IEnvelope : INotifyPropertyChanged { /// /// Gets or sets the min X. /// /// The min X. double MinX { get; set; } /// /// Gets or sets the min Y. /// /// The min Y. double MinY { get; set; } /// /// Gets or sets the max X. /// /// The max X. double MaxX { get; set; } /// /// Gets or sets the max Y. /// /// The max Y. double MaxY { get; set; } } /// /// Extension method class /// public static class EnvelopeExtensions { static bool IsZero(this float val) { return Math.Abs(val) < float.Epsilon; } static bool IsZero(this double val) { return Math.Abs(val) < double.Epsilon; } /// /// Gets whether the given instance is empty /// /// /// public static bool IsEmpty(this IEnvelope env) { return env == null || (env.MaxX.IsZero() && env.MaxY.IsZero() && env.MinX.IsZero() && env.MinY.IsZero()); } /// /// Gets the center of the specified envelope /// /// /// public static IPoint2D Center(this IEnvelope env) { Check.ArgumentNotNull(env, nameof(env)); return ObjectFactory.CreatePoint2D( (env.MinX + env.MaxX) / 2, (env.MinY + env.MaxY) / 2); } /// /// Clones this instance /// /// The envelope. /// public static IEnvelope Clone(this IEnvelope env) { Check.ArgumentNotNull(env, nameof(env)); return ObjectFactory.CreateEnvelope(env.MinX, env.MinY, env.MaxX, env.MaxY); } /// /// Expands this envelope to accomodate the given envelope /// /// /// public static void ExpandToInclude(this IEnvelope env, IEnvelope e1) { Check.ArgumentNotNull(env, nameof(env)); if (e1 == null) return; if (e1.MinX < env.MinX) env.MinX = e1.MinX; if (e1.MinY < env.MinY) env.MinY = e1.MinY; if (e1.MaxX > env.MaxX) env.MaxX = e1.MaxX; if (e1.MaxY > env.MaxY) env.MaxY = e1.MaxY; } /// /// Indicates whether the specified coordinates are within this instance /// /// The env. /// The x. /// The y. /// /// true if [contains] [the specified env]; otherwise, false. /// public static bool Contains(this IEnvelope env, double x, double y) { Check.ArgumentNotNull(env, nameof(env)); return env.MinX <= x && env.MaxX >= x && env.MinY <= y && env.MaxY >= y; } /// /// Indicates whether the specified envelope intersects this instance /// /// The env. /// The other. /// public static bool Intersects(this IEnvelope env, IEnvelope other) { Check.ArgumentNotNull(env, nameof(env)); if (other == null) return false; return !(other.MinX > env.MaxX || other.MaxX < env.MinX || other.MinY > env.MaxY || other.MaxY < env.MinY); } } partial class Envelope : IEnvelope { internal Envelope() { } public Envelope(double minx, double miny, double maxx, double maxy) { this.lowerLeftCoordinateField = new EnvelopeLowerLeftCoordinate() { X = minx, Y = miny }; this.upperRightCoordinateField = new EnvelopeUpperRightCoordinate() { X = maxx, Y = maxy }; } /// /// Gets or sets the lower X value /// public double MinX { get { return this.lowerLeftCoordinateField.X; } set { this.lowerLeftCoordinateField.X = value; OnPropertyChanged(nameof(MinX)); } } /// /// Gets or set the lower Y value /// public double MinY { get { return this.lowerLeftCoordinateField.Y; } set { this.lowerLeftCoordinateField.Y = value; OnPropertyChanged(nameof(MinY)); } } /// /// Gets or sets the upper X value /// public double MaxX { get { return this.upperRightCoordinateField.X; } set { this.upperRightCoordinateField.X = value; OnPropertyChanged(nameof(MaxX)); } } /// /// Gets or sets the upper Y value /// public double MaxY { get { return this.upperRightCoordinateField.Y; } set { this.upperRightCoordinateField.Y = value; OnPropertyChanged(nameof(MaxY)); } } } }