Skip to main content
Version: Phaser v4.0.0

Triangle

A Triangle is a closed polygon defined by three vertices in 2D space, useful for hit testing, defining spawn or trigger regions, and geometric calculations. It is a geometry object only — not a Game Object — and cannot be rendered directly. To draw a Triangle to the screen, pass it to a Graphics Game Object's strokeTriangleShape or fillTriangleShape method.

The three vertices are stored as coordinate pairs (x1,y1), (x2,y2), and (x3,y3). All coordinates default to 0 if not provided.

Constructor

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

Parameters

nametypeoptionaldefaultdescription
x1numberYes0x coordinate of the first point.
y1numberYes0y coordinate of the first point.
x2numberYes0x coordinate of the second point.
y2numberYes0y coordinate of the second point.
x3numberYes0x coordinate of the third point.
y3numberYes0y coordinate of the third point.

Scope: static

Source: src/geom/triangle/Triangle.js#L15
Since: 3.0.0

Public Methods

Area

<static> Area(triangle)

Description:

Returns the area of a Triangle.

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to use.

Returns: number - The area of the Triangle, always non-negative.

Source: src/geom/triangle/Area.js#L9
Since: 3.0.0


BuildEquilateral

<static> BuildEquilateral(x, y, length)

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:

nametypeoptionaldescription
xnumberNox coordinate of the top point of the triangle.
ynumberNoy coordinate of the top point of the triangle.
lengthnumberNoLength of each side of the triangle.

Returns: Phaser.Geom.Triangle - A new equilateral Triangle with its apex at (x, y) and all sides of the specified length.

Source: src/geom/triangle/BuildEquilateral.js#L9
Since: 3.0.0


BuildFromPolygon

<static> BuildFromPolygon(data, [holes], [scaleX], [scaleY], [out])

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.

Tags:

  • generic

Parameters:

nametypeoptionaldefaultdescription
dataarrayNoA flat array of vertex coordinates like [x0,y0, x1,y1, x2,y2, ...]
holesarrayYesnullAn 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).
scaleXnumberYes1Horizontal scale factor to multiply the resulting points by.
scaleYnumberYes1Vertical scale factor to multiply the resulting points by.
outarray | Array.<Phaser.Geom.Triangle>YesAn array to store the resulting Triangle instances in. If not provided, a new array is created.

Returns: array, Array.<Phaser.Geom.Triangle> - An array of Triangle instances, where each triangle is based on the decomposed vertices data.

Source: src/geom/triangle/BuildFromPolygon.js#L10
Since: 3.0.0


BuildRight

<static> BuildRight(x, y, width, height)

Description:

Builds a right triangle, i.e. one which has a 90-degree angle and two acute angles. The x and y coordinates mark the position of the right-angle vertex, which becomes the first point of the Triangle. The width and height values determine the lengths of the two sides adjacent to the right angle and may be positive or negative, which controls the direction in which each side extends from the right-angle vertex.

Parameters:

nametypeoptionaldescription
xnumberNoThe X coordinate of the right angle, which will also be the first X coordinate of the constructed Triangle.
ynumberNoThe Y coordinate of the right angle, which will also be the first Y coordinate of the constructed Triangle.
widthnumberNoThe length of the side which is to the left or to the right of the right angle.
heightnumberNoThe length of the side which is above or below the right angle. If not given, defaults to the value of width.

Returns: Phaser.Geom.Triangle - The constructed right Triangle.

Source: src/geom/triangle/BuildRight.js#L13
Since: 3.0.0


CenterOn

<static> CenterOn(triangle, x, y, [centerFunc])

Description:

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

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe triangle to be positioned.
xnumberNoThe horizontal coordinate to center on.
ynumberNoThe vertical coordinate to center on.
centerFuncCenterFunctionYesThe function used to center the triangle. Defaults to Centroid centering.

Returns: Phaser.Geom.Triangle - The Triangle that was centered.

Source: src/geom/triangle/CenterOn.js#L18
Since: 3.0.0


Centroid

<static> Centroid(triangle, [out])

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.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to use.
outPhaser.Math.Vector2YesA Vector2 object to store the coordinates in.

Returns: Phaser.Math.Vector2 - The out object with modified x and y properties, or a new Vector2 if none was provided.

Source: src/geom/triangle/Centroid.js#L9
Since: 3.0.0


CircumCenter

<static> CircumCenter(triangle, [out])

Description:

Computes the circumcenter of a triangle. The circumcenter is the centre of the circumcircle, the unique circle that passes through all three vertices of 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.

