Phaser API Documentation

  Version: 
Filter

A triangle is a plane created by connecting three points. The first two arguments specify the first point, the middle two arguments specify the second point, and the last two arguments specify the third point.

Constructor:

new Triangle([x1], [y1], [x2], [y2], [x3], [y3])

Parameters:

name type arguments description
x1 number <optional>

x coordinate of the first point.

y1 number <optional>

y coordinate of the first point.

x2 number <optional>

x coordinate of the second point.

y2 number <optional>

y coordinate of the second point.

x3 number <optional>

x coordinate of the third point.

y3 number <optional>

y coordinate of the third point.

Since: 3.0.0

Members

bottom: number
Focus
Focus

Description:

Bottom most Y coordinate of the triangle. Setting it moves the triangle on the Y axis accordingly.

Type:
number
Since: 3.0.0
Focus
Focus
left: number
Focus
Focus

Description:

Left most X coordinate of the triangle. Setting it moves the triangle on the X axis accordingly.

Type:
number
Since: 3.0.0
Focus
Focus
top: number
Focus
Focus

Description:

Top most Y coordinate of the triangle. Setting it moves the triangle on the Y axis accordingly.

Type:
number
Since: 3.0.0
Focus
Focus
<readonly> type: number
Focus
Focus

Description:

The geometry constant type of this object: GEOM_CONST.TRIANGLE. Used for fast type comparisons.

Type:
number
Since: 3.19.0
Focus
Focus
x1: number
Focus
Focus

Description:

x coordinate of the first point.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
x2: number
Focus
Focus

Description:

x coordinate of the second point.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
x3: number
Focus
Focus

Description:

x coordinate of the third point.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
y1: number
Focus
Focus

Description:

y coordinate of the first point.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
y2: number
Focus
Focus

Description:

y coordinate of the second point.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
y3: number
Focus
Focus

Description:

y coordinate of the third point.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus

Methods

contains(x, y)
Focus
Focus

Description:

Checks whether a given points lies within the triangle.

Parameters:

name type description
x number

The x coordinate of the point to check.

y number

The y coordinate of the point to check.

Returns:
Description:

true if the coordinate pair is within the triangle, otherwise false.

Type:
  • boolean
Since: 3.0.0
Focus
Focus
getLineA([line])
Focus
Focus

Description:

Returns a Line object that corresponds to Line A of this Triangle.

Parameters:

name type arguments description
line Phaser.Geom.Line <optional>

A Line object to set the results in. If undefined a new Line will be created.

Returns:
Description:

A Line object that corresponds to line A of this Triangle.

Since: 3.0.0
Focus
Focus
getLineB([line])
Focus
Focus

Description:

Returns a Line object that corresponds to Line B of this Triangle.

Parameters:

name type arguments description
line Phaser.Geom.Line <optional>

A Line object to set the results in. If undefined a new Line will be created.

Returns:
Description:

A Line object that corresponds to line B of this Triangle.

Since: 3.0.0
Focus
Focus
getLineC([line])
Focus
Focus

Description:

Returns a Line object that corresponds to Line C of this Triangle.

Parameters:

name type arguments description
line Phaser.Geom.Line <optional>

A Line object to set the results in. If undefined a new Line will be created.

Returns:
Description:

A Line object that corresponds to line C of this Triangle.

Since: 3.0.0
Focus
Focus
getPoint(position, [output])
Focus
Focus

Description:

Returns a specific point on the triangle.

Parameters:

name type arguments description
position number

Position as float within 0 and 1. 0 equals the first point.

output object | Phaser.Geom.Point <optional>

Optional Point, or point-like object, that the calculated point will be written to.

Returns:
Description:

Calculated Point that represents the requested position. It is the same as output when this parameter has been given.

Type:
Since: 3.0.0
Focus
Focus
getPoints(quantity, [stepRate], [output])
Focus
Focus

Description:

Calculates a list of evenly distributed points on the triangle. It is either possible to pass an amount of points to be generated (quantity) or the distance between two points (stepRate).

Parameters:

name type arguments description
quantity number

Number of points to be generated. Can be falsey when stepRate should be used. All points have the same distance along the triangle.

stepRate number <optional>

Distance between two points. Will only be used when quantity is falsey.

output array | Array.<Phaser.Geom.Point> <optional>

Optional Array for writing the calculated points into. Otherwise a new array will be created.

Returns:
Description:

Returns a list of calculated Point instances or the filled array passed as parameter output.

Type:
Since: 3.0.0
Focus
Focus
getRandomPoint([point])
Focus
Focus

Description:

Returns a random point along the triangle.

