- Object
-
- AbstractShape
-
- Polygon
-
- Triangle
-
- All Implemented Interfaces:
DataContainer
,Convex
,Rotatable
,Shape
,Transformable
,Translatable
,Wound
public class Triangle extends Polygon implements Convex, Wound, Shape, Transformable, DataContainer
Implementation of a TriangleConvex
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
andShape
interfaces.- Since:
- 1.0.0
- Version:
- 3.2.0
- Author:
- William Bittle
-
-
Field Summary
-
Fields inherited from class AbstractShape
center, radius, userData
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(Vector2 point, Transform transform)
Returns true if the point is inside theTriangle
.String
toString()
-
Methods inherited from class Polygon
computeAABB, createMass, getAxes, getFarthestFeature, getFarthestPoint, getFoci, getNormalIterator, getNormals, getRadius, getVertexIterator, getVertices, project, rotate, translate
-
Methods inherited from class AbstractShape
computeAABB, contains, createAABB, createAABB, getCenter, getRadius, getUserData, project, rotate, rotate, rotate, rotate, rotate, rotateAboutCenter, setUserData, translate
-
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface Convex
getAxes, getFarthestFeature, getFarthestPoint, getFoci
-
Methods inherited from interface DataContainer
getUserData, setUserData
-
Methods inherited from interface Shape
computeAABB, computeAABB, contains, createAABB, createAABB, createMass, getCenter, getRadius, getRadius, project, project, rotateAboutCenter
-
Methods inherited from interface Translatable
translate, translate
-
Methods inherited from interface Wound
getNormalIterator, getNormals, getVertexIterator, getVertices
-
-
-
-
Constructor Detail
-
Triangle
public Triangle(Vector2 point1, Vector2 point2, Vector2 point3)
Full constructor.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.
- Parameters:
point1
- the first pointpoint2
- the second pointpoint3
- the third point- Throws:
NullPointerException
- if point1, point2, or point3 is nullIllegalArgumentException
- if point1, point2, and point3 contain coincident points or has clockwise winding
-
-
Method Detail
-
contains
public boolean contains(Vector2 point, Transform transform)
Returns true if the point is inside theTriangle
.The equation of a plane is:
N · (P - A) = 0
Where A is any point on the plane.
Create two axes (Vector2
s), we will choose Vab and Vac.Vac = C - A Vab = B - A
Where A, B, and C are the vertices of theTriangle
.
From this we can say that you can get to any point on the plane by going some u distance on Vac and some v distance on Vab where A is the origin.P = A + u * Vac + v * Vab
Simplifing P - AVpa = u * Vac + v * Vab
We still need another equation to solve for u and v:
Dot the equation by Vac to getVpa · 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.
-
-