Namespace: maths

maths

The maths folder contains utility classes and functions for mathematical operations used throughout the project. This includes constants such as conversion factors for radians and degrees, as well as shapes such as polygons, rectangles, circles, ellipses, triangles, and rounded rectangles.

import { RAD_TO_DEG, Circle } from 'pixi.js';

// Convert 180 degrees to radians
const radians = 180 * RAD_TO_DEG;

// test if a point is inside a circle
const isPointInCircle = new Circle(0, 0, 10).contains(0, 0); // true

Classes

Circle
Ellipse
Matrix
ObservablePoint
Point
Polygon
Rectangle
RoundedRectangle
Triangle

Interface Definitions

Observer

Observer used to listen for observable point changes.

Properties:
Name Type Description
_onUpdate (point: T) => void

Callback to call when the point has updated.

PointData

Common interface for points. Both Point and ObservablePoint implement it

Properties:
Name Type Description
x number

X coord

y number

Y coord

PointLike

Common interface for points. Both Point and ObservablePoint implement it

Properties:
Name Type Description
copyFrom (p: PointData) => this

Copies x and y from the given point

copyTo (p: T) => T

Copies x and y into the given point

equals (p: PointData) => boolean

Returns true if the given point is equal to this point

set (x: number, y: number) => void

Sets the point to a new x and y position. If y is omitted, both x and y will be set to x.

ShapePrimitive

A basic object to define a Pixi shape.

Properties:
Name Type Description
type SHAPE_PRIMITIVE

The type of the object, mainly used to avoid instanceof checks

x number

The X coordinate of the shape

y number

The Y coordinate of the shape

Size

Defines a size with a width and a height.

Properties:
Name Type Description
height number

The height.

width number

The width.

Namespaces

groupD8

Members

DEG_TO_RAD number staticreadonly

Conversion factor for converting degrees to radians.

PI_2 number staticreadonly

Two Pi.

RAD_TO_DEG number staticreadonly

Conversion factor for converting radians to degrees.

Type Definitions

SHAPE_PRIMITIVE

Constants that identify shapes, mainly to prevent instanceof calls.

Methods

floatEqual (a, b) boolean

The idea of a relative epsilon comparison is to find the difference between the two numbers, and see if it is less than Math.EPSILON.

Name Type Description
a number

First floating number to compare.

b number

Second floating number to compare.

Returns:
Type Description
boolean Returns true if the difference between the values is less than Math.EPSILON; otherwise false.

floatEqual (a, b, epsilon) boolean

The idea of a relative epsilon comparison is to find the difference between the two numbers, and see if it is less than a given epsilon. A good epsilon would be the N% of the largest of the two values or Math.EPSILON.

Note: Only available with pixi.js/math-extras.

Name Type Description
a number

First floating number to compare.

b number

Second floating number to compare.

epsilon number

The epsilon to compare to. The larger the epsilon, the easier for the numbers to be considered equals.

Returns:
Type Description
boolean Returns true if the difference between the values is less than the given epsilon; otherwise false.

isPow2 (v) boolean

Checks if a number is a power of two.

Name Type Description
v number

input value

Returns:
Type Description
boolean true if value is power of two

lineIntersection (aStart, aEnd, bStart, bEnd) PointData

Computes the point where non-coincident and non-parallel Lines intersect. Coincident or parallel lines return a NaN point {x: NaN, y: NaN}. The intersection point may land outside the extents of the lines.

Note: Only available with pixi.js/math-extras.

Name Type Description
aStart PointData

First point of the first line.

aEnd PointData

Second point of the first line.

bStart PointData

First point of the second line.

bEnd PointData

Second point of the second line.

Returns:
Type Description
PointData The point where the lines intersect.

lineIntersection (aStart, aEnd, bStart, bEnd, outPoint) PointData

Computes the point where non-coincident and non-parallel Lines intersect. Coincident or parallel lines return a NaN point {x: NaN, y: NaN}. The intersection point may land outside the extents of the lines.

Note: Only available with pixi.js/math-extras.

Name Type Description
aStart PointData

First point of the first line.

aEnd PointData

Second point of the first line.

bStart PointData

First point of the second line.

bEnd PointData

Second point of the second line.

outPoint PointData

A Point-like object in which to store the value, optional (otherwise will create a new Point).

Returns:
Type Description
PointData The point where the lines intersect or a NaN Point.

log2 (v) number

Computes ceil of log base 2

Name Type Description
v number

input value

Returns:
Type Description
number logarithm base 2

nextPow2 (v) number

Rounds to next power of two.

Name Type Description
v number

input value

Returns:
Type Description
number
  • next rounded power of two

segmentIntersection (aStart, aEnd, bStart, bEnd) PointData

Computes the point where non-coincident and non-parallel segments intersect. Coincident, parallel or non-intersecting segments return a NaN point {x: NaN, y: NaN}. The intersection point must land inside the extents of the segments or return a NaN Point.

Note: Only available with pixi.js/math-extras.

Name Type Description
aStart PointData

Starting point of the first segment.

aEnd PointData

Ending point of the first segment.

bStart PointData

Starting point of the second segment.

bEnd PointData

Ending point of the second segment.

Returns:
Type Description
PointData The point where the segments intersect.

segmentIntersection (aStart, aEnd, bStart, bEnd, outPoint) PointData

Computes the point where non-coincident and non-parallel segments intersect. Coincident, parallel or non-intersecting segments return a NaN point {x: NaN, y: NaN}. The intersection point must land inside the extents of the segments or return a NaN Point.

Note: Only available with pixi.js/math-extras.

Name Type Description
aStart PointData

Starting point of the first segment.

aEnd PointData

Ending point of the first segment.

bStart PointData

Starting point of the second segment.

bEnd PointData

Ending point of the second segment.

outPoint PointData

A Point-like object in which to store the value, optional (otherwise will create a new Point).

Returns:
Type Description
PointData The point where the segments intersect or a NaN Point.