Parameters:

name type arguments description
point Phaser.Geom.Point <optional>

Optional Point that should be modified. Otherwise a new one will be created.

Returns:
Description:

Random Point. When parameter point has been provided it will be returned.

Since: 3.0.0
Focus
Focus
setTo([x1], [y1], [x2], [y2], [x3], [y3])
Focus
Focus

Description:

Sets all three points of the triangle. Leaving out any coordinate sets it to be 0.

Parameters:

name type arguments description
x1 number <optional>

x coordinate of the first point.

y1 number <optional>

y coordinate of the first point.

x2 number <optional>

x coordinate of the second point.

y2 number <optional>

y coordinate of the second point.

x3 number <optional>

x coordinate of the third point.

y3 number <optional>

y coordinate of the third point.

Returns:
Description:

This Triangle object.

Since: 3.0.0
Focus
Focus
<static> Area(triangle)
Focus
Focus

Description:

Returns the area of a Triangle.

Parameters:

name type description
triangle Phaser.Geom.Triangle

The Triangle to use.

Returns:
Description:

The area of the Triangle, always non-negative.

Type:
  • number
Since: 3.0.0
Source: src/geom/triangle/Area.js (Line 9)
Focus
Focus
<static> BuildEquilateral(x, y, length)
Focus
Focus

Description:

Builds an equilateral triangle. In the equilateral triangle, all the sides are the same length (congruent) and all the angles are the same size (congruent). The x/y specifies the top-middle of the triangle (x1/y1) and length is the length of each side.

Parameters:

name type description
x number

x coordinate of the top point of the triangle.

y number

y coordinate of the top point of the triangle.

length number

Length of each side of the triangle.

Returns:
Description:

The Triangle object of the given size.

Since: 3.0.0
Focus
Focus
<static> BuildFromPolygon(data, [holes], [scaleX], [scaleY], [out])
Focus
Focus

Description:

Takes an array of vertex coordinates, and optionally an array of hole indices, then returns an array of Triangle instances, where the given vertices have been decomposed into a series of triangles.

Parameters:

name type arguments Default description
data array

A flat array of vertex coordinates like [x0,y0, x1,y1, x2,y2, ...]

holes array <optional> null

An array of hole indices if any (e.g. [5, 8] for a 12-vertex input would mean one hole with vertices 5–7 and another with 8–11).

scaleX number <optional> 1

Horizontal scale factor to multiply the resulting points by.

scaleY number <optional> 1

Vertical scale factor to multiply the resulting points by.

out array | Array.<Phaser.Geom.Triangle> <optional>

An array to store the resulting Triangle instances in. If not provided, a new array is created.

Returns:
Description:

An array of Triangle instances, where each triangle is based on the decomposed vertices data.

Type:
Since: 3.0.0
Focus
Focus
<static> BuildRight(x, y, width, height)
Focus
Focus

Description:

Builds a right triangle, i.e. one which has a 90-degree angle and two acute angles.

Parameters:

name type description
x number

The X coordinate of the right angle, which will also be the first X coordinate of the constructed Triangle.

y number

The Y coordinate of the right angle, which will also be the first Y coordinate of the constructed Triangle.

width number

The length of the side which is to the left or to the right of the right angle.

height number

The length of the side which is above or below the right angle.

Returns:
Description:

The constructed right Triangle.

Since: 3.0.0
Focus
Focus
<static> CenterOn(triangle, x, y, [centerFunc])
Focus
Focus

Description:

Positions the Triangle so that it is centered on the given coordinates.

Parameters:

name type arguments description
triangle Phaser.Geom.Triangle

The triangle to be positioned.

x number

The horizontal coordinate to center on.

y number

The vertical coordinate to center on.

centerFunc CenterFunction <optional>

The function used to center the triangle. Defaults to Centroid centering.

Returns:
Description:

The Triangle that was centered.

Since: 3.0.0
Focus
Focus
<static> Centroid(triangle, [out])
Focus
Focus

Description:

Calculates the position of a Triangle's centroid, which is also its center of mass (center of gravity).

The centroid is the point in a Triangle at which its three medians (the lines drawn from the vertices to the bisectors of the opposite sides) meet. It divides each one in a 2:1 ratio.

Parameters:

name type arguments description
triangle Phaser.Geom.Triangle

The Triangle to use.

out object | Phaser.Geom.Point <optional>

An object to store the coordinates in.

Returns:
Description:

The out object with modified x and y properties, or a new Point if none was provided.

Type:
Since: 3.0.0
Focus
Focus
<static> CircumCenter(triangle, [out])
Focus
Focus

