python实现gjk算法
时间: 2023-11-02 14:06:06 浏览: 339
为 C、C# 和Matlab 快速可靠地实现 Gilbert-Johnson-Keerthi (GJK)算法_代码_下载
5星 · 资源好评率100%
GJK算法是一种用于计算两个凸多边形之间最小距离的算法,其基本思想是通过迭代逼近两个凸多边形的Minkowski差集的最小点,从而得到最小距离。下面是一个简单的Python实现:
```python
import numpy as np
def support(a, b, d):
return np.argmax(np.dot(a, d)), np.argmax(np.dot(b, -d))
def gjk(a, b):
d = np.array([1, 0])
p = support(a, b, d)
q = support(a, b, -d)
d = q - p
simplex = [q, p]
while True:
a, b = simplex[-1], simplex[-2]
ao, bo = -a, b
ab = b - a
ap = support(a, b, ab)
if np.dot(ap - a, ab) <= 0:
return False
bp = support(a, b, -ab)
if np.dot(bp - b, -ab) <= 0:
return False
if np.dot(ap - a, ao) >= 0 and np.dot(bp - b, bo) >= 0:
return True
if np.dot(ap - a, ao) < np.dot(bp - b, bo):
simplex[-2], simplex[-1] = simplex[-1], simplex[-2]
simplex[-1] = ap
d = np.cross(ab, ap - a)
else:
simplex[-2], simplex[-1] = simplex[-1], simplex[-2]
simplex[-1] = bp
d = np.cross(ab, bp - b)
相关问题:
阅读全文