namespace GeometriCS { /// /// Intersection between two two-dimensional lines. /// public class IntersectionLine2ToLine2d { /// /// Possible statuses the intersection can be. /// public enum Statuses { /// /// Intersection is valid. /// SUCCESS, /// /// The two lines are parallel. /// PARALLEL, /// /// The two lines are colinear and intersect on their whole length. /// COLINEAR, } /// /// First of the intersecting lines. /// public Line2d Line1 { get; } /// /// The second intersecting line. /// public Line2d Line2 { get; } /// /// Status of the intersection. /// public Statuses Status { get; } /// /// The point of the intersection between the two lines. /// public Vector2d? Result { get; } /// /// Constructor for Line2d to Line2d intersection. /// /// First of the intersecting lines. /// Second of the intersecting lines. public IntersectionLine2ToLine2d(Line2d line1, Line2d line2) { // Set the intersector references Line1 = line1; Line2 = line2; // Check if lines are parallel // TODO: Check what happens if coeff_B == 0 if(Utils.DoubleEquals(line1.Coeff_A / line1.Coeff_B, line2.Coeff_A / line2.Coeff_B)) { // Null result Result = null; // Check if lines are coincidental if (Utils.DoubleEquals(line1.Coeff_A / line2.Coeff_A, line1.Coeff_B / line2.Coeff_B) && Utils.DoubleEquals(line1.Coeff_A / line2.Coeff_A, line1.Coeff_C / line2.Coeff_C)) { Status = Statuses.COLINEAR; } else { Status = Statuses.PARALLEL; } return; } // Perform the intersection calculation double divisor = line1.Coeff_A * line2.Coeff_B - line2.Coeff_A * line1.Coeff_B; double x0 = (line1.Coeff_B * line2.Coeff_C - line2.Coeff_B * line1.Coeff_C) / divisor; double y0 = (line1.Coeff_C * line2.Coeff_A - line2.Coeff_C * line1.Coeff_A) / divisor; Result = new Vector2d(x0, y0); Status = Statuses.SUCCESS; } } }