/// <summary> /// Try cross two segments. /// </summary> /// <param name="other"></param> /// <param name="point">Resulted point</param> /// <returns>True if then lines intersect</returns> public bool TryCrossPoint(Segment other, out Vector2Ext point) { var v1 = (other.To.X - other.From.X) * (From.Y - other.From.Y) - (other.To.Y - other.From.Y) * (From.X - other.From.X); var v2 = (other.To.X - other.From.X) * (To.Y - other.From.Y) - (other.To.Y - other.From.Y) * (To.X - other.From.X); var v3 = (To.X - From.X) * (other.From.Y - From.Y) - (To.Y - From.Y) * (other.From.X - From.X); var v4 = (To.X - From.X) * (other.To.Y - From.Y) - (To.Y - From.Y) * (other.To.X - From.X); if ((v1 * v2 >= 0) || (v3 * v4 >= 0)) { point = null; return false; } var x = -((From.X * To.Y - To.X * From.Y) * (other.To.X - other.From.X) - (other.From.X * other.To.Y - other.To.X * other.From.Y) * (To.X - From.X)) / ((From.Y - To.Y) * (other.To.X - other.From.X) - (other.From.Y - other.To.Y) * (To.X - From.X)); if (double.IsNaN(x)) { point = null; return false; } var y = ((other.From.Y - other.To.Y) * (-x) - (other.From.X * other.To.Y - other.To.X * other.From.Y)) / (other.To.X - other.From.X); if ((y - From.Y) * (To.X - From.X) - (x - From.X) * (To.Y - From.Y) > 0.01) { point = null; return false; } point = new Vector2Ext(x, y); return true; }
public Segment(Vector2Ext from, Vector2Ext to) { From = from; To = to; }