Description:

Computes the circumcentre of a triangle. The circumcentre is the centre of the circumcircle, the smallest circle which encloses the triangle. It is also the common intersection point of the perpendicular bisectors of the sides of the triangle, and is the only point which has equal distance to all three vertices of the triangle.

Parameters:

name type arguments description
triangle Phaser.Geom.Triangle

The Triangle to get the circumcenter of.

out Phaser.Math.Vector2 <optional>

The Vector2 object to store the position in. If not given, a new Vector2 instance is created.

Returns:
Description:

A Vector2 object holding the coordinates of the circumcenter of the Triangle.

Since: 3.0.0
Focus
Focus
<static> CircumCircle(triangle, [out])
Focus
Focus

Description:

Finds the circumscribed circle (circumcircle) of a Triangle object. The circumcircle is the circle which touches all of the triangle's vertices.

Parameters:

name type arguments description
triangle Phaser.Geom.Triangle

The Triangle to use as input.

out Phaser.Geom.Circle <optional>

An optional Circle to store the result in.

Returns:
Description:

The updated out Circle, or a new Circle if none was provided.

Since: 3.0.0
Focus
Focus
<static> Clone(source)
Focus
Focus

Description:

Clones a Triangle object.

Parameters:

name type description
source Phaser.Geom.Triangle

The Triangle to clone.

Returns:
Description:

A new Triangle identical to the given one but separate from it.

Since: 3.0.0
Source: src/geom/triangle/Clone.js (Line 9)
Focus
Focus
<static> Contains(triangle, x, y)
Focus
Focus

Description:

Checks if a point (as a pair of coordinates) is inside a Triangle's bounds.

Parameters:

name type description
triangle Phaser.Geom.Triangle

The Triangle to check.

x number

The X coordinate of the point to check.

y number

The Y coordinate of the point to check.

Returns:
Description:

true if the point is inside the Triangle, otherwise false.

Type:
  • boolean
Since: 3.0.0
Focus
Focus
<static> ContainsArray(triangle, points, [returnFirst], [out])
Focus
Focus

Description:

Filters an array of point-like objects to only those contained within a triangle. If returnFirst is true, will return an array containing only the first point in the provided array that is within the triangle (or an empty array if there are no such points).

Parameters:

name type arguments Default description
triangle Phaser.Geom.Triangle

The triangle that the points are being checked in.

points Array.<Phaser.Geom.Point>

An array of point-like objects (objects that have an x and y property)

returnFirst boolean <optional> false

If true, return an array containing only the first point found that is within the triangle.

out array <optional>

If provided, the points that are within the triangle will be appended to this array instead of being added to a new array. If returnFirst is true, only the first point found within the triangle will be appended. This array will also be returned by this function.

Returns:
Description:

An array containing all the points from points that are within the triangle, if an array was provided as out, points will be appended to that array and it will also be returned here.

Type:
Since: 3.0.0
Focus
Focus
<static> ContainsPoint(triangle, point)
Focus
Focus

Description:

Tests if a triangle contains a point.

Parameters:

name type description
triangle Phaser.Geom.Triangle

The triangle.

point any | Phaser.Geom.Point | Phaser.Math.Vector2

The point to test, or any point-like object with public x and y properties.

Returns:
Description:

true if the point is within the triangle, otherwise false.

Type:
  • boolean
Since: 3.0.0
Focus
Focus
<static> CopyFrom(source, dest)
Focus
Focus

Description:

Copy the values of one Triangle to a destination Triangle.

Parameters:

name type description
source Phaser.Geom.Triangle

The source Triangle to copy the values from.

dest Phaser.Geom.Triangle

The destination Triangle to copy the values to.

Returns:
Description:

The destination Triangle.

Since: 3.0.0
Focus
Focus
<static> Decompose(triangle, [out])
Focus
Focus

Description:

Decomposes a Triangle into an array of its points.

Parameters:

name type arguments description
triangle Phaser.Geom.Triangle

The Triangle to decompose.

out array <optional>

An array to store the points into.

Returns:
Description:

The provided out array, or a new array if none was provided, with three objects with x and y properties representing each point of the Triangle appended to it.

Type:
  • array
Since: 3.0.0
Focus
Focus
<static> Equals(triangle, toCompare)
Focus
Focus

Description:

Returns true if two triangles have the same coordinates.

Parameters:

name type description
triangle Phaser.Geom.Triangle

The first triangle to check.

toCompare Phaser.Geom.Triangle

The second triangle to check.

Returns:
Description:

true if the two given triangles have the exact same coordinates, otherwise false.

