Client LuaCsForBarotrauma
Triangle2D.cs
1 using Microsoft.Xna.Framework;
2 
3 namespace Barotrauma;
4 
5 readonly record struct Triangle2D(Vector2 A, Vector2 B, Vector2 C)
6 {
7  public bool Contains(Vector2 point)
8  {
9  // Get the half-plane that the point lands in, for each side of the triangle
10  int halfPlaneAb = MathUtils.VectorOrientation(A, B, point);
11  int halfPlaneBc = MathUtils.VectorOrientation(B, C, point);
12  int halfPlaneCa = MathUtils.VectorOrientation(C, A, point);
13 
14  // The intersection of three half-planes derived from the three sides of the triangle
15  // is the triangle itself, so check for the point being in those three half-planes
16  bool allNonNegative = halfPlaneAb >= 0 && halfPlaneBc >= 0 && halfPlaneCa >= 0;
17  bool allNonPositive = halfPlaneAb <= 0 && halfPlaneBc <= 0 && halfPlaneCa <= 0;
18 
19  return allNonNegative || allNonPositive;
20  }
21 }