Adapted from http://bjornharrtell.github.io/jsts/doc/api/jsts_geom_Triangle.js.html

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to get the circumcenter of.
outPhaser.Math.Vector2YesThe Vector2 object to store the position in. If not given, a new Vector2 instance is created.

Returns: Phaser.Math.Vector2 - A Vector2 object holding the coordinates of the circumcenter of the Triangle.

Source: src/geom/triangle/CircumCenter.js#L28
Since: 3.0.0


CircumCircle

<static> CircumCircle(triangle, [out])

Description:

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

Adapted from https://gist.github.com/mutoo/5617691

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to use as input.
outPhaser.Geom.CircleYesAn optional Circle to store the result in.

Returns: Phaser.Geom.Circle - The updated out Circle, or a new Circle if none was provided.

Source: src/geom/triangle/CircumCircle.js#L9
Since: 3.0.0


Clone

<static> Clone(source)

Description:

Clones a Triangle object.

Parameters:

nametypeoptionaldescription
sourcePhaser.Geom.TriangleNoThe Triangle to clone.

Returns: Phaser.Geom.Triangle - A new Triangle identical to the given one but separate from it.

Source: src/geom/triangle/Clone.js#L9
Since: 3.0.0


contains

<instance> contains(x, y)

Description:

Checks whether a given point lies within the triangle.

Parameters:

nametypeoptionaldescription
xnumberNoThe x coordinate of the point to check.
ynumberNoThe y coordinate of the point to check.

Returns: boolean - true if the coordinate pair is within the triangle, otherwise false.

Source: src/geom/triangle/Triangle.js#L122
Since: 3.0.0


Contains

<static> Contains(triangle, x, y)

Description:

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

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to check.
xnumberNoThe X coordinate of the point to check.
ynumberNoThe Y coordinate of the point to check.

Returns: boolean - true if the point is inside the Triangle, otherwise false.

Source: src/geom/triangle/Contains.js#L9
Since: 3.0.0


ContainsArray

<static> ContainsArray(triangle, points, [returnFirst], [out])

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:

nametypeoptionaldefaultdescription
trianglePhaser.Geom.TriangleNoThe triangle that the points are being checked in.
pointsArray.<Phaser.Math.Vector2>NoAn array of Vector2 objects to check if they are within the triangle.
returnFirstbooleanYesfalseIf true, return an array containing only the first point found that is within the triangle.
outarrayYesIf 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: Array.<Phaser.Math.Vector2> - 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.

Source: src/geom/triangle/ContainsArray.js#L7
Since: 3.0.0


ContainsPoint

<static> ContainsPoint(triangle, vec)

Description:

Tests if a triangle contains a point.

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe triangle.
vecPhaser.Math.Vector2NoThe Vector2 point to test if it's within the triangle.

Returns: boolean - true if the point is within the triangle, otherwise false.

Source: src/geom/triangle/ContainsPoint.js#L9
Since: 3.0.0


CopyFrom

<static> CopyFrom(source, dest)

Description:

Copy the values of one Triangle to a destination Triangle.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
sourcePhaser.Geom.TriangleNoThe source Triangle to copy the values from.
destPhaser.Geom.TriangleNoThe destination Triangle to copy the values to.

Returns: Phaser.Geom.Triangle - The destination Triangle.

Source: src/geom/triangle/CopyFrom.js#L7
Since: 3.0.0


Decompose

<static> Decompose(triangle, [out])

Description:

Decomposes a Triangle into an array of its points.

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to decompose.
outarrayYesAn array to store the points into.

Returns: array - 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.

Source: src/geom/triangle/Decompose.js#L7
Since: 3.0.0


Equals

<static> Equals(triangle, toCompare)

Description:

Returns true if two triangles have the same coordinates.

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe first triangle to check.
toComparePhaser.Geom.TriangleNoThe second triangle to check.

Returns: boolean - true if the two given triangles have the exact same coordinates, otherwise false.

Source: src/geom/triangle/Equals.js#L7
Since: 3.0.0


getLineA

<instance> getLineA([line])

Description:

Returns a Line object that corresponds to Line A of this Triangle, running from vertex 1 (x1, y1) to vertex 2 (x2, y2).

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineYesA Line object to set the results in. If undefined a new Line will be created.

Returns: Phaser.Geom.Line - A Line object that corresponds to line A of this Triangle.

Source: src/geom/triangle/Triangle.js#L228
Since: 3.0.0


