namespace GeometriCS { /// /// Common utilities throughout the library. /// public static class Utils { /// /// Default precision for double equality comparison. /// private const double DOUBLEPRECISION = 1E-5; /// /// Is this number in the range (inclusive). /// /// Number to compare /// Lower end of the range. /// Higher end of the range. /// trueif number is in the range , false if it is outside. public static bool IsInRange(this double num, double lower, double higher) { return num >= lower && num <= higher; } /// /// Tests whether the difference between two doubles is within a given limit. /// /// First value to test. /// Second value to test. /// Maximum allowed difference. /// Do the values almost equal each other. public static bool DoubleEquals(double a, double b, double prec = DOUBLEPRECISION) => Math.Abs(a - b) <= prec; /// /// Returns the coefficients of a line defined by equation Ax + By + C = 0, /// given two points that lie on the line. /// /// First point on the line. /// Second point on the line. /// Touple (Coeff_A, Coeff_B, Coeff_C). public static (double A, double B, double C) Line2CoefficientsFromTwoPoints(Vector2d pt1, Vector2d pt2) { double coeff_A = pt2.Y - pt1.Y; double coeff_B = pt1.X - pt2.X; double coeff_C = (pt1.Y * (pt2.X - pt1.X)) - (pt1.X * (pt2.Y - pt1.Y)); return (coeff_A, coeff_B, coeff_C); } } }