Python判断直线和矩形是否相交的方法
2019/7/13 21:03:16
本文主要是介绍Python判断直线和矩形是否相交的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例讲述了Python判断直线和矩形是否相交的方法。分享给大家供大家参考。具体实现方法如下:
""" A(ax,ay),B(px,py)为两个点 (x1,y1),(x2,y2)为矩形的左上角和右下角坐标 ,判断A,B两点是否和矩形相交 """ def Judge(ax, ay, px, py, x1, y1, x2, y2): #转换为真除法 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 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)
希望本文所述对大家的Python程序设计有所帮助。
这篇关于Python判断直线和矩形是否相交的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python