2016-06-01 2 views
1

그래서 파이썬을 연습하는 동안이 문제가 발생하지만 모든 언어에 적용됩니다. 나는 모든 대각선을 규칙적인 n-gon으로 그릴 것이다. 그리고 그것은 멋지다, 나는 그럭저럭 할 수 있었다. 그러나 또 다른 기준이 있습니다. 동일한 선을 두 번 이상 그려서는 안됩니다. 제가 이것을 해석하는 방법은 거북이 (저는 거북이 그래픽을 사용합니다)가 같은 두 구석 사이를 두 번 움직일 수는 없으며 단지 펜을 들어야 만하는 것이 아닙니다. 나는 잠시 동안 이것에 대한 해결책을 찾아 내려고 노력해 왔지만 그것을 이해하는 것처럼 보일 수는 없으며 그것이 가능할 지 궁금해지기 시작했다.효과적으로 n-gon의 대각선 그리기

모든 n-gons에 대해 수행 할 수있는 경우 여기에있는 사람에게 알리시겠습니까? 그리고 만약 그렇다면, 나에게 힌트를 줄 수 있겠 니?

무엇인지 모르는 사람들을위한 두 개의 정규 n-gons가 있습니다. (I 있는지 도대체하지 그랬던 것처럼)

Nonagon

Octagon

/Q

EDIT 내가 풀 수있는 부분을 할 수 있었다 요한 칸에

감사합니다, 그가 말했듯이, 그것은 일정하지 않은 정도의 규칙적인 n-gons에서만 수행 될 수 있습니다. 내 솔루션 코드는 다음과 같습니다. 어떻게 생각해?

def nhorning(r, n,): 

    if n % 2 == 0: 
     print("It can't be done") 
     return None 
    angl = (2 * pi)/n # angle for calculating all the coordinates of the n-gon 
    a = {} # contains the destinations for each corners diagonals 
    cord = {} # contains the coordinates of each corner 
    for x in range(n): 
     cord[x] = [float("%.2f" % (r * cos(x * angl))), float("%.2f" % (r * sin(x * angl)))] # all corners coordinates 
    for i in range(n): # the diagonals that are to be drawn from the corner "i" 
     a[i] = [x for x in range(n)] 
     a[i].remove(i) # can't draw a diagonal to itself 
    cunt = 0 
    pu() 
    goto(cord[0]) # you have to start on a corner 
    pd() 
    cordstring = str(cord) # a list isn't hashable, so making the lists to a string 

    while cunt < (((n-1) * n)/2): # loops until all diagonals are drawn 


     if str(list(pos())) in cordstring: # should always be on the circles radius except in the beginning 
      for i in range(len(cord)): 
       if cord[i] == list(pos()): # finds what corner the turtle is on 
        where = i 

      diags = a[where] # the diagonals not drawn from the corner 

      dest = diags.pop() # the corner to which a diagonal is to be drawn, 
           # removes it since the diagonal to that corner will be drawn 

      nwhere = a[dest] # the diagonals not drawn from the corner where a 
           # diagonal is to be drawn next 

      nwhere.remove(where) # the diagonal from the destination corner to the current corner will be drawn next, 
            # so can't be drawn again 

      goto(cord[dest]) # draw the diagonal 

      cunt += 1 



    done() 

답변

2

TLDR

당신은 Eulerian Path 찾고 있습니다.

홀수 개의 정점을 사용하여이를 수행 할 수 있지만 짝수로는 불가능합니다.

설명

"이것이 사실 왜 경로가 정점을 통과 할 때마다, 그것은 정점에 연결된 가장자리의 두 사용 있습니다. 첫 번째를 제외하고이 때문에, 모든 정점을 보려면 마지막 경로는 균일해야합니다. 사이클의 경우 첫 번째 및 마지막 꼭지점이 동일하므로 모든 꼭지점이 균일해야합니다. " - For a square, but the concept applies to n-gons