C # code used to determine whether two line segments overlap

Source: Internet
Author: User

Http://www.bkjia.com/kf/201111/112304.html tonight
The c # code is written to determine whether the two line segments overlap. The basic idea is the intersection of two line segments. For any line segment, the points at both ends are on both sides of the other line.

 

 

/// <Summary>

/// Determine whether two points of Line 2 are on either side of Line 1.

/// </Summary>

/// <Param name = "line1"> line 1 </param>

/// <Param name = "line2"> line 2 </param>

/// <Returns> </returns>

Private bool CheckCrose (Line line1, Line line2)

{

PointF v1 = new PointF ();

PointF v2 = new PointF ();

PointF v3 = new PointF ();

 

V1.X = line2.Start. X-line1.End. X;

V1.Y = line2.Start. Y-line1.End. Y;

 

V2.X = line2.End. X-line1.End. X;

V2.Y = line2.End. Y-line1.End. Y;

 

V3.X = line1.Start. X-line1.End. X;

V3.Y = line1.Start. Y-line1.End. Y;

 

Return (CrossMul (v1, v3) * CrossMul (v2, v3) <= 0 );

 

}

/// <Summary>

/// Determine whether two line segments meet each other.

/// </Summary>

/// <Param name = "line1"> Line Segment 1 </param>

/// <Param name = "line2"> Line Segment 2 </param>

/// <Returns> returns true if the intersection is true. Otherwise, false is returned. </Returns>

Private bool CheckTwoLineCrose (Line line1, Line line2)

{

Return CheckCrose (line1, line2) & CheckCrose (line2, line1 );

}

/// <Summary>

/// Calculate the cross multiplication of two vectors.

/// </Summary>

/// <Param name = "pt1"> </param>

/// <Param name = "pt2"> </param>

/// <Returns> </returns>

Private float CrossMul (PointF pt1, PointF pt2)

{

Return pt1.X * pt2.Y-pt1.Y * pt2.X;

}

 

Author I _code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.