File-By-File
Triangle

Triangle

A polygon with 3 sides and 3 vertices. This class represents a triangle defined by three PointXY vertices. It provides methods for geometric operations such as finding the centroid, checking for point containment, collision detection, and transformations like rotation, shifting, scaling, and growing.

Key Concepts

  • Vertices: A triangle is defined by three PointXY objects (a, b, c) that represent its corners.
  • Lines: The sides of the triangle are represented by Line objects.
  • Centroid: The geometric center of the triangle, calculated as the intersection of its medians.
  • Immutability: Like other geometric objects in Pathfinder, Triangle objects are immutable. Operations that modify the triangle (e.g., rotate, shift) return new Triangle instances.

Methods

Triangle(PointXY a, PointXY b, PointXY c)

Constructs a new Triangle from three PointXY vertices. Throws an IllegalArgumentException if any two points are identical.

getCentroid(Line ab, Line bc, Line ca)

Static method. Calculates the centroid (geometric center) of a triangle given its three side Line segments. The centroid is the intersection of the medians of the triangle.

getClosestPoint(PointXY reference)

Returns the point on the perimeter of the triangle that is closest to the given reference point. If the reference point is inside the triangle, it returns the reference point itself.

isPointInShape(PointXY reference)

Checks if a given reference point is inside or on the boundary of the triangle. This method uses a ray-casting algorithm to determine containment.

doesCollideWith(Shape<?> shape)

Determines if this Triangle collides with another Shape. It checks if the closest point on this triangle to the other shape's center is inside the other shape.

getCenter()

Returns the PointXY representing the centroid of the triangle.

rotate(Angle rotation)

Rotates the triangle by the specified rotation angle around its own centroid. Returns a new Triangle instance.

rotate(Angle rotation, PointXY centerOfRotation)

Rotates the triangle by the specified rotation angle around a given centerOfRotation. Returns a new Triangle instance.

shift(double shiftX, double shiftY)

Shifts (translates) the triangle by the given shiftX along the X-axis and shiftY along the Y-axis. Returns a new Triangle instance.

moveTo(PointXY newCenter)

Moves the triangle so that its centroid is at the newCenter PointXY. Returns a new Triangle instance.

scale(double scale)

Scales the triangle by a scale factor relative to its centroid. Returns a new Triangle instance.

growBy(double growth)

Grows or shrinks the triangle by adding growth to its dimensions, effectively expanding or contracting it from its centroid. Returns a new Triangle instance.

getA()

Returns the first vertex of the triangle.

getB()

Returns the second vertex of the triangle.

getC()

Returns the third vertex of the triangle.

getAB()

Returns the Line segment representing the side between vertex A and vertex B.

getBC()

Returns the Line segment representing the side between vertex B and vertex C.

getCA()

Returns the Line segment representing the side between vertex C and vertex A.