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.