所以我在练习python时遇到了这个问题,但我想它适用于所有语言。我要用普通的n-gon绘制所有的对角线,这很好,我设法做到了。但是,还有另一个标准,同一条线不能多次绘制。我解释这个的方式是乌龟(我使用乌龟图形顺便说一句)不能在相同的两个角之间穿过两次,而不仅仅是你要抬起笔。我一直试图找到解决方案一段时间,但我似乎无法弄明白并开始怀疑它是否有可能。
这里的任何人都知道是否可以为所有n-gons做什么?如果是的话,你能给我一个暗示吗?
对于那些不知道那是什么的人,这里有两个常规的n-gons。 (我当然没有)
/ Q
修改的
感谢John Kahn我能够做出可解决的部分,因为他指出它只能在不规则程度的常规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()
答案 0 :(得分:2)
<强> TLDR 强>
您正在寻找Eulerian Path。
可以使用奇数个顶点执行此操作,但使用偶数值则不可能。
<强>解释强>
“要知道为什么会这样,请注意每次路径通过顶点时,它会使用连接到顶点的两条边。因此,除路径上的第一个和最后一个之外的所有顶点都需要在一个循环的情况下,第一个和最后一个顶点是相同的,所以所有顶点都需要具有均匀度。“ - For a square, but the concept applies to n-gons