public class Triangle extends Polygon implements Convex, Wound, Shape, Transformable, DataContainer
Convex
Shape
.
A Triangle
must have one vertex which is not colinear with the other two.
This class is provided to enhance performance of some of the methods contained in
the Convex
and Shape
interfaces.
Constructor and Description |
---|
Triangle(Vector2 point1,
Vector2 point2,
Vector2 point3)
Full constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(Vector2 point,
Transform transform)
Returns true if the point is inside the
Triangle . |
java.lang.String |
toString() |
createAABB, createMass, getAxes, getFarthestFeature, getFarthestPoint, getFoci, getNormalIterator, getNormals, getRadius, getVertexIterator, getVertices, project, rotate, translate
contains, createAABB, getCenter, getId, getRadius, getUserData, hashCode, project, rotate, rotate, rotateAboutCenter, setUserData, translate
getAxes, getFarthestFeature, getFarthestPoint, getFoci
getNormalIterator, getNormals, getVertexIterator, getVertices
contains, createAABB, createAABB, createMass, getCenter, getId, getRadius, getRadius, project, project, rotateAboutCenter
public Triangle(Vector2 point1, Vector2 point2, Vector2 point3)
Creates a new triangle using the given points. The center will be the area weighted center of the points.
A triangle must have 3 non-null points of which one is not colinear with the other two.
point1
- the first pointpoint2
- the second pointpoint3
- the third pointjava.lang.NullPointerException
- if point1, point2, or point3 is nulljava.lang.IllegalArgumentException
- if point1, point2, and point3 contain coincident points or has clockwise windingpublic boolean contains(Vector2 point, Transform transform)
Triangle
.
The equation of a plane is:
N · (P - A) = 0
Where A is any point on the plane.Vector2
s), we will choose Vab and Vac.
Vac = C - A Vab = B - A
Where A, B, and C are the vertices of theTriangle
.P = A + u * Vac + v * Vab
Simplifing P - AVpa = u * Vac + v * Vab
We still need another equation to solve for u and v:Vpa · Vac = (u * Vac + v * Vab) · Vac
Dot the equation by Vab to get the otherVpa · Vab = (u * Vac + v * Vab) · Vab
Distribute out both equationsVpa · Vac = u * Vac · Vac + v * Vab · Vac Vpa · Vab = u * Vac · Vab + v * Vab · Vab
Solving the first equation for u:u = (Vpa · Vac - v * Vab · Vac) / (Vac · Vac)
Substitute one into the other:Vpa · Vab = (Vpa · Vac - v * Vab · Vac) / (Vac · Vac) * Vac · Vab + v * Vab · Vab Vpa · Vab = (Vpa · Vac / Vac · Vac) * Vac · Vab - v * (Vab · Vac / Vac · Vac) * Vac · Vab + v * Vab · Vab Vpa · Vab = (Vpa · Vac / Vac · Vac) * Vac · Vab + v * (Vab · Vab - (Vab · Vac / Vac · Vac) * Vac · Vab) v = (Vpa · Vab - (Vpa · Vac / Vac · Vac) * Vac · Vab) / (Vab · Vab - (Vab · Vac / Vac · Vac) * Vac · Vab)
Which reduces to:v = ((Vpa · Vab) * (Vac · Vac) - (Vpa · Vac) * (Vac · Vab)) / ((Vab · Vab) * (Vac · Vac) - (Vab · Vac) * (Vac · Vab))
Once v is obtained use either equation to obtain u:u = (v * Vab · Vab - Vpa · Vab) / Vac · Vab
We know that the point is inside theTriangle
if u and v are greater than
zero and u + v is less than one.