I was wondering if I draw two intersecting lines like in the picture below, how would I find the top and bottom angles of intersection of the two lines?
the angle is the same if you calculate the intersection or not:
/// <summary>
/// Angle between vectors from -179 to 180.
/// </summary>
/// <returns>From -179 to 180.</returns>
/// <param name="one">One.</param>
/// <param name="two">Two.</param>
public static float AngleBetweenVectors(Vector3 one, Vector3 two)
{
float a = one.sqrMagnitude;
float b = two.sqrMagnitude;
if (a > 0.0f && b > 0.0f)
{
float angle = Mathf.Acos(Vector3.Dot(one, two) / Mathf.Sqrt(a * b)) * 180.0f / Mathf.PI;
Vector3 cross = Vector3.Cross(one, two);
//float sign = Mathf.Min( Mathf.Min(cross.x, cross.y), cross.z );
float sign = cross.y;
if (sign < 0.0f)
return -angle;
else
return angle;
}
return 0.0f;
}
this function calculates the angle between two vectors on the x-z plane (no y component)
if you want to calculate the intersection, you need this function:
// Finds the intersection of two lines, or returns false.
// The lines are defined by (o1, p1) and (o2, p2).
public static bool LineIntersection(Vector3 one1, Vector3 two1, Vector3 one2, Vector3 two2, ref Vector3 point)
{
Vector3 seg1 = two1 - one1;
Vector3 seg2 = two2 - one2;
float cross = seg1.x*seg2.z - seg1.z*seg2.x;
if (Mathf.Abs(cross) < 1e-8)
return false;
Vector3 one12 = one2 - one1;
float t = (one12.x * seg2.z - one12.z * seg2.x)/cross;
point = one1 + seg1 * t;
return true;
}