getLineB

<instance> getLineB([line])

Description:

Returns a Line object that corresponds to Line B of this Triangle, running from vertex 2 (x2, y2) to vertex 3 (x3, y3).

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineYesA Line object to set the results in. If undefined a new Line will be created.

Returns: Phaser.Geom.Line - A Line object that corresponds to line B of this Triangle.

Source: src/geom/triangle/Triangle.js#L249
Since: 3.0.0


getLineC

<instance> getLineC([line])

Description:

Returns a Line object that corresponds to Line C of this Triangle, running from vertex 3 (x3, y3) back to vertex 1 (x1, y1), closing the shape.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
linePhaser.Geom.LineYesA Line object to set the results in. If undefined a new Line will be created.

Returns: Phaser.Geom.Line - A Line object that corresponds to line C of this Triangle.

Source: src/geom/triangle/Triangle.js#L270
Since: 3.0.0


getPoint

<instance> getPoint(position, [output])

Description:

Returns a point at a given normalized position along the perimeter of the triangle.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
positionnumberNoPosition as float within 0 and 1. 0 equals the first point.
outputPhaser.Math.Vector2YesOptional Vector2 point that the calculated point will be written to.

Returns: Phaser.Math.Vector2 - Calculated Vector2 that represents the requested position. It is the same as output when this parameter has been given.

Source: src/geom/triangle/Triangle.js#L138
Since: 3.0.0


GetPoint

<static> GetPoint(triangle, position, [out])

Description:

Returns a point along the perimeter of a Triangle as a Vector2, based on a normalized position value.

The position parameter is a value between 0 and 1, where 0 and 1 both map to the start of line A (the first vertex). The point is calculated by traversing the triangle's perimeter in order: line A, then line B, then line C.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to get the point on its perimeter from.
positionnumberNoA normalized value between 0 and 1 representing a position along the triangle's perimeter. Both 0 and 1 return the start of line A.
outPhaser.Math.Vector2YesAn optional Vector2 point to store the result in. If not given, a new Vector2 will be created.

Returns: Phaser.Math.Vector2 - A Vector2 point containing the coordinates of the position on the triangle's perimeter.

Source: src/geom/triangle/GetPoint.js#L10
Since: 3.0.0


getPoints

<instance> getPoints(quantity, [stepRate], [output])

Description:

Calculates a list of evenly distributed points along the perimeter of the triangle. Either pass the number of points to generate (quantity) or the distance between consecutive points (stepRate).

Tags:

  • generic

Parameters:

nametypeoptionaldescription
quantitynumberNoNumber of points to be generated. Can be falsey when stepRate should be used. All points have the same distance along the triangle.
stepRatenumberYesDistance between two points. Will only be used when quantity is falsey.
outputArray.<Phaser.Math.Vector2>YesOptional array of Vector2 points for writing the calculated points into. Otherwise a new array will be created.

Returns: Array.<Phaser.Math.Vector2> - Returns a list of calculated Vector2 instances or the filled array passed as parameter output.

Source: src/geom/triangle/Triangle.js#L156
Since: 3.0.0


GetPoints

<static> GetPoints(triangle, quantity, stepRate, [out])

Description:

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

The points are placed along the triangle's three edges in order (A, B, then C), with their positions calculated proportionally based on the total perimeter length.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to get the points from.
quantitynumberNoThe number of evenly spaced points to return. Set to 0 to return an arbitrary number of points based on the stepRate.
stepRatenumberNoIf quantity is 0, the distance in pixels between each returned point along the perimeter.
outArray.<Phaser.Math.Vector2>YesAn array to which the points should be appended.

Returns: Array.<Phaser.Math.Vector2> - The modified out array, or a new array if none was provided.

Source: src/geom/triangle/GetPoints.js#L10
Since: 3.0.0


getRandomPoint

<instance> getRandomPoint([vec])

Description:

Returns a random point from within the area of the triangle.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
vecPhaser.Math.Vector2YesOptional Vector2 point that will be modified. Otherwise a new one will be created.

Returns: Phaser.Math.Vector2 - Random Vector2. When parameter vec has been provided it will be returned.

Source: src/geom/triangle/Triangle.js#L175
Since: 3.0.0


InCenter

<static> InCenter(triangle, [out])

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.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to find the incenter of.
outPhaser.Math.Vector2YesAn optional Vector2 point in which to store the coordinates.

Returns: Phaser.Math.Vector2 - The incenter of the triangle in a Vector2.

