The X coord of the first point.
The Y coord of the first point.
The X coord of the second point.
The Y coord of the second point.
The X coord of the third point.
The Y coord of the third point.
Readonly
typeThe type of the object, mainly used to avoid instanceof
checks
// Check shape type
const shape = new Triangle(0, 0, 100, 0, 50, 100);
console.log(shape.type); // 'triangle'
// Use in type guards
if (shape.type === 'triangle') {
console.log(shape.x2, shape.y2);
}
SHAPE_PRIMITIVE For all shape types
The X coordinate of the first point of the triangle.
The X coordinate of the second point of the triangle.
The X coordinate of the third point of the triangle.
The Y coordinate of the first point of the triangle.
The Y coordinate of the second point of the triangle.
The Y coordinate of the third point of the triangle.
Creates a clone of this Triangle
A copy of the triangle
// Basic cloning
const original = new Triangle(0, 0, 100, 0, 50, 100);
const copy = original.clone();
// Clone and modify
const modified = original.clone();
modified.x3 = 75;
modified.y3 = 150;
// Verify independence
console.log(original.y3); // 100
console.log(modified.y3); // 150
Checks whether the x and y coordinates given are contained within this triangle
The X coordinate of the point to test
The Y coordinate of the point to test
Whether the x/y coordinates are within this Triangle
// Basic containment check
const triangle = new Triangle(0, 0, 100, 0, 50, 100);
const isInside = triangle.contains(25, 25); // true
Copies another triangle to this one.
The triangle to copy from
Returns itself
// Basic copying
const source = new Triangle(0, 0, 100, 0, 50, 100);
const target = new Triangle();
target.copyFrom(source);
// Chain with other operations
const triangle = new Triangle()
.copyFrom(source)
.getBounds(rect);
Copies this triangle to another one.
The triangle to copy to
Returns given parameter
// Basic copying
const source = new Triangle(0, 0, 100, 0, 50, 100);
const target = new Triangle();
source.copyTo(target);
// Chain with other operations
const result = source
.copyTo(new Triangle())
.getBounds();
Returns the framing rectangle of the triangle as a Rectangle object
Optional
out: RectangleOptional rectangle to store the result
The framing rectangle
// Basic bounds calculation
const triangle = new Triangle(0, 0, 100, 0, 50, 100);
const bounds = triangle.getBounds();
// bounds: x=0, y=0, width=100, height=100
// Reuse existing rectangle
const rect = new Rectangle();
triangle.getBounds(rect);
Checks whether the x and y coordinates given are contained within this triangle including the stroke.
The X coordinate of the point to test
The Y coordinate of the point to test
The width of the line to check
The alignment of the stroke (1 = inner, 0.5 = centered, 0 = outer)
Whether the x/y coordinates are within this triangle's stroke
// Basic stroke check
const triangle = new Triangle(0, 0, 100, 0, 50, 100);
const isOnStroke = triangle.strokeContains(25, 25, 4); // 4px line width
// Check with different alignments
const innerStroke = triangle.strokeContains(25, 25, 4, 1); // Inside
const centerStroke = triangle.strokeContains(25, 25, 4, 0.5); // Centered
const outerStroke = triangle.strokeContains(25, 25, 4, 0); // Outside
A class to define a shape of a triangle via user defined coordinates.
Used for creating triangular shapes and hit areas with three points (x,y), (x2,y2), (x3,y3). Points are stored in counter-clockwise order.
Example
See