UPDATE : OP는 모든 예를 준 전에이 코드가 작성되었습니다. 그것은 포인트 클라우드와 함께 작동해야합니다. 그러나 "거의 평행 사변형"을 찾을 수는 없습니다.
순진한 접근법은 모든 점 쌍에 대해 (dx, dy)
을 계산하여이를 사전에 저장하는 것입니다. dict의 값은 점 쌍 목록입니다. 1 쌍 이상인 경우 쌍의 모든 조합이 평행 사변형을 형성합니다.
효율적인 것은 아니지만 (O(n**2)
) 여전히 2300 포인트로 수행 할 수 있습니다. 또한 4 포인트 튜플을 테스트하는 것보다 훨씬 효율적입니다.
from random import randint, random
from collections import defaultdict
import matplotlib.pyplot as plt
import matplotlib.patches as patches
# N = 2300
# width = 3000
# height = 2000
N = 180
width = 3000
height = 2000
points = [(randint(0, width), randint(0, height)) for _ in range(N)]
points = list(set(points)) # unique points
n = len(points)
plt.scatter(*zip(*points), linewidth=0.001)
vectors = defaultdict(list)
for i in range(n):
x1, y1 = points[i]
for j in range(i + 1, n):
x2, y2 = points[j]
vectors[(x2 - x1, y2 - y1)].append((i, j))
ax = plt.gca()
for vector, pairs in vectors.items():
if len(pairs) > 1:
# TODO: Consider every combination if len(pairs) > 2
a, b, c, d = points[pairs[0][0]], points[pairs[0][1]], points[pairs[1][1]], points[pairs[1][0]]
ax.add_patch(patches.Polygon(xy=[a, b, c, d], fill=False, color=[random(), random(), random()]))
plt.show()
여기에 3000 * 2000 표에서 180 점을 출력입니다 :
가 여기에 신속하고 더러운 구현의

2300 점으로, 당신이 많이 찾을 수 있습니다 평행 사변형.
우리는 정말 심지어 이것에 대해 생각을 시작하기 위해 입력/출력의 일부 코드와 형식/형태를 필요 했어. [ask] 및 [mcve]를 읽어주십시오. –
의견을 보내 주셔서 감사합니다. – Pradhuman