Bounds with specs

This commit is contained in:
mourner 2010-09-13 14:58:28 +03:00
parent 59a1736b3e
commit c958de9721
3 changed files with 83 additions and 2 deletions

View File

@ -18,10 +18,10 @@
<script type="text/javascript" src="../src/core/Util.js"></script> <script type="text/javascript" src="../src/core/Util.js"></script>
<script type="text/javascript" src="../src/core/Class.js"></script> <script type="text/javascript" src="../src/core/Class.js"></script>
<script type="text/javascript" src="../src/core/Events.js"></script> <script type="text/javascript" src="../src/core/Events.js"></script>
<script type="text/javascript" src="../src/core/Options.js"></script>
<!-- /geometry --> <!-- /geometry -->
<script type="text/javascript" src="../src/geometry/Point.js"></script> <script type="text/javascript" src="../src/geometry/Point.js"></script>
<script type="text/javascript" src="../src/geometry/Bounds.js"></script>
<script type="text/javascript" src="../src/geometry/Transformation.js"></script> <script type="text/javascript" src="../src/geometry/Transformation.js"></script>
<!-- /geo --> <!-- /geo -->
@ -41,10 +41,10 @@
<script type="text/javascript" src="suites/core/UtilSpec.js"></script> <script type="text/javascript" src="suites/core/UtilSpec.js"></script>
<script type="text/javascript" src="suites/core/ClassSpec.js"></script> <script type="text/javascript" src="suites/core/ClassSpec.js"></script>
<script type="text/javascript" src="suites/core/EventsSpec.js"></script> <script type="text/javascript" src="suites/core/EventsSpec.js"></script>
<script type="text/javascript" src="suites/core/OptionsSpec.js"></script>
<!-- /geometry --> <!-- /geometry -->
<script type="text/javascript" src="suites/geometry/PointSpec.js"></script> <script type="text/javascript" src="suites/geometry/PointSpec.js"></script>
<script type="text/javascript" src="suites/geometry/BoundsSpec.js"></script>
<script type="text/javascript" src="suites/geometry/TransformationSpec.js"></script> <script type="text/javascript" src="suites/geometry/TransformationSpec.js"></script>
<!-- /geo --> <!-- /geo -->

View File

@ -0,0 +1,43 @@
describe('Bounds', function() {
var a, b;
beforeEach(function() {
a = new L.Bounds(
new L.Point(14, 12),
new L.Point(30, 40));
b = new L.Bounds([
new L.Point(20, 12),
new L.Point(14, 20),
new L.Point(30, 40)
]);
});
describe('constructor', function() {
it('should create bounds with proper min & max on (Point, Point)', function() {
expect(a.min).toEqual(new L.Point(14, 12));
expect(a.max).toEqual(new L.Point(30, 40));
});
it('should create bounds with proper min & max on (Point[])', function() {
expect(b.min).toEqual(new L.Point(14, 12));
expect(b.max).toEqual(new L.Point(30, 40));
});
});
describe('#extend', function() {
it('should extend the bounds to contain the given point', function() {
a.extend(new L.Point(50, 20));
expect(a.min).toEqual(new L.Point(14, 12));
expect(a.max).toEqual(new L.Point(50, 40));
b.extend(new L.Point(25, 50));
expect(b.min).toEqual(new L.Point(14, 12));
expect(b.max).toEqual(new L.Point(30, 50));
});
});
describe('#getCenter', function() {
it('should return the center point', function() {
expect(a.getCenter()).toEqual(new L.Point(22, 26));
});
});
});

38
src/geometry/Bounds.js Normal file
View File

@ -0,0 +1,38 @@
/*
* L.Bounds represents a rectangular area on the screen in pixel coordinates.
*/
L.Bounds = L.Class.extend({
initialize: function(min, max) { //(Point, Point) or Point[]
var points = (min instanceof Array ? min : [min, max]);
for (var i = 0, len = points.length; i < len; i++) {
this.extend(points[i]);
}
},
// extend the bounds to contain the given point
extend: function(/*Point*/ point) {
if (!this.min && !this.max) {
this.min = new L.Point(point.x, point.y);
this.max = new L.Point(point.x, point.y);
} else {
this.min.x = Math.min(point.x, this.min.x);
this.max.x = Math.max(point.x, this.max.x);
this.min.y = Math.min(point.y, this.min.y);
this.max.y = Math.max(point.y, this.max.y);
}
},
getCenter: function(round)/*->Point*/ {
return new L.Point(
(this.min.x + this.max.x) / 2,
(this.min.y + this.max.y) / 2, round);
},
contains: function(/*Bounds*/ bounds)/*->Boolean*/ {
return (bounds.min.x >= this.min.x) &&
(bounds.max.x <= this.max.x) &&
(bounds.min.y >= this.min.y) &&
(bounds.max.y <= this.max.y);
}
});