Python method for judging whether lines and rectangles intersect

Source: Internet
Author: User
This example describes how Python determines whether a line and a rectangle intersect. Share to everyone for your reference. The implementation method is as follows:

"" A (Ax,ay), B (Px,py) is two points (x1,y1), (X2,Y2) is the upper-left and lower-right coordinates of the rectangle, judging whether A and b two points intersect 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)  #判断矩形上边线和两点直线相交的点  sx = (y1-ay) * (Px-ax)/(Py-ay) + ax  if SX >= x1 and SX <= x2:
  
   return True  #判断矩形下边线和两点直线相交的点  xx = (y1-ay) * (Px-ax)/(Py-ay) + ax  if SX >= x1 and SX <= X2:
   return True  #判断矩形左边线和两点直线相交的点  zy = (y2-ay) * (X2-ax)/(Px-ax) + ay  if Zy >= y1 and Zy <= y2:    return True  #判断矩形右边线和两点直线相交的点  yy = (y2-ay) * (X2-ax)/(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 = in Put () y2 = input () print Judge (ax, ay, px, py, x1, y1, x2, y2)
  

Hopefully 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.