Code coverage report for src/geometry/Point.js

Statements: 100% (43 / 43)      Branches: 100% (16 / 16)      Functions: 100% (19 / 19)      Lines: 100% (43 / 43)     

All files » src/geometry/ » Point.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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116        1 461 461     1     173         14         14 14 14       11       57 57 57       1       21 21 21       21       21 21 21       2       54 54 54       1       1 1 1       1   1     1       3         3         1           1 142 129   13 10   3 2   1    
/*
 * L.Point represents a point with x and y coordinates.
 */
 
L.Point = function (/*Number*/ x, /*Number*/ y, /*Boolean*/ round) {
	this.x = (round ? Math.round(x) : x);
	this.y = (round ? Math.round(y) : y);
};
 
L.Point.prototype = {
 
	clone: function () {
		return new L.Point(this.x, this.y);
	},
 
	// non-destructive, returns a new point
	add: function (point) {
		return this.clone()._add(L.point(point));
	},
 
	// destructive, used directly for performance in situations where it's safe to modify existing point
	_add: function (point) {
		this.x += point.x;
		this.y += point.y;
		return this;
	},
 
	subtract: function (point) {
		return this.clone()._subtract(L.point(point));
	},
 
	_subtract: function (point) {
		this.x -= point.x;
		this.y -= point.y;
		return this;
	},
 
	divideBy: function (num) {
		return this.clone()._divideBy(num);
	},
 
	_divideBy: function (num) {
		this.x /= num;
		this.y /= num;
		return this;
	},
 
	multiplyBy: function (num) {
		return this.clone()._multiplyBy(num);
	},
 
	_multiplyBy: function (num) {
		this.x *= num;
		this.y *= num;
		return this;
	},
 
	round: function () {
		return this.clone()._round();
	},
 
	_round: function () {
		this.x = Math.round(this.x);
		this.y = Math.round(this.y);
		return this;
	},
 
	floor: function () {
		return this.clone()._floor();
	},
 
	_floor: function () {
		this.x = Math.floor(this.x);
		this.y = Math.floor(this.y);
		return this;
	},
 
	distanceTo: function (point) {
		point = L.point(point);
 
		var x = point.x - this.x,
		    y = point.y - this.y;
 
		return Math.sqrt(x * x + y * y);
	},
 
	equals: function (point) {
		return point.x === this.x &&
		       point.y === this.y;
	},
 
	contains: function (point) {
		return Math.abs(point.x) <= Math.abs(this.x) &&
		       Math.abs(point.y) <= Math.abs(this.y);
	},
 
	toString: function () {
		return 'Point(' +
		        L.Util.formatNum(this.x) + ', ' +
		        L.Util.formatNum(this.y) + ')';
	}
};
 
L.point = function (x, y, round) {
	if (x instanceof L.Point) {
		return x;
	}
	if (L.Util.isArray(x)) {
		return new L.Point(x[0], x[1]);
	}
	if (x === undefined || x === null) {
		return x;
	}
	return new L.Point(x, y, round);
};