Source: src/geom/triangle/InCenter.js#L21
Since: 3.0.0


Offset

<static> Offset(triangle, x, y)

Description:

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

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to move.
xnumberNoThe horizontal offset (distance) by which to move each point. Can be positive or negative.
ynumberNoThe vertical offset (distance) by which to move each point. Can be positive or negative.

Returns: Phaser.Geom.Triangle - The modified Triangle.

Source: src/geom/triangle/Offset.js#L7
Since: 3.0.0


Perimeter

<static> Perimeter(triangle)

Description:

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

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to get the length from.

Returns: number - The length of the Triangle.

Source: src/geom/triangle/Perimeter.js#L9
Since: 3.0.0


Random

<static> Random(triangle, [out])

Description:

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

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to get a random point from.
outPhaser.Math.Vector2YesThe Vector2 point object to store the position in. If not given, a new Vector2 instance is created.

Returns: Phaser.Math.Vector2 - A Vector2 point object holding the coordinates of a random position within the Triangle.

Source: src/geom/triangle/Random.js#L9
Since: 3.0.0


Rotate

<static> Rotate(triangle, angle)

Description:

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

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to rotate.
anglenumberNoThe angle by which to rotate the Triangle, in radians.

Returns: Phaser.Geom.Triangle - The rotated Triangle.

Source: src/geom/triangle/Rotate.js#L10
Since: 3.0.0


RotateAroundPoint

<static> RotateAroundPoint(triangle, point, angle)

Description:

Rotates a Triangle at a certain angle about a given Vector2 point.

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to rotate.
pointPhaser.Math.Vector2NoThe Vector2 point to rotate the Triangle around.
anglenumberNoThe angle by which to rotate the Triangle, in radians.

Returns: Phaser.Geom.Triangle - The rotated Triangle.

Source: src/geom/triangle/RotateAroundPoint.js#L9
Since: 3.0.0


RotateAroundXY

<static> RotateAroundXY(triangle, x, y, angle)

Description:

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

Tags:

  • generic

Parameters:

nametypeoptionaldescription
trianglePhaser.Geom.TriangleNoThe Triangle to rotate.
xnumberNoThe X coordinate of the point to rotate the Triangle about.
ynumberNoThe Y coordinate of the point to rotate the Triangle about.
anglenumberNoThe angle by which to rotate the Triangle, in radians.

Returns: Phaser.Geom.Triangle - The rotated Triangle.

Source: src/geom/triangle/RotateAroundXY.js#L7
Since: 3.0.0


setTo

<instance> setTo([x1], [y1], [x2], [y2], [x3], [y3])

Description:

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

Parameters:

nametypeoptionaldefaultdescription
x1numberYes0x coordinate of the first point.
y1numberYes0y coordinate of the first point.
x2numberYes0x coordinate of the second point.
y2numberYes0y coordinate of the second point.
x3numberYes0x coordinate of the third point.
y3numberYes0y coordinate of the third point.

Returns: Phaser.Geom.Triangle - This Triangle object.

Source: src/geom/triangle/Triangle.js#L192
Since: 3.0.0


Public Members

bottom

bottom: number

Description:

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

Source: src/geom/triangle/Triangle.js#L405
Since: 3.0.0


left

left: number

Description:

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

Source: src/geom/triangle/Triangle.js#L291
Since: 3.0.0


right: number

Description:

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

Source: src/geom/triangle/Triangle.js#L329
Since: 3.0.0


top

top: number

Description:

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

Source: src/geom/triangle/Triangle.js#L367
Since: 3.0.0


type

type: number

Description:

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

Source: src/geom/triangle/Triangle.js#L50
Since: 3.19.0


x1

x1: number

Description:

x coordinate of the first point.

Source: src/geom/triangle/Triangle.js#L61
Since: 3.0.0


x2

x2: number

Description:

x coordinate of the second point.

Source: src/geom/triangle/Triangle.js#L81
Since: 3.0.0


x3

x3: number

Description:

x coordinate of the third point.

Source: src/geom/triangle/Triangle.js#L101
Since: 3.0.0


y1

y1: number

Description:

y coordinate of the first point.

Source: src/geom/triangle/Triangle.js#L71
Since: 3.0.0


y2

y2: number

Description:

y coordinate of the second point.

Source: src/geom/triangle/Triangle.js#L91
Since: 3.0.0


y3

y3: number

Description:

y coordinate of the third point.

Source: src/geom/triangle/Triangle.js#L111
Since: 3.0.0