⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boundingbox.cs

📁 Sharp Map 用于制作GIS系统S harp Map 用于制作GIS系统S harp Map 用于制作GIS系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap 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 of the License, or
// (at your option) any later version.
// 
// SharpMap 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 SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System;
using System.Collections.Generic;
using System.Text;

namespace SharpMap.Geometries
{
	/// <summary>
	/// Bounding box type with double precision
	/// </summary>
	/// <remarks>
	/// The Bounding Box represents a box whose sides are parallel to the two axes of the coordinate system.
	/// </remarks>
	[Serializable]
	public class BoundingBox : IEquatable<BoundingBox>
	{
		/// <summary>
		/// Initializes a bounding box
		/// </summary>
		/// <remarks>
		/// In case min values are larger than max values, the parameters will be swapped to ensure correct min/max boundary
		/// </remarks>
		/// <param name="minX">left</param>
		/// <param name="minY">bottom</param>
		/// <param name="maxX">right</param>
		/// <param name="maxY">top</param>
		public BoundingBox(double minX, double minY, double maxX, double maxY)
		{
			_Min = new Point(minX, minY);
			_Max = new Point(maxX, maxY);
			CheckMinMax();
		}

		/// <summary>
		/// Initializes a bounding box
		/// </summary>
		/// <param name="lowerLeft">Lower left corner</param>
		/// <param name="upperRight">Upper right corner</param>
		public BoundingBox(Geometries.Point lowerLeft, Geometries.Point upperRight)
			: this(lowerLeft.X, lowerLeft.Y, upperRight.X, upperRight.Y)
		{
		}

		/// <summary>
		/// Initializes a new Bounding Box based on the bounds from a set of geometries
		/// </summary>
		/// <param name="objects">list of objects</param>
		public BoundingBox(List<SharpMap.Geometries.Geometry> objects)
		{
			if (objects == null || objects.Count == 0)
			{
				_Min = null;
				_Max = null;
				return;
			}
			_Min = objects[0].GetBoundingBox().Min.Clone();
			_Max = objects[0].GetBoundingBox().Max.Clone();
			CheckMinMax();
			for (int i = 1; i < objects.Count; i++)
			{
				BoundingBox box = objects[i].GetBoundingBox();
				_Min.X = Math.Min(box.Min.X, this.Min.X);
				_Min.Y = Math.Min(box.Min.Y, this.Min.Y);
				_Max.X = Math.Max(box.Max.X, this.Max.X);
				_Max.Y = Math.Max(box.Max.Y, this.Max.Y);
			}
		}

		/// <summary>
		/// Initializes a new Bounding Box based on the bounds from a set of bounding boxes
		/// </summary>
		/// <param name="objects">list of objects</param>
		public BoundingBox(List<SharpMap.Geometries.BoundingBox> objects)
		{
			if (objects.Count == 0) { _Max = null; _Min = null; }
			else
			{
				_Min = objects[0].Min.Clone();
				_Max = objects[0].Max.Clone();
				for (int i = 1; i < objects.Count; i++)
				{
					_Min.X = Math.Min(objects[i].Min.X, this.Min.X);
					_Min.Y = Math.Min(objects[i].Min.Y, this.Min.Y);
					_Max.X = Math.Max(objects[i].Max.X, this.Max.X);
					_Max.Y = Math.Max(objects[i].Max.Y, this.Max.Y);
				}
			}
		}

		private Point _Min;

		/// <summary>
		/// Gets or sets the lower left corner.
		/// </summary>
		public SharpMap.Geometries.Point Min
		{
			get { return _Min; }
			set { _Min = value; }
		}
		private SharpMap.Geometries.Point _Max;

		/// <summary>
		/// Gets or sets the upper right corner.
		/// </summary>
		public SharpMap.Geometries.Point Max
		{
			get { return _Max; }
			set { _Max = value; }
		}

		/// <summary>
		/// Gets the left boundary
		/// </summary>
		public Double Left
		{
			get { return _Min.X; }
		}

		/// <summary>
		/// Gets the right boundary
		/// </summary>
		public Double Right
		{
			get { return _Max.X; }
		}

		/// <summary>
		/// Gets the top boundary
		/// </summary>
		public Double Top
		{
			get { return _Max.Y; }
		}

		/// <summary>
		/// Gets the bottom boundary
		/// </summary>
		public Double Bottom
		{
			get { return _Min.Y; }
		}

		/// <summary>
		/// Returns the width of the bounding box
		/// </summary>
		/// <returns>Width of boundingbox</returns>
		public double Width
		{
			get { return Math.Abs(_Max.X - _Min.X); }
		}
		/// <summary>
		/// Returns the height of the bounding box
		/// </summary>
		/// <returns>Height of boundingbox</returns>
		public double Height
		{
			get { return Math.Abs(_Max.Y - _Min.Y); }
		}

		/// <summary>
		/// Moves/translates the <see cref="BoundingBox"/> along the the specified vector
		/// </summary>
		/// <param name="vector">Offset vector</param>
		public void Offset(Point vector)
		{
			_Min += vector;
			_Max += vector;
		}

		/// <summary>
		/// Checks whether min values are actually smaller than max values and in that case swaps them.
		/// </summary>
		/// <returns>true if the bounding was changed</returns>
		public bool CheckMinMax()
		{
			bool wasSwapped = false;
			if (_Min.X > _Max.X)
			{
				double tmp = _Min.X;
				_Min.X = _Max.X;
				_Max.X = tmp;
				wasSwapped = true;
			}
			if (_Min.Y > _Max.Y)
			{
				double tmp = _Min.Y;
				_Min.Y = _Max.Y;
				_Max.Y = tmp;
				wasSwapped = true;
			}
			return wasSwapped;
		}

		/// <summary>
		/// Determines whether the boundingbox intersects another boundingbox
		/// </summary>
		/// <param name="box"></param>
		/// <returns></returns>
		public bool Intersects(BoundingBox box)
		{
			return !(box.Min.X > this.Max.X ||
					 box.Max.X < this.Min.X ||
					 box.Min.Y > this.Max.Y ||
					 box.Max.Y < this.Min.Y);
		}
		/// <summary>
		/// Returns true if this <see cref="BoundingBox"/> intersects the geometry
		/// </summary>
		/// <param name="g">Geometry</param>
		/// <returns>True if intersects</returns>
		public bool Intersects(Geometry g)
		{
			return this.Touches(g);
		}
		/// <summary>
		/// Returns true if this instance touches the <see cref="BoundingBox"/>
		/// </summary>
		/// <param name="r"><see cref="BoundingBox"/></param>
		/// <returns>True it touches</returns>
		public bool Touches(BoundingBox r)
		{
			for (uint cIndex = 0; cIndex < 2; cIndex++)
			{
				if ((Min[cIndex] > r.Min[cIndex] && Min[cIndex] < r.Min[cIndex]) ||
						(Max[cIndex] > r.Max[cIndex] && Max[cIndex] < r.Max[cIndex]))
					return true;
			}
			return false;
		}
		/// <summary>
		/// Returns true if this <see cref="BoundingBox"/> touches the geometry
		/// </summary>
		/// <param name="s">Geometry</param>
		/// <returns>True if touches</returns>
		public bool Touches(Geometry s)
		{
			if (s is Point) return Touches(s as Point);
			throw new NotImplementedException("Touches: Not implemented on this geometry type");
		}
		/// <summary>
		/// Returns true if this instance contains the <see cref="BoundingBox"/>
		/// </summary>
		/// <param name="r"><see cref="BoundingBox"/></param>
		/// <returns>True it contains</returns>
		public bool Contains(BoundingBox r)
		{
			for (uint cIndex = 0; cIndex < 2; cIndex++)
				if (Min[cIndex] > r.Min[cIndex] || Max[cIndex] < r.Max[cIndex]) return false;

			return true;
		}
		/// <summary>
		/// Returns true if this instance contains the geometry
		/// </summary>
		/// <param name="s"><see cref="BoundingBox"/></param>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -