/src/com/google/maps/extras/markermanager/GridBounds.as

http://gmaps-utility-library-flash.googlecode.com/ · ActionScript · 151 lines · 70 code · 24 blank · 57 comment · 10 complexity · fcfdd9e58fde6f7e08ec170d624556a8 MD5 · raw file

  1. /*
  2. * Copyright 2008 Google Inc.
  3. * Licensed under the Apache License, Version 2.0:
  4. * http://www.apache.org/licenses/LICENSE-2.0
  5. */
  6. package com.google.maps.extras.markermanager {
  7. import flash.geom.Point;
  8. public class GridBounds {
  9. public var z:Number;
  10. public var minX:Number;
  11. public var minY:Number;
  12. public var maxX:Number;
  13. public var maxY:Number;
  14. /**
  15. * A Bounds is defined by minimum and maximum X and Y coordinates on a plane.
  16. * @param {Array.<Point>} opt_points Points which this Bound must contain.
  17. * @constructor
  18. */
  19. public function GridBounds(opt_points:Array):void {
  20. this.minX = Infinity;
  21. this.minY = Infinity;
  22. this.maxX = -Infinity;
  23. this.maxY = -Infinity;
  24. if (opt_points && opt_points.length) {
  25. for (var i:Number = 0; i < opt_points.length; i++) {
  26. this.extend(opt_points[i]);
  27. }
  28. }
  29. }
  30. /**
  31. * Gets the minimum x and y in this bound.
  32. *
  33. * @return {Point}
  34. */
  35. public function min():Point {
  36. return new Point(this.minX, this.minY);
  37. }
  38. /**
  39. * Gets the maximum x and y in this bound.
  40. *
  41. * @return {Point}
  42. */
  43. public function max():Point {
  44. return new Point(this.maxX, this.maxY);
  45. }
  46. /**
  47. * @return {Size} The size of this bounds.
  48. */
  49. public function getSize():Point {
  50. return new Point(this.maxX - this.minX, this.maxY - this.minY);
  51. }
  52. /**
  53. * Gets the midpoint x and y in this bound.
  54. *
  55. * @return {Point} The midpoint.
  56. */
  57. public function mid():Point {
  58. return new Point((this.minX + this.maxX) / 2, (this.minY + this.maxY) / 2);
  59. }
  60. /**
  61. * Returns a string representation of this bound.
  62. *
  63. * @returns {string}
  64. */
  65. public function toString():String {
  66. return "(" + this.min() + ", " + this.max() + ")";
  67. }
  68. /**
  69. * Test for empty bounds.
  70. * @return {boolean} This Bounds is empty
  71. */
  72. public function isEmpty():Boolean {
  73. return (this.minX > this.maxX || this.minY > this.maxY);
  74. }
  75. /**
  76. * Returns true if this bounds (inclusively) contains the given bounds.
  77. * @param {Bounds} inner Inner Bounds.
  78. * @return {boolean} This Bounds contains the given Bounds.
  79. */
  80. public function containsBounds(inner:GridBounds): Boolean {
  81. var outer:GridBounds = this;
  82. return (outer.minX <= inner.minX &&
  83. outer.maxX >= inner.maxX &&
  84. outer.minY <= inner.minY &&
  85. outer.maxY >= inner.maxY);
  86. }
  87. /**
  88. * Returns true if this bounds (inclusively) contains the given point.
  89. * @param {Point} point The point to test.
  90. * @return {boolean} This Bounds contains the given Point.
  91. */
  92. public function containsPoint(point:Point):Boolean {
  93. var outer:GridBounds = this;
  94. return (outer.minX <= point.x &&
  95. outer.maxX >= point.x &&
  96. outer.minY <= point.y &&
  97. outer.maxY >= point.y);
  98. }
  99. /**
  100. * Extends this bounds to contain the given point.
  101. *
  102. * @param {Point} point Additional point.
  103. */
  104. public function extend(point:Point):void {
  105. if (this.isEmpty()) {
  106. this.minX = this.maxX = point.x;
  107. this.minY = this.maxY = point.y;
  108. } else {
  109. this.minX = Math.min(this.minX, point.x);
  110. this.maxX = Math.max(this.maxX, point.x);
  111. this.minY = Math.min(this.minY, point.y);
  112. this.maxY = Math.max(this.maxY, point.y);
  113. }
  114. }
  115. /**
  116. * Compare this bounds to another.
  117. * @param {Bounds} bounds The bounds to test against.
  118. * @return {boolean} True when the bounds are equal.
  119. */
  120. public function equals(bounds:GridBounds):Boolean {
  121. return this.minX == bounds.minX &&
  122. this.minY == bounds.minY &&
  123. this.maxX == bounds.maxX &&
  124. this.maxY == bounds.maxY;
  125. }
  126. }
  127. }