Calculate the intersection of two straight lines (C #)

Source: Internet
Author: User

The source code from other places is problematic.

/// <Summary> /// calculate the intersection of two straight lines /// </summary> /// <param name = "lineFirstStar"> coordinate 1 of L1 Points </param> /// <param name = "lineFirstEnd"> L1 coordinate 2 </param> /// <param name = "lineSecondStar"> L2 coordinate 1 </param> /// <param name = "lineSecondEnd"> L2 coordinate 2 </param> /// <returns> </returns> public static PointF GetIntersection (PointF lineFirstStar, pointF lineFirstEnd, PointF lineSecondStar, PointF lineSecondEnd) {/** L1, L2 all have slope * Linear equation L1: (y-y1)/(y2-y1) = (x-x1)/(x2-x1) * => y = [(y2-y1)/(x2-x1)] (x-x1) + y1 * Make a = (y2-y1)/(x2-x1) * y = a * x-a * x1 + y1 ......... 1 * linear equation L2: (y-y3)/(y4-y3) = (x-x3)/(x4-x3) * Order B = (y4-y3) /(x4-x3) * y = B * x-B * x3 + y3 .......... 2 ** if a = B, the two straight lines are equal. Otherwise, the equation 1, 2 is obtained: * x = (a * x1-B * x3-y1 + y3 ). )/(A-B) * y = a * x-a * x1 + y1 ** L1 has a slope and L2 parallel y axis: * x = x3 * y = a * x3-a * x1 + y1 ** L1 parallel y axis. L2 has a slope: * x = x1 * y = B * x-B * x3 + y3 ** when both L1 and L2 are parallel y axes: * If x1 = x3, L1 and L2 coincide, otherwise equal **/float a = 0, B = 0; int state = 0; if (lineFirstStar. x! = LineFirstEnd. X) {a = (lineFirstEnd. Y-lineFirstStar. Y)/(lineFirstEnd. X-lineFirstStar. X); state | = 1 ;}if (lineSecondStar. X! = LineSecondEnd. x) {B = (lineSecondEnd. y-lineSecondStar. y)/(lineSecondEnd. x-lineSecondStar. x); state | = 2;} switch (state) {case 0: // L1 and L2 are both parallel y axes {if (lineFirstStar. X = lineSecondStar. x) {// throw new Exception ("the two straight lines overlap with each other and are parallel to the Y axis. The intersection cannot be calculated. "); Return new PointF (0, 0);} else {// throw new Exception (" the two straight lines are parallel to each other and are parallel to the Y axis. The intersection cannot be calculated. "); Return new PointF (0, 0);} case 1: // L1 slope, L2 parallel Y axis {float x = lineSecondStar. x; float y = (lineFirstStar. x-x) * (-a) + lineFirstStar. y; return new PointF (x, y);} case 2: // L1 parallel Y axis, L2 has slope {float x = lineFirstStar. x; // similar code is found on the Internet, which is incorrect. You can compare the logic of case 1 for analysis // source code: lineSecondStar * x + lineSecondStar * lineSecondStar. X + p3.Y; float y = (lineSecondStar. x-x) * (-B) + lineSecondStar. y; return new PointF (x, y);} case 3: // L1, L2 all have slope {if (a = B) {// throw new Exception ("the intersection cannot be calculated because two straight lines are parallel or overlapped. "); Return new PointF (0, 0);} float x = (a * lineFirstStar. x-B * lineSecondStar. x-lineFirstStar. Y + lineSecondStar. y)/(a-B); float y = a * x-a * lineFirstStar. X + lineFirstStar. y; return new PointF (x, y) ;}// throw new Exception ("impossible situation"); return new PointF (0, 0 );}

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.