Example #1
0
        /// <summary> Calculates the point of intersection. Returns NaN when the specified lines are parallel. </summary>
        protected static Point FindExtrapolatedIntersection(Line line1, Line line2)
        {
            if (line1.IsParallelTo(line2))
            {
                return(new Point(float.NaN, float.NaN));
            }

            var x = (line2.LinearAddend - line1.LinearAddend) / (line1.LinearCoefficient - line2.LinearCoefficient);

            Contract.Assert(FloatingTypeEqualityComparisonHelper.EqualByTolerance(line1.GetYValueAt(x), line2.GetYValueAt(x)), "bug in line above");
            return(new Point(x, line1.GetYValueAt(x)));
        }
Example #2
0
        /// <summary> Gets the linear coefficient and linear addend, respectively, for a line intersecting the two specified points. </summary>
        private static Tuple <double, double> GetLinearCoefficientAndAddend(Point a, Point b)
        {
            double linearCoefficient, linearAddend;

            if (FloatingTypeEqualityComparisonHelper.EqualByTolerance(a.X, b.X))
            {
                linearCoefficient = a.Y < b.Y ? float.NegativeInfinity : float.PositiveInfinity;
                linearAddend      = float.NaN;
            }
            else
            {
                linearCoefficient = (b.Y - a.Y) / (b.X - a.X);
                linearAddend      = a.Y - a.X * linearCoefficient;
            }
            return(new Tuple <double, double>(linearCoefficient, linearAddend));
        }
Example #3
0
 /// <summary> Returns whether this line contains the specified point. </summary>
 public virtual bool Contains(Point point)
 {
     return(FloatingTypeEqualityComparisonHelper.EqualByTolerance(LinearAddend + LinearCoefficient * point.X, point.Y));
 }
Example #4
0
 /// <summary> Gets whether the specified line is parallel or antiparallel to this line. </summary>
 public bool IsParallelTo(Line other)
 {
     return(FloatingTypeEqualityComparisonHelper.EqualByTolerance(Math.Abs(other.LinearCoefficient), Math.Abs(this.LinearCoefficient)));
 }
Example #5
0
 /// <summary> Determines whether the specified line is equal to the current. </summary>
 public virtual bool Equals(Line other)
 {
     return(FloatingTypeEqualityComparisonHelper.EqualByTolerance(this.LinearCoefficient, other.LinearCoefficient) &&
            FloatingTypeEqualityComparisonHelper.EqualByTolerance(this.LinearAddend, other.LinearAddend));
 }
Example #6
0
 /// <summary> Returns whether this line segment contains the specified point. </summary>
 public override bool Contains(Point point)
 {
     return(base.Contains(point) &&          //the base case returns whether the point lies on (the extrapolation of) this line segment
            FloatingTypeEqualityComparisonHelper.InToleranceInterval(point.X, this.A.X, this.B.X) &&              // point.X ∈ (-ε + min(A.X, B.X), max(A.X, B.X) + ε)
            FloatingTypeEqualityComparisonHelper.InToleranceInterval(point.X, this.A.X, this.B.X));                // point.Y ∈ (-ε + min(A.Y, B.Y), max(A.Y, B.Y) + ε)
 }