python 空间距离计算

目录

python 空间距离计算

已知两点,画三角形

批量矩阵计算


python 空间距离计算

要在空间中找到一个点,使其位于点 b 和 c 之间的连线上,并且与点 b 的距离等于点 a 到点 b 的距离的2倍。

import numpy as np

if __name__ == '__main__':

    a = np.array([1, 2, 1])
    b = np.array([3, 4,2])
    c = np.array([7, 8, 15])


    bc = c - b

    print('dis a',np.linalg.norm(b - a) )
    # 计算点 d 的坐标,使得 db = ab
    d = b + 2*(np.linalg.norm(b - a) / np.linalg.norm(bc)) * bc

    print("点 d 的坐标为:", d)

    print('dis a', np.linalg.norm(d - b))

已知两点,画三角形

import numpy as np
import cv2

# 定义点 a 和 b
a = np.array([100, 400])
b = np.array([300, 200])

# 计算向量 ab 和点 d 的坐标
ab = b - a
d = a + 0.65 * ab

# 计算向量 ab 的单位法向量
n = np.array([-ab[1], ab[0]])
n = n / np.linalg.norm(n)

up=-1
# 计算点 c 的坐标
c = d -up* int(0.2 * np.linalg.norm(ab)) * n

# 创建一个黑色背景的图像
height, width = 500, 500
img = np.zeros((height, width, 3), dtype=np.uint8)

# 在图像上绘制点和线段
cv2.circle(img, tuple(a), 5, (0, 0, 255), -1)
cv2.circle(img, tuple(b), 5, (0, 255, 0), -1)
cv2.circle(img, tuple(c.astype(int)), 5, (255, 0, 0), -1)
cv2.line(img, tuple(a), tuple(c.astype(int)), (255, 255, 255), 2)
cv2.line(img, tuple(b.astype(int)), tuple(c.astype(int)), (255, 255, 255), 2)

# 添加文字标签
cv2.putText(img, 'A', tuple(a - np.array([10, 10])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
cv2.putText(img, 'B', tuple(b + np.array([10, -10])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
cv2.putText(img, 'C', tuple(c.astype(int) + np.array([10, -10])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)

# 显示图像
cv2.imshow('Points and Lines with Labels', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

批量矩阵计算

import numpy as np
import cv2

if __name__ == '__main__':

    up=1
    # 定义多组点 a 和 b 的坐标
    A = np.array([[100, 100], [150, 250], [200, 300]])
    B = np.array([[300, 400], [400, 450], [400, 600]])

    # 计算向量 AB 和点 D 的坐标
    AB = B - A
    D = A + 0.65 * AB

    # 计算向量 AB 的单位法向量
    N = np.array([-AB[:, 1], AB[:, 0]]).T
    N_norm = np.linalg.norm(N, axis=1, keepdims=True)
    N_unit = N / N_norm

    # 计算点 C 的坐标
    C = D + 0.2 *up* np.linalg.norm(AB, axis=1, keepdims=True) * N_unit

    # 创建一个黑色背景的图像
    height, width = 600, 600
    img = np.zeros((height, width, 3), dtype=np.uint8)

    # 在图像上绘制点和线段
    for i in range(A.shape[0]):
        a, b, c = A[i], B[i], C[i]
        cv2.circle(img, tuple(a), 5, (0, 0, 255), -1)
        cv2.circle(img, tuple(b), 5, (0, 255, 0), -1)
        cv2.circle(img, tuple(c.astype(int)), 5, (255, 0, 0), -1)
        cv2.line(img, tuple(a), tuple(c.astype(int)), (255, 255, 255), 2)
        cv2.line(img, tuple(b), tuple(c.astype(int)), (255, 255, 255), 2)
        cv2.putText(img, f'A{i + 1}', tuple(a - np.array([10, 10])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
        cv2.putText(img, f'B{i + 1}', tuple(b + np.array([10, -10])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
        cv2.putText(img, f'C{i + 1}', tuple(c.astype(int) + np.array([10, -10])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)

    # 显示图像
    cv2.imshow('Batch Points and Lines with Labels', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

相关推荐

  1. mysql 根据经纬度计算距离

    2024-03-23 01:42:02       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-23 01:42:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-23 01:42:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-23 01:42:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-23 01:42:02       20 阅读

热门阅读

  1. 力扣由浅至深 每日一题.10 最后一个单词的长度

    2024-03-23 01:42:02       19 阅读
  2. Docker attach V/S Docker exec

    2024-03-23 01:42:02       18 阅读
  3. 软件测试---Linux命令

    2024-03-23 01:42:02       18 阅读
  4. 低代码,我的理解

    2024-03-23 01:42:02       18 阅读
  5. Qt 显示图片

    2024-03-23 01:42:02       21 阅读
  6. xml转json

    2024-03-23 01:42:02       20 阅读
  7. FDU 2020 | 5.二叉搜索树的父节点

    2024-03-23 01:42:02       17 阅读