Python determines whether a straight line and a rectangle intersect. python rectangle
This article describes how to determine whether a straight line and a rectangle are intersecting by Python. 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.