unity-atoms/Assets/UnityAtoms/Utils/Vector2Utils.cs

25 lines
882 B
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityAtoms.Utils
{
public static class Vector2Utils
{
// Find out if two lines intersect
public static bool IsIntersectingAlternative(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
{
float denominator = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y);
if (denominator != 0)
{
float u_a = ((p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x)) / denominator;
float u_b = ((p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x)) / denominator;
if (u_a >= 0 && u_a <= 1 && u_b >= 0 && u_b <= 1)
{
return true;
}
}
return false;
}
}
}