Last active
October 22, 2022 07:09
-
-
Save atcarter714/6f0d2d467ddcfccc3bc09bd5056cb2cd to your computer and use it in GitHub Desktop.
Some useful C# methods for various mathematical purposes (such as 2D/3D math and complex operations)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Determines if a Vector2 point is within a triangle | |
/// </summary> | |
/// <param name="point">Point to check</param> | |
/// <param name="p0">Triangle vertex position</param> | |
/// <param name="p1">Triangle vertex position</param> | |
/// <param name="p2">Triangle vertex position</param> | |
/// <returns>True if point is inside triangle, otherwise false</returns> | |
/// <remarks> | |
/// Uses barycentric coordinate computation | |
/// </remarks> | |
public static bool PointInTriangle( Vector2 point, Vector2 p0, Vector2 p1, Vector2 p2 ) | |
{ | |
var s = (p0.x - p2.x) * (point.y - p2.y) - (p0.y - p2.y) * (point.x - p2.x); | |
var t = (p1.x - p0.x) * (point.y - p0.y) - (p1.y - p0.y) * (point.x - p0.x); | |
if ( (s < 0) != (t < 0) && s != 0 && t != 0 ) return false; | |
var d = (p2.x - p1.x) * (point.y - p1.y) - | |
(p2.y - p1.y) * (point.x - p1.x); | |
return d == 0 || (d < 0) == (s + t <= 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will share any clever math tricks for C#/game dev I think of here ...