Skip to content

Commit 6a361ba

Browse files
committed
Line.Extend is a new static function that allows you extend the start and/or end points of a Line by the given amounts.
1 parent 730c84e commit 6a361ba

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ one set of bindings ever created, which makes things a lot cleaner.
169169
* `CanvasTexture.setPixel` is a new method that sets the given pixel in the CanvasTexture to the color and alpha values provided.
170170
* `CanvasTexture.getData` is a new method that will extract an ImageData block from the CanvasTexture from the region given.
171171
* `CanvasTexture.putData` is a new method that will put an ImageData block at the given coordinates in a CanvasTexture.
172+
* `Line.Extend` is a new static function that allows you extend the start and/or end points of a Line by the given amounts.
172173

173174
### Updates
174175

src/geom/line/Extend.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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;

src/geom/line/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Line.CenterOn = require('./CenterOn');
1212
Line.Clone = require('./Clone');
1313
Line.CopyFrom = require('./CopyFrom');
1414
Line.Equals = require('./Equals');
15+
Line.Extend = require('./Extend');
1516
Line.GetMidPoint = require('./GetMidPoint');
1617
Line.GetNearestPoint = require('./GetNearestPoint');
1718
Line.GetNormal = require('./GetNormal');

0 commit comments

Comments
 (0)