Phaser.Line = function (x1, y1, x2, y2){ x1 = x1 || 0; y1 = y1 || 0; x2 = x2 || 0; y2 = y2 || 0; this.start = new Phaser.Point(x1, y1); this.end = new Phaser.Point(x2, y2); this.type = Phaser.LINE; } ; Phaser.Line.prototype.constructor = Phaser.Line; Phaser.Line.prototype = { setTo: function (x1, y1, x2, y2){ this.start.setTo(x1, y1); this.end.setTo(x2, y2); return this; } , fromSprite: function (startSprite, endSprite, useCenter){ if (useCenter === undefined) { useCenter = false ; } if (useCenter) { return this.setTo(startSprite.centerX, startSprite.centerY, endSprite.centerX, endSprite.centerY); } return this.setTo(startSprite.x, startSprite.y, endSprite.x, endSprite.y); } , fromAngle: function (x, y, angle, length){ this.start.setTo(x, y); this.end.setTo(x + (Math.cos(angle) * length), y + (Math.sin(angle) * length)); return this; } , rotate: function (angle, asDegrees){ var cx = (this.start.x + this.end.x) / 2; var cy = (this.start.y + this.end.y) / 2; this.start.rotate(cx, cy, angle, asDegrees); this.end.rotate(cx, cy, angle, asDegrees); return this; } , rotateAround: function (x, y, angle, asDegrees){ this.start.rotate(x, y, angle, asDegrees); this.end.rotate(x, y, angle, asDegrees); return this; } , intersects: function (line, asSegment, result){ return Phaser.Line.intersectsPoints(this.start, this.end, line.start, line.end, asSegment, result); } , reflect: function (line){ return Phaser.Line.reflect(this, line); } , midPoint: function (out){ if (out === undefined) { out = new Phaser.Point(); } out.x = (this.start.x + this.end.x) / 2; out.y = (this.start.y + this.end.y) / 2; return out; } , centerOn: function (x, y){ var cx = (this.start.x + this.end.x) / 2; var cy = (this.start.y + this.end.y) / 2; var tx = x - cx; var ty = y - cy; this.start.add(tx, ty); this.end.add(tx, ty); } , pointOnLine: function (x, y){ return ((x - this.start.x) * (this.end.y - this.start.y) === (this.end.x - this.start.x) * (y - this.start.y)); } , pointOnSegment: function (x, y){ var xMin = Math.min(this.start.x, this.end.x); var xMax = Math.max(this.start.x, this.end.x); var yMin = Math.min(this.start.y, this.end.y); var yMax = Math.max(this.start.y, this.end.y); return (this.pointOnLine(x, y) && (x >= xMin && x <= xMax) && (y >= yMin && y <= yMax)); } , random: function (out){ if (out === undefined) { out = new Phaser.Point(); } var t = Math.random(); out.x = this.start.x + t * (this.end.x - this.start.x); out.y = this.start.y + t * (this.end.y - this.start.y); return out; } , coordinatesOnLine: function (stepRate, results){ if (stepRate === undefined) { stepRate = 1; } if (results === undefined) { results = [] ; } var x1 = Math.round(this.start.x); var y1 = Math.round(this.start.y); var x2 = Math.round(this.end.x); var y2 = Math.round(this.end.y); var dx = Math.abs(x2 - x1); var dy = Math.abs(y2 - y1); var sx = (x1 < x2)? 1: -1; var sy = (y1 < y2)? 1: -1; var err = dx - dy; results.push([x1, y1] ); var i = 1; while (!((x1 === x2) && (y1 === y2))){ var e2 = err << 1; if (e2 > - dy) { err -= dy; x1 += sx; } if (e2 < dx) { err += dx; y1 += sy; } if (i % stepRate === 0) { results.push([x1, y1] ); } i++ ; } return results; } , clone: function (output){ if (output === undefined || output === null ) { output = new Phaser.Line(this.start.x, this.start.y, this.end.x, this.end.y); } else { output.setTo(this.start.x, this.start.y, this.end.x, this.end.y); } return output; } } ; Object.defineProperty(Phaser.Line.prototype, "length", { get: function (){ return Math.sqrt((this.end.x - this.start.x) * (this.end.x - this.start.x) + (this.end.y - this.start.y) * (this.end.y - this.start.y)); } } ); Object.defineProperty(Phaser.Line.prototype, "angle", { get: function (){ return Math.atan2(this.end.y - this.start.y, this.end.x - this.start.x); } } ); Object.defineProperty(Phaser.Line.prototype, "slope", { get: function (){ return (this.end.y - this.start.y) / (this.end.x - this.start.x); } } ); Object.defineProperty(Phaser.Line.prototype, "perpSlope", { get: function (){ return - ((this.end.x - this.start.x) / (this.end.y - this.start.y)); } } ); Object.defineProperty(Phaser.Line.prototype, "x", { get: function (){ return Math.min(this.start.x, this.end.x); } } ); Object.defineProperty(Phaser.Line.prototype, "y", { get: function (){ return Math.min(this.start.y, this.end.y); } } ); Object.defineProperty(Phaser.Line.prototype, "left", { get: function (){ return Math.min(this.start.x, this.end.x); } } ); Object.defineProperty(Phaser.Line.prototype, "right", { get: function (){ return Math.max(this.start.x, this.end.x); } } ); Object.defineProperty(Phaser.Line.prototype, "top", { get: function (){ return Math.min(this.start.y, this.end.y); } } ); Object.defineProperty(Phaser.Line.prototype, "bottom", { get: function (){ return Math.max(this.start.y, this.end.y); } } ); Object.defineProperty(Phaser.Line.prototype, "width", { get: function (){ return Math.abs(this.start.x - this.end.x); } } ); Object.defineProperty(Phaser.Line.prototype, "height", { get: function (){ return Math.abs(this.start.y - this.end.y); } } ); Object.defineProperty(Phaser.Line.prototype, "normalX", { get: function (){ return Math.cos(this.angle - 1.570796326794897); } } ); Object.defineProperty(Phaser.Line.prototype, "normalY", { get: function (){ return Math.sin(this.angle - 1.570796326794897); } } ); Object.defineProperty(Phaser.Line.prototype, "normalAngle", { get: function (){ return Phaser.Math.wrap(this.angle - 1.570796326794897, - Math.PI, Math.PI); } } ); Phaser.Line.intersectsPoints = function (a, b, e, f, asSegment, result){ if (asSegment === undefined) { asSegment = true ; } if (result === undefined) { result = new Phaser.Point(); } var a1 = b.y - a.y; var a2 = f.y - e.y; var b1 = a.x - b.x; var b2 = e.x - f.x; var c1 = (b.x * a.y) - (a.x * b.y); var c2 = (f.x * e.y) - (e.x * f.y); var denom = (a1 * b2) - (a2 * b1); if (denom === 0) { return null ; } result.x = ((b1 * c2) - (b2 * c1)) / denom; result.y = ((a2 * c1) - (a1 * c2)) / denom; if (asSegment) { var uc = ((f.y - e.y) * (b.x - a.x) - (f.x - e.x) * (b.y - a.y)); var ua = (((f.x - e.x) * (a.y - e.y)) - (f.y - e.y) * (a.x - e.x)) / uc; var ub = (((b.x - a.x) * (a.y - e.y)) - ((b.y - a.y) * (a.x - e.x))) / uc; if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) { return result; } else { return null ; } } return result; } ; Phaser.Line.intersects = function (a, b, asSegment, result){ return Phaser.Line.intersectsPoints(a.start, a.end, b.start, b.end, asSegment, result); } ; Phaser.Line.intersectsRectangle = function (line, rect){ if (!Phaser.Rectangle.intersects(line, rect)) { return false ; } var x1 = line.start.x; var y1 = line.start.y; var x2 = line.end.x; var y2 = line.end.y; var bx1 = rect.x; var by1 = rect.y; var bx2 = rect.right; var by2 = rect.bottom; var t = 0; if ((x1 >= bx1 && x1 <= bx2 && y1 >= by1 && y1 <= by2) || (x2 >= bx1 && x2 <= bx2 && y2 >= by1 && y2 <= by2)) { return true ; } if (x1 < bx1 && x2 >= bx1) { t = y1 + (y2 - y1) * (bx1 - x1) / (x2 - x1); if (t > by1 && t <= by2) { return true ; } } else if (x1 > bx2 && x2 <= bx2) { t = y1 + (y2 - y1) * (bx2 - x1) / (x2 - x1); if (t >= by1 && t <= by2) { return true ; } } if (y1 < by1 && y2 >= by1) { t = x1 + (x2 - x1) * (by1 - y1) / (y2 - y1); if (t >= bx1 && t <= bx2) { return true ; } } else if (y1 > by2 && y2 <= by2) { t = x1 + (x2 - x1) * (by2 - y1) / (y2 - y1); if (t >= bx1 && t <= bx2) { return true ; } } return false ; } ; Phaser.Line.reflect = function (a, b){ return 2 * b.normalAngle - 3.141592653589793 - a.angle; } ;