目录
python 计算线段的相似度
import math
def line_similarity(line1, line2):
# 计算线段的向量
vector1 = (line1[1][0] - line1[0][0], line1[1][1] - line1[0][1])
vector2 = (line2[1][0] - line2[0][0], line2[1][1] - line2[0][1])
# 计算线段的长度
length1 = math.sqrt(vector1[0]**2 + vector1[1]**2)
length2 = math.sqrt(vector2[0]**2 + vector2[1]**2)
# 计算长度比例
length_ratio = min(length1, length2) / max(length1, length2)
# 计算方向的余弦相似度
dot_product = vector1[0]*vector2[0] + vector1[1]*vector2[1]
cosine_similarity = dot_product / (length1 * length2)
# 计算线段中点