|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Length = require('./Length'); |
| 8 | + |
| 9 | +/** |
| 10 | + * Extends the start and end points of a Line by the given amounts. |
| 11 | + * |
| 12 | + * The amounts can be positive or negative. Positive points will increase the length of the line, |
| 13 | + * while negative ones will decrease it. |
| 14 | + * |
| 15 | + * If no `right` value is provided it will extend the length of the line equally in both directions. |
| 16 | + * |
| 17 | + * Pass a value of zero to leave the start or end point unchanged. |
| 18 | + * |
| 19 | + * @function Phaser.Geom.Line.Extend |
| 20 | + * @since 3.16.0 |
| 21 | + * |
| 22 | + * @param {Phaser.Geom.Line} line - The line instance to extend. |
| 23 | + * @param {number} left - The amount to extend the start of the line by. |
| 24 | + * @param {number} [right] - The amount to extend the end of the line by. If not given it will be set to the `left` value. |
| 25 | + * |
| 26 | + * @return {Phaser.Geom.Line} The modified Line instance. |
| 27 | + */ |
| 28 | +var Extend = function (line, left, right) |
| 29 | +{ |
| 30 | + if (right === undefined) { right = left; } |
| 31 | + |
| 32 | + var length = Length(line); |
| 33 | + |
| 34 | + var slopX = line.x2 - line.x1; |
| 35 | + var slopY = line.y2 - line.y1; |
| 36 | + |
| 37 | + if (left) |
| 38 | + { |
| 39 | + line.x1 = line.x1 - slopX / length * left; |
| 40 | + line.y1 = line.y1 - slopY / length * left; |
| 41 | + } |
| 42 | + |
| 43 | + if (right) |
| 44 | + { |
| 45 | + line.x2 = line.x2 + slopX / length * right; |
| 46 | + line.y2 = line.y2 + slopY / length * right; |
| 47 | + } |
| 48 | + |
| 49 | + return line; |
| 50 | +}; |
| 51 | + |
| 52 | +module.exports = Extend; |
0 commit comments