""" A(ax,ay),B(px,py)為兩個(gè)點(diǎn) (x1,y1),(x2,y2)為矩形的左上角和右下角坐標(biāo) ,判斷A,B兩點(diǎn)是否和矩形相交 """ def Judge(ax, ay, px, py, x1, y1, x2, y2): #轉(zhuǎn)換為真除法 ax, ay, px, py = float(ax), float(ay), float(px), float(py) x1, y1, x2, y2 = float(x1), float(y1), float(x2), float(y2) #判斷矩形上邊線和兩點(diǎn)直線相交的點(diǎn) sx = (y1 - ay) * (px - ax) / (py - ay) + ax if sx >= x1 and sx <= x2: return True #判斷矩形下邊線和兩點(diǎn)直線相交的點(diǎn) xx = (y1 - ay) * (px - ax) / (py - ay) + ax if sx >= x1 and sx <= x2: return True #判斷矩形左邊線和兩點(diǎn)直線相交的點(diǎn) zy = (y2 - ay) * (x2 - ax) / (px - ax) + ay if zy >= y1 and zy <= y2: return True #判斷矩形右邊線和兩點(diǎn)直線相交的點(diǎn) yy = (y2 - ay) * (x2 - ax) / (px - ax) + ay if yy <= y1 and yy >= y2: return True return False ax = 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)
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com