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