Skip to content

Instantly share code, notes, and snippets.

@rgoupil
Last active August 21, 2020 16:21
Show Gist options
  • Save rgoupil/04b59be8ddb56c992f25e1489c61b310 to your computer and use it in GitHub Desktop.
Save rgoupil/04b59be8ddb56c992f25e1489c61b310 to your computer and use it in GitHub Desktop.
using Unity.Mathematics;
public static class math_ext
{
private static float2 vect2d(float2 p1, float2 p2)
{
float2 temp;
temp.x = (p2.x - p1.x);
temp.y = -(p2.y - p1.y);
return temp;
}
private static bool vectEquals(float2 a, float2 b)
{
return a.x == b.x && a.y == b.y;
}
public static bool PointIsInArea(float2 m, float2 A, float2 B, float2 C, float2 D)
{
if (vectEquals(A, B) && vectEquals(A, C) && vectEquals(A, D))
{
return vectEquals(A, m);
}
float2 AB = vect2d(A, B);
float C1 = -(AB.y * A.x + AB.x * A.y);
float D1 = (AB.y * m.x + AB.x * m.y) + C1;
float2 AD = vect2d(A, D);
float C2 = -(AD.y * A.x + AD.x * A.y);
float D2 = (AD.y * m.x + AD.x * m.y) + C2;
float2 BC = vect2d(B, C);
float C3 = -(BC.y * B.x + BC.x * B.y);
float D3 = (BC.y * m.x + BC.x * m.y) + C3;
float2 CD = vect2d(C, D);
float C4 = -(CD.y * C.x + CD.x * C.y);
float D4 = (CD.y * m.x + CD.x * m.y) + C4;
return 0 >= D1 && 0 >= D4 && 0 <= D2 && 0 >= D3;
}
}
@rgoupil
Copy link
Author

rgoupil commented May 17, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment