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

GetAngleBetweenLines() public method

Calculate minimum angle between this line and the specified line measured in [0, 90] degrees range.
public GetAngleBetweenLines ( Line secondLine ) : float
secondLine Line The line to find angle between.
return float
		public float GetAngleBetweenLines( Line secondLine )
		{
			float k2 = secondLine.k;

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

			// check if lines are parallel
			if ( ( k == k2 ) || ( isVertical1 && isVertical2 ) )
				return 0;

			float angle = 0;

			if ( ( !isVertical1 ) && ( !isVertical2 ) )
			{
				float tanPhi = ( ( k2 > k ) ? ( k2 - k ) : ( k - k2 ) ) / ( 1 + k * k2 );
				angle = (float) Math.Atan( tanPhi );
			}
			else
			{
				// one of the lines is parallel to Y axis

				if ( isVertical1 )
				{
					angle = (float) ( Math.PI / 2 - Math.Atan( k2 ) * Math.Sign( k2 ) );
				}
				else
				{
					angle = (float) ( Math.PI / 2 - Math.Atan( k ) * Math.Sign( k ) );
				}
			}

			// convert radians to degrees
			angle *= (float) ( 180.0 / Math.PI );

			if ( angle < 0 )
			{
				angle = -angle;
			}

			return angle;
		}

Usage Example

Example #1
0
        public static float GetAngleBetweenLines(Point a1, Point a2, Point b1, Point b2)
        {
            Line line = Line.FromPoints(a1, a2);

            return(line.GetAngleBetweenLines(Line.FromPoints(b1, b2)));
        }