Geometry Utils
GeometryUtil provides helpers for geometric calculations and coordinate transformations so layout logic stays simple even when scenes mix coordinate spaces.
The example below demonstrates a typical scenario when each scatter symbol from the slot machine is cloned and visually "flies" toward the scatter-collection panel. The GeometryUtil.convertPoint() calls translate both the scatter’s local position and the panel’s position into the shared coordinate space of the effectsWrapper, ensuring the animation moves along the correct global path even if elements belong to different containers. The distance between points defines animation duration, and the action chain handles spawning, tweening, and cleanup of the temporary clone.
const effectsWrapper:Container; // effects container
const slotMachine:SlotMachine; // slot machine with scatter symbols
const scattersCollectPanel:Container; // panel with counter to collect scatters
const scattersList = slotMachine.obtainSymbols([ SymbolType.SCATTER ]);
const moveSpeed = 200;
for (const scatter of scattersList) {
const clone = scatter.clone();
const sourcePosition = GeometryUtil.convertPoint(clone, effectsWrapper);
const targetPosition = GeometryUtil.convertPoint(scattersCollectPanel, effectsWrapper);
const distance = GeometryUtil.distance(sourcePosition, targetPosition);
const duration = distance / moveSpeed;
Actions.stage.addObject(effectsWrapper, clone)
.stage.changePoint(clone.position, sourcePosition)
.tweens.move(clone, duration, targetPosition)
.stage.removeObject(clone)
.utils.releaseObject(clone)
.start();
}
distance
Calculates the Euclidean distance between two points.
static distance(point_1:IPointData, point_2:IPointData):number
const point1 = { x: 2, y: 2 };
const point2 = { x: 5, y: 10 };
const distance = GeometryUtil.distance(point1, point2);
console.log(distance);
// Output: 8.54
containsRectangle
Checks whether one rectangle fully contains another.
static containsRectangle(rect_1:Rectangle, rect_2:Rectangle):boolean
const rect1 = new Rectangle(0, 0, 200, 200);
const rect2 = new Rectangle(50, 50, 50, 50);
const rect3 = new Rectangle(-50, -50, 100, 100);
const isContainsRect2 = GeometryUtil.containsRectangle(rect1, rect2);
const isContainsRect3 = GeometryUtil.containsRectangle(rect1, rect3);
console.log(isContainsRect2, isContainsRect3);
// Output: true false
convertPoint
Converts a point from one display object's coordinate space to another's.
static convertPoint(source:DisplayObject, target:DisplayObject, point?:IPointData):IPointData
const container1 = new Container();
container1.position.set(200, 200);
container1.rotation = Math.PI / 4;
const container2 = new Container();
container2.position.set(300, 300);
const sprite = new Sprite();
sprite.position.set(50, 50);
container1.addChild(sprite);
const convertedPoint = GeometryUtil.convertPoint(sprite, container2);
console.log(convertedPoint);
// Output: { x: -100, y: -29.3 }
approxEquals
Checks if two points are approximately equal within an epsilon threshold.
static approxEquals(point_1:IPointData, point_2:IPointData, epsilon?:number):boolean
const expectedPosition = { x: 10, y: 10 };
const actualPosition = { x: 10.0002, y: 9.9999 };
const arePositionsEquals = GeometryUtil.approxEquals(expectedPosition, actualPosition, 0.001);
console.log(arePositionsEquals);
// Output: true
interpolatePoint
Linearly interpolates between two points.
static interpolatePoint(point_1:IPointData, point_2:IPointData, value:number):Point
const startPoint = { x: 0, y: 0 };
const endPoint = { x: 100, y: 100 };
const point1 = GeometryUtil.interpolatePoint(startPoint, endPoint, 0.5);
console.log(point1);
// Output: { x: 50, y: 50 }
const point2 = GeometryUtil.interpolatePoint(startPoint, endPoint, 0.25);
console.log(point2);
// Output: { x: 25, y: 25 }
polarPoint
Creates a point using polar coordinates (length and angle).
static polarPoint(length:number, angle:number):Point
const rightPoint = GeometryUtil.polarPoint(100, 0);
console.log(rightPoint);
// Output: { x: 100, y: 0 }
const diagonalPoint = GeometryUtil.polarPoint(100, Math.PI / 4);
console.log(diagonalPoint);
// Output: { x: 70.7, y: 70.7 }
const upPoint = GeometryUtil.polarPoint(100, -Math.PI / 2);
console.log(upPoint);
// Output: { x: 0, y: -100 }
polygonLength
Calculates the perimeter length of a polygon.
static polygonLength(points:IPointData[]):number
const trianglePoints = [
{ x: 0, y: 0 },
{ x: 0, y: 3 },
{ x: 4, y: 0 },
{ x: 0, y: 0 }
];
const length = GeometryUtil.polygonLength(trianglePoints);
console.log(length);
// Output: 12
vectorLength
Returns the length of a vector represented by a point.
static vectorLength(point:IPointData):number
const velocity = { x: 3, y: 4 };
const speed = GeometryUtil.vectorLength(velocity);
console.log(speed);
// Output: 5