GJK算法python实现
时间: 2023-11-02 11:06:05 浏览: 302
GJK算法是一种用于计算两个凸多边形之间最近距离的算法。下面是一个简单的Python实现:
```python
import numpy as np
def support(a, b, d):
return np.argmax(np.dot(a-b, d))
def gjk(a, b):
simplex = [support(a, b, np.array([1, 0])) - support(a, b, np.array([-1, 0]))]
while True:
d = -simplex[-1]
if len(simplex) == 3:
a, b, c = simplex
ab, ac, ad = b-a, c-a, d-a
abc = np.cross(ab, ac)
if np.dot(np.cross(abc, ac), ad) > 0:
simplex.remove(c)
simplex.remove(simplex[-1])
continue
if np.dot(np.cross(ab, abc), ad) > 0:
simplex.remove(c)
continue
if np.dot(abc, ad) > 0:
return False
return True
else:
simplex.append(support(a, b, d))
```
阅读全文