Skip to content

Commit 8e7b717

Browse files
committed
Line.midPoint will return a Point object where the x and y values correspond to the center (or midpoint) of the Line segment.
1 parent 62c8705 commit 8e7b717

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ If you are an exceptional JavaScript developer and would like to join the Phaser
256256
* Text.addFontStyle and Text.addFontWeight allow you to apply font weights and styles to specific characters in a Text object. For example you can now include bold or italics within single Text objects (thanks @jdnichollsc #1950)
257257
* PIXI.CanvasPool is a new static global created to deal with the issue of resource leaks and continuous DOM node build-up when creating lots of Text or BitmapData objects, or when calling `generateTexture` on any display object. The CanvasPool will do its best to re-use out dated canvas elements rather than filling up the DOM with new ones.
258258
* Sprite.setTexture has a new `destroyBase` parameter - set this to `true` if you know the base used a generated texture that isn't being used by any other sprites. This will free-up the canvas for further re-use by other calls to generateTexture or Text objects.
259+
* Line.midPoint will return a Point object where the x and y values correspond to the center (or midpoint) of the Line segment.
259260

260261
### Updates
261262

src/geom/Line.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,24 @@ Phaser.Line.prototype = {
157157

158158
},
159159

160+
/**
161+
* Returns a Point object where the x and y values correspond to the center (or midpoint) of the Line segment.
162+
*
163+
* @method Phaser.Line#midPoint
164+
* @param {Phaser.Point} [out] - A Phaser.Point object into which the result will be populated. If not given a new Point object is created.
165+
* @return {Phaser.Point} A Phaser.Point object with the x and y values set to the center of the line segment.
166+
*/
167+
midPoint: function (out) {
168+
169+
if (out === undefined) { out = new Phaser.Point(); }
170+
171+
out.x = (this.start.x + this.end.x) / 2;
172+
out.y = (this.start.y + this.end.y) / 2;
173+
174+
return out;
175+
176+
},
177+
160178
/**
161179
* Tests if the given coordinates fall on this line. See pointOnSegment to test against just the line segment.
162180
*

typescript/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,7 @@ declare module Phaser {
21302130
fromAngle(x: number, y: number, angle: number, length: number): Phaser.Line;
21312131
fromSprite(startSprite: Phaser.Sprite, endSprite: Phaser.Sprite, useCenter?: boolean): Phaser.Line;
21322132
intersects(line: Phaser.Line, asSegment?: boolean, result?: Phaser.Point): Phaser.Point;
2133+
midPoint(out?: Phaser.Point): Phaser.Point;
21332134
pointOnLine(x: number, y: number): boolean;
21342135
pointOnSegment(x: number, y: number): boolean;
21352136
random(out?: Phaser.Point): Phaser.Point;

0 commit comments

Comments
 (0)