/// <summary> /// Calculates the intersection of this line with the given line. /// </summary> /// <param name="other">Other.</param> /// <param name="finiteThis">If set to <c>true</c> this line is finite.</param> /// <param name="finiteOther">If set to <c>true</c> the other line is finite.</param> /// <param name="intersection">Intersection.</param> public bool Intersects(Line2d other, bool finiteThis, bool finiteOther, out Vector2 intersection) { intersection = Vector2.zero; Vector2 b = m_PointB - m_PointA; Vector2 d = other.pointB - other.pointA; float bDotDPerp = b.x * d.y - b.y * d.x; // If bDotDPerp is 0 it means the lines are parallel. if (HydraMathUtils.Approximately(bDotDPerp, 0.0f)) return false; Vector2 c = other.pointA - m_PointA; float t = (c.x * d.y - c.y * d.x) / bDotDPerp; if (finiteThis && (t < 0.0f || t > 1.0f)) return false; float u = (c.x * b.y - c.y * b.x) / bDotDPerp; if (finiteOther && (u < 0.0f || u > 1.0f)) return false; intersection = m_PointA + t * b; return true; }
/// <summary> /// Returns the number of intersections with the given rectangle. /// </summary> /// <param name="rect">Rect.</param> /// <param name="finiteThis">If set to <c>true</c> this line is finite.</param> /// <param name="intersections">Intersections.</param> public int Intersects(Rect rect, bool finiteThis, ref Vector2[] intersections) { int output = 0; Array.Resize(ref intersections, 4); Vector2 topLeft = rect.GetTopLeft(); Vector2 topRight = rect.GetTopRight(); Vector2 bottomLeft = rect.GetBottomLeft(); Vector2 bottomRight = rect.GetBottomRight(); Line2d top = new Line2d(topLeft, topRight); Line2d bottom = new Line2d(bottomLeft, bottomRight); Line2d left = new Line2d(topLeft, bottomLeft); Line2d right = new Line2d(topRight, bottomRight); Vector2 intersection; if (Intersects(top, finiteThis, true, out intersection)) { intersections[output] = intersection; output++; } if (Intersects(bottom, finiteThis, true, out intersection)) { intersections[output] = intersection; output++; } if (Intersects(left, finiteThis, true, out intersection)) { intersections[output] = intersection; output++; } if (Intersects(right, finiteThis, true, out intersection)) { intersections[output] = intersection; output++; } return(output); }
/// <summary> /// Returns true if the line intersects the given line. Both lines are /// considered finite. /// </summary> /// <param name="other">Other.</param> /// <param name="intersection">Intersection.</param> public bool Intersects(Line2d other, out Vector2 intersection) { return(Intersects(other, true, true, out intersection)); }
/// <summary> /// Returns true if the line intersects the given line. Both lines are /// considered finite. /// </summary> /// <param name="other">Other.</param> /// <param name="intersection">Intersection.</param> public bool Intersects(Line2d other, out Vector2 intersection) { return Intersects(other, true, true, out intersection); }
/// <summary> /// Returns the number of intersections with the given rectangle. /// </summary> /// <param name="rect">Rect.</param> /// <param name="finiteThis">If set to <c>true</c> this line is finite.</param> /// <param name="intersections">Intersections.</param> public int Intersects(Rect rect, bool finiteThis, ref Vector2[] intersections) { int output = 0; Array.Resize(ref intersections, 4); Vector2 topLeft = rect.GetTopLeft(); Vector2 topRight = rect.GetTopRight(); Vector2 bottomLeft = rect.GetBottomLeft(); Vector2 bottomRight = rect.GetBottomRight(); Line2d top = new Line2d(topLeft, topRight); Line2d bottom = new Line2d(bottomLeft, bottomRight); Line2d left = new Line2d(topLeft, bottomLeft); Line2d right = new Line2d(topRight, bottomRight); Vector2 intersection; if (Intersects(top, finiteThis, true, out intersection)) { intersections[output] = intersection; output++; } if (Intersects(bottom, finiteThis, true, out intersection)) { intersections[output] = intersection; output++; } if (Intersects(left, finiteThis, true, out intersection)) { intersections[output] = intersection; output++; } if (Intersects(right, finiteThis, true, out intersection)) { intersections[output] = intersection; output++; } return output; }
/// <summary> /// Draws the line. /// </summary> /// <param name="line">Line.</param> /// <param name="color">Color.</param> /// <param name="duration">Duration.</param> public static void DrawLine(Line2d line, Color color, float duration) { Debug.DrawLine(line.pointA, line.pointB, color, duration); }