Type:
  • boolean
Since: 3.0.0
Focus
Focus
<static> GetPoint(triangle, position, [out])
Focus
Focus

Description:

Returns a Point from around the perimeter of a Triangle.

Parameters:

name type arguments description
triangle Phaser.Geom.Triangle

The Triangle to get the point on its perimeter from.

position number

The position along the perimeter of the triangle. A value between 0 and 1.

out object | Phaser.Geom.Point <optional>

An option Point, or Point-like object to store the value in. If not given a new Point will be created.

Returns:
Description:

A Point object containing the given position from the perimeter of the triangle.

Type:
Since: 3.0.0
Focus
Focus
<static> GetPoints(triangle, quantity, stepRate, [out])
Focus
Focus

Description:

Returns an array of evenly spaced points on the perimeter of a Triangle.

Parameters:

name type arguments description
triangle Phaser.Geom.Triangle

The Triangle to get the points from.

quantity number

The number of evenly spaced points to return. Set to 0 to return an arbitrary number of points based on the stepRate.

stepRate number

If quantity is 0, the distance between each returned point.

out array | Array.<Phaser.Geom.Point> <optional>

An array to which the points should be appended.

Returns:
Description:

The modified out array, or a new array if none was provided.

Type:
Since: 3.0.0
Focus
Focus
<static> InCenter(triangle, [out])
Focus
Focus

Description:

Calculates the position of the incenter of a Triangle object. This is the point where its three angle bisectors meet and it's also the center of the incircle, which is the circle inscribed in the triangle.

Parameters:

name type arguments description
triangle Phaser.Geom.Triangle

The Triangle to find the incenter of.

out Phaser.Geom.Point <optional>

An optional Point in which to store the coordinates.

Returns:
Description:

Point (x, y) of the center pixel of the triangle.

Since: 3.0.0
Focus
Focus
<static> Offset(triangle, x, y)
Focus
Focus

Description:

Moves each point (vertex) of a Triangle by a given offset, thus moving the entire Triangle by that offset.

Parameters:

name type description
triangle Phaser.Geom.Triangle

The Triangle to move.

x number

The horizontal offset (distance) by which to move each point. Can be positive or negative.

y number

The vertical offset (distance) by which to move each point. Can be positive or negative.

Returns:
Description:

The modified Triangle.

Since: 3.0.0
Focus
Focus
<static> Perimeter(triangle)
Focus
Focus

Description:

Gets the length of the perimeter of the given triangle. Calculated by adding together the length of each of the three sides.

Parameters:

name type description
triangle Phaser.Geom.Triangle

The Triangle to get the length from.

Returns:
Description:

The length of the Triangle.

Type:
  • number
Since: 3.0.0
Focus
Focus
<static> Random(triangle, [out])
Focus
Focus

Description:

Returns a random Point from within the area of the given Triangle.

Parameters:

name type arguments description
triangle Phaser.Geom.Triangle

The Triangle to get a random point from.

out Phaser.Geom.Point <optional>

The Point object to store the position in. If not given, a new Point instance is created.

Returns:
Description:

A Point object holding the coordinates of a random position within the Triangle.

Since: 3.0.0
Focus
Focus
<static> Rotate(triangle, angle)
Focus
Focus

Description:

Rotates a Triangle about its incenter, which is the point at which its three angle bisectors meet.

Parameters:

name type description
triangle Phaser.Geom.Triangle

The Triangle to rotate.

angle number

The angle by which to rotate the Triangle, in radians.

Returns:
Description:

The rotated Triangle.

Since: 3.0.0
Focus
Focus
<static> RotateAroundPoint(triangle, point, angle)
Focus
Focus

Description:

Rotates a Triangle at a certain angle about a given Point or object with public x and y properties.

Parameters:

name type description
triangle Phaser.Geom.Triangle

The Triangle to rotate.

point Phaser.Geom.Point

The Point to rotate the Triangle about.

angle number

The angle by which to rotate the Triangle, in radians.

Returns:
Description:

The rotated Triangle.

Since: 3.0.0
Focus
Focus
<static> RotateAroundXY(triangle, x, y, angle)
Focus
Focus

Description:

Rotates an entire Triangle at a given angle about a specific point.

Parameters:

name type description
triangle Phaser.Geom.Triangle

The Triangle to rotate.

x number

The X coordinate of the point to rotate the Triangle about.

y number

The Y coordinate of the point to rotate the Triangle about.

angle number

The angle by which to rotate the Triangle, in radians.

Returns:
Description:

The rotated Triangle.

Since: 3.0.0
Focus
Focus