public List <HitInfo> IntersectTriangle(Triangle triangle)
{
List <HitInfo> res = new List <HitInfo>();
for (int i = 0; i < vertices.Count; i++)
{
LineSegment2D l1 = new LineSegment2D(V2(vertices[i]), V2(vertices[(i + 1) % vertices.Count]));
for (int j = 0; j < 3; j++)
{
LineSegment2D l2 = new LineSegment2D(V2(triangle.Vertex(j)), V2(triangle.Vertex((j + 1) % 3)));
Vector2D point;
if (l1.LineIntersect(l2, out point))
{
double distance = (point - V2(vertices[i])).magnitude;
double segmentLength = l1.Length();
//if (l1.DoLinesIntersect(l2)){
double normalizedDistance = distance / segmentLength;
Vector3D point3D = Vector3D.Lerp(new Vector3D(vertices[i]), new Vector3D(vertices[(i + 1) % vertices.Count]), normalizedDistance);
res.Add(new HitInfo(i, j, point3D.ToVector3()));
}
/*Vector2 intersectionPoint;
* if (LineLineIntersection(from,to, tFrom, tTo,out intersectionPoint)){
* float normalizedDistance = Vector3.Dot(to-from, tTo-tFrom);
* Vector3 point3D = Vector3.Lerp (vertices[i], vertices[(i+1)%vertices.Count], normalizedDistance);
* res.Add(new HitInfo(i,j,point3D));
* }*/
}
}
return(res);
}