# Code coverage report for src/geometry/Bounds.js

## Statements: 100% (37 / 37) Branches: 100% (30 / 30) Functions: 100% (10 / 10) Lines: 100% (36 / 36)

All files » src/geometry/ » Bounds.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96

    1
    53

    42

    42
    95

    1

    99

    99
    43
    43

    56
    56
    56
    56

    99

    3

    1

    1

    1

    4

    4
    2

    2

    4
    2
    2

    2

    4

    2

    2

    2

    3

    1
    8
    4

    4

     /*
     * L.Bounds represents a rectangular area on the screen in pixel coordinates.
     */

    L.Bounds = function (a, b) { //(Point, Point) or Point[]
    	if (!a) { return; }

    	var points = b ? [a, b] : a;

    	for (var i = 0, len = points.length; i < len; i++) {
    		this.extend(points[i]);
    	}
    };

    L.Bounds.prototype = {
    	// extend the bounds to contain the given point
    	extend: function (point) { // (Point)
    		point = L.point(point);

    		if (!this.min && !this.max) {
    			this.min = point.clone();
    			this.max = point.clone();
    		} 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);
    		}
    		return this;
    	},

    	getCenter: function (round) { // (Boolean) -> Point
    		return new L.Point(
    		        (this.min.x + this.max.x) / 2,
    		        (this.min.y + this.max.y) / 2, round);
    	},

    	getBottomLeft: function () { // -> Point
    		return new L.Point(this.min.x, this.max.y);
    	},

    	getTopRight: function () { // -> Point
    		return new L.Point(this.max.x, this.min.y);
    	},

    	getSize: function () {
    		return this.max.subtract(this.min);
    	},

    	contains: function (obj) { // (Bounds) or (Point) -> Boolean
    		var min, max;

    		if (typeof obj[0] === 'number' || obj instanceof L.Point) {
    			obj = L.point(obj);
    		} else {
    			obj = L.bounds(obj);
    		}

    		if (obj instanceof L.Bounds) {
    			min = obj.min;
    			max = obj.max;
    		} else {
    			min = max = obj;
    		}

    		return (min.x >= this.min.x) &&
    		       (max.x <= this.max.x) &&
    		       (min.y >= this.min.y) &&
    		       (max.y <= this.max.y);
    	},

    	intersects: function (bounds) { // (Bounds) -> Boolean
    		bounds = L.bounds(bounds);

    		var min = this.min,
    		    max = this.max,
    		    min2 = bounds.min,
    		    max2 = bounds.max,
    		    xIntersects = (max2.x >= min.x) && (min2.x <= max.x),
    		    yIntersects = (max2.y >= min.y) && (min2.y <= max.y);

    		return xIntersects && yIntersects;
    	},

    	isValid: function () {
    		return !!(this.min && this.max);
    	}
    };

    L.bounds = function (a, b) { // (Bounds) or (Point, Point) or (Point[])
    	if (!a || a instanceof L.Bounds) {
    		return a;
    	}
    	return new L.Bounds(a, b);
    };