pyqt/libs/tools.py

33 lines
726 B
Python
Raw Normal View History

2022-11-24 17:57:53 +08:00
import math
def line_intersection(line1, line2):
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
def det(a, b):
return a[0] * b[1] - a[1] * b[0]
div = det(xdiff, ydiff)
if div == 0:
raise Exception('lines do not intersect')
d = (det(*line1), det(*line2))
x = det(d, xdiff) / div
y = det(d, ydiff) / div
return x, y
def norm(point1, point2):
xdiff = point1[0] - point2[0]
ydiff = point1[1] - point2[1]
norm = math.sqrt(xdiff * xdiff + ydiff * ydiff)
# print norm
return norm
def str_tuple(str: str):
val = eval(str)
return val