2014-09-11 2 views
1

임의의 점 집합을 거친 모서리가있는 사각형 모양으로 배열하고 가장 바깥 쪽 점 만 음모를 꾸미고 싶습니다. 모양의 모서리 (일부 겹치는 여러 유사한 데이터 집합 사이에 명확한 경계선을 가질 수 있도록).파이썬 - 데이터 집합의 가장 바깥 쪽 점 만 그릴 때

포인트를 선택하는 방법에 대한 제안 사항에 크게 감사드립니다.

+0

당신이 오전 [MCVE] 지금까지 알아 낸 코드를 제공 할 수있는 다음의 예를 제공합니다 (http://stackoverflow.com/help/mcve)? – Ffisegydd

+0

나는 지금 코드를 추가하고 있었지만 아래의 답은 문제를 해결한다. 감사! – mannaroth

답변

3

데이터 집합의 볼록 선체를 계산할 수 있습니다. 다음은 pure-Python implementation입니다. 더 나은 성능을 가질 수있다 third-party packages이 있습니다

import random 
import sys 
import matplotlib.pyplot as plt 

CLOCKWISE = -1 
COLLINEAR = 0 
COUNTERCLOCKWISE = +1 
eps = sys.float_info.epsilon 


def orientation(a, b): 
    x0, y0 = a 
    x1, y1 = b 
    cross = x0 * y1 - x1 * y0 
    if cross > eps: 
     return COUNTERCLOCKWISE 
    elif cross < -eps: 
     return CLOCKWISE 
    else: 
     return COLLINEAR 


def same_halfplane(a, b): 
    x0, y0 = a 
    x1, y1 = b 
    dot = x0 * x1 + y0 * y1 
    if dot >= eps: 
     return True 
    elif dot < eps: 
     return False 


def jarvis(points): 
    """ 
    http://cgi.di.uoa.gr/~compgeom/pycgalvisual/whypython.shtml 
    Jarvis Convex Hull algorithm. 
    """ 
    points = points[:] 
    r0 = min(points) 
    hull = [r0] 
    r, u = r0, None 
    remainingPoints = [x for x in points if x not in hull] 
    while u != r0 and remainingPoints: 
     u = random.choice(remainingPoints) 
     for t in points: 
      a = (u[0] - r[0], u[1] - r[1]) 
      b = (t[0] - u[0], t[1] - u[1]) 
      if (t != u and 
       (orientation(a, b) == CLOCKWISE or 
       (orientation(a, b) == COLLINEAR and 
        same_halfplane(a, b)))): 
       u = t 
     r = u 
     points.remove(r) 
     hull.append(r) 
     try: 
      remainingPoints.remove(r) 
     except ValueError: 
      # ValueError: list.remove(x): x not in list 
      pass 
    return hull 

if __name__ == '__main__': 
    points = iter(random.uniform(0, 10) for _ in xrange(20)) 
    points = zip(points, points) 
    hull = jarvis(points) 
    px, py = zip(*points) 
    hx, hy = zip(*hull) 
    plt.plot(px, py, 'b.', markersize=10) 
    plt.plot(hx, hy, 'g.-', markersize=10) 
    plt.show() 

enter image description here

2

당신은 scipy docs를 참조 scipy의 볼록 선체 기능을 사용할 수 있습니다. 워드 프로세서 페이지

from scipy.spatial import ConvexHull 
points = np.random.rand(30, 2) # 30 random points in 2-D 
hull = ConvexHull(points) 

import matplotlib.pyplot as plt 
plt.plot(points[:,0], points[:,1], 'o') 
# plot convex hull polygon 
plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'r--', lw=2) 
# plot convex full vertices 
plt.plot(points[hull.vertices[0],0], points[hull.vertices[0],1], 'ro') 
plt.show() 
관련 문제