//-----------------------------------------------------------------------
//
// Copyright (c) 2016 Mapbox. All rights reserved.
//
//-----------------------------------------------------------------------
namespace Mapbox.Utils
{
/// Represents a bounding box derived from a southwest corner and a northeast corner.
public struct Vector2dBounds
{
/// Southwest corner of bounding box.
public Vector2d SouthWest;
/// Northeast corner of bounding box.
public Vector2d NorthEast;
/// Initializes a new instance of the struct.
/// Geographic coordinate representing southwest corner of bounding box.
/// Geographic coordinate representing northeast corner of bounding box.
public Vector2dBounds(Vector2d sw, Vector2d ne)
{
this.SouthWest = sw;
this.NorthEast = ne;
}
/// Gets the south latitude.
/// The south latitude.
public double South {
get {
return this.SouthWest.x;
}
}
/// Gets the west longitude.
/// The west longitude.
public double West {
get {
return this.SouthWest.y;
}
}
/// Gets the north latitude.
/// The north latitude.
public double North {
get {
return this.NorthEast.x;
}
}
/// Gets the east longitude.
/// The east longitude.
public double East {
get {
return this.NorthEast.y;
}
}
///
/// Gets or sets the central coordinate of the bounding box. When
/// setting a new center, the bounding box will retain its original size.
///
/// The central coordinate.
public Vector2d Center {
get {
var lat = (this.SouthWest.x + this.NorthEast.x) / 2;
var lng = (this.SouthWest.y + this.NorthEast.y) / 2;
return new Vector2d(lat, lng);
}
set {
var lat = (this.NorthEast.x - this.SouthWest.x) / 2;
this.SouthWest.x = value.x - lat;
this.NorthEast.x = value.x + lat;
var lng = (this.NorthEast.y - this.SouthWest.y) / 2;
this.SouthWest.y = value.y - lng;
this.NorthEast.y = value.y + lng;
}
}
///
/// Creates a bound from two arbitrary points. Contrary to the constructor,
/// this method always creates a non-empty box.
///
/// The first point.
/// The second point.
/// The convex hull.
public static Vector2dBounds FromCoordinates(Vector2d a, Vector2d b)
{
var bounds = new Vector2dBounds(a, a);
bounds.Extend(b);
return bounds;
}
/// A bounding box containing the world.
/// The world bounding box.
public static Vector2dBounds World()
{
var sw = new Vector2d(-90, -180);
var ne = new Vector2d(90, 180);
return new Vector2dBounds(sw, ne);
}
/// Extend the bounding box to contain the point.
/// A geographic coordinate.
public void Extend(Vector2d point)
{
if (point.x < this.SouthWest.x)
{
this.SouthWest.x = point.x;
}
if (point.x > this.NorthEast.x)
{
this.NorthEast.x = point.x;
}
if (point.y < this.SouthWest.y)
{
this.SouthWest.y = point.y;
}
if (point.y > this.NorthEast.y)
{
this.NorthEast.y = point.y;
}
}
/// Extend the bounding box to contain the bounding box.
/// A bounding box.
public void Extend(Vector2dBounds bounds)
{
this.Extend(bounds.SouthWest);
this.Extend(bounds.NorthEast);
}
/// Whenever the geographic bounding box is empty.
/// true, if empty, false otherwise.
public bool IsEmpty()
{
return this.SouthWest.x > this.NorthEast.x ||
this.SouthWest.y > this.NorthEast.y;
}
///
/// Converts to an array of doubles.
///
/// An array of coordinates.
public double[] ToArray()
{
double[] array =
{
this.SouthWest.x,
this.SouthWest.y,
this.NorthEast.x,
this.NorthEast.y
};
return array;
}
/// Converts the Bbox to a URL snippet.
/// Returns a string for use in a Mapbox query URL.
public override string ToString()
{
return string.Format("{0},{1}", this.SouthWest.ToString(), this.NorthEast.ToString());
}
}
}