How does Python determine whether a straight line and a rectangle are intersecting?

Source: Internet
Author: User
This article mainly introduces Python's method of determining whether a straight line and a rectangle are intersecting. It involves the calculation of the line and a rectangle in the Python coordinate system and has some reference value, for more information about how to determine the intersection of a straight line and a rectangle in Python, see the following example. Share it with you for your reference. The specific implementation method is as follows:

"" A (ax, ay), B (px, py) is two points (x1, y1), (x2, y2) is the coordinates of the upper left corner and lower right corner of the rectangle, determine whether two points A and B are intersecting with the rectangle "def Judge (ax, ay, px, py, x1, y1, x2, y2): # convert to true division ax, ay, px, py = float (ax), float (ay), float (px), float (py) x1, y1, x2, y2 = float (x1 ), float (y1), float (x2), float (y2) # determine the point of intersection between the top line of a rectangle and the two points. sx = (y1-ay) * (px-ax) /(py-ay) + ax if sx> = x1 and sx <= x2: return True # determine the point xx = (y1-ay) at which the bottom line of the rectangle is located at the intersection of the two points) * (px-ax)/(py-ay) + ax if sx> = x1 and sx <= x2: return True # determine the intersection between the left line of the rectangle and the two-point line zy = (y2-ay) * (x2-ax)/(px-ax) + ay if zy> = y1 and zy <= y2: return True # judge the point yy = (y2-ay) * (x2-ax) of the intersection between the right line of the rectangle and the two points) /(px-ax) + ay if yy <= y1 and yy> = y2: return True return Falseax = raw_input () ay = input () px = input () py = input () x1 = input () y1 = input () x2 = input () y2 = input () print Judge (ax, ay, px, py, x1, y1, x2, y2)

I hope this article will help you with Python programming.

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.