Calculates the intersection of a line segment and a circle.

Source: Internet
Author: User

Because oneProgramThis is used inAlgorithm, Vomit blood to write down ~

Set the two endpoints of a line segment to p1 (x1, Y1) and P2 (X2, Y2) respectively. The center of the circle is P3 (X3, Y3) and the radius is R, if there is an intersection of P (x, y ),


Where, U is between 0 and 1 and is converted into coordinates.

Because p is also on the circle

You can obtain

Where

Solve the quadratic equation of one element to obtain

Based on the results, you can determineLineIntersection with a circle

  • If it is less than 0, it indicates there is no intersection.
  • If it is equal to 0, it indicates tangent and there is only one intersection.
  • If the value is greater than 0, there are two intersections.

For the line segments between P1 and P2, there are 5 results based on the calculated U value.

  • If there is no intersection between a line segment and a circle, and both are outside the circle, the two solutions of u are smaller than 0 or greater than 1.
  • If there is no intersection between a line segment and a circle, and both are inside the circle, the two unsigned symbols of u are the opposite. One is less than 0, and the other is greater than 1.
  • If a line segment and a circle have only one intersection, one of the u values is between 0 and 1, and the other is not
  • If a line segment and a circle have two intersections, U is worth the fact that both solutions are between 0 and 1.
  • If the line segment is tangent to the circle, the U value has only one solution and is between 0 and 1.

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.