AForge.Math.Geometry.Line.GetIntersectionWith C# (CSharp) Method

GetIntersectionWith() public method

Finds intersection point with the specified line.
Thrown if the specified line is the same line as this line.
public GetIntersectionWith ( Line secondLine ) : Point?
secondLine Line Line to find intersection with.
return Point?
		public Point? GetIntersectionWith( Line secondLine )
		{
			float k2 = secondLine.k;
			float b2 = secondLine.b;

			bool isVertical1 = IsVertical;
			bool isVertical2 = secondLine.IsVertical;

			Point? intersection = null;

			if ( ( k == k2 ) || ( isVertical1 && isVertical2 ) )
			{
				if ( b == b2 )
				{
					throw new InvalidOperationException( "Identical lines do not have an intersection point." );
				}
			}
			else
			{
				if ( isVertical1 )
				{
					intersection = new Point( b, k2 * b + b2 );
				}
				else if ( isVertical2 )
				{
					intersection = new Point( b2, k * b2 + b );
				}
				else
				{
					// the intersection is at x=(b2-b1)/(k1-k2), and y=k1*x+b1
					float x = ( b2 - b ) / ( k - k2 );
					intersection = new Point( x, k * x + b );
				}
			}

			return intersection;
		}

Same methods

Line::GetIntersectionWith ( LineSegment other ) : Point?

Usage Example

Example #1
0
        /// <summary>
        /// Finds, provided it exists, the intersection point with the specified <see cref="Line"/>.
        /// </summary>
        ///
        /// <param name="other"><see cref="Line"/> to find intersection with.</param>
        ///
        /// <returns>Returns intersection point with the specified <see cref="Line"/>, or <see langword="null"/>, if
        /// the line does not intersect with this segment.</returns>
        ///
        /// <remarks><para>If the line and the segment do not intersect, the method returns <see langword="null"/>. If the line
        /// and the segment share multiple points, the method throws an <see cref="InvalidOperationException"/>.
        /// </para></remarks>
        ///
        /// <exception cref="InvalidOperationException">Thrown if this segment is a portion of
        /// <paramref name="other"/> line.</exception>
        ///
        public Point?GetIntersectionWith(Line other)
        {
            Point?result;

            if ((line.Slope == other.Slope) || (line.IsVertical && other.IsVertical))
            {
                if (line.Intercept == other.Intercept)
                {
                    throw new InvalidOperationException("Segment is a portion of the specified line.");
                }

                // unlike Line.GetIntersectionWith(Line), this does not throw on parallel distinct lines
                result = null;
            }
            else
            {
                result = line.GetIntersectionWith(other);
            }

            if ((result.HasValue) && (LocateProjection(result.Value) != ProjectionLocation.SegmentAB))
            {
                // the intersection is on this segment's extended line, but not on the segment itself
                result = null;
            }

            return(result);
        }
All Usage Examples Of AForge.Math.Geometry.Line::GetIntersectionWith