So habe ich dieses Problem beim Üben Python, aber ich denke, es gilt für alle Sprachen. Ich zeichne alle Diagonalen in einem normalen N-Gon und das ist gut, das habe ich geschafft. Aber es gibt noch ein anderes Kriterium, die gleiche Zeile darf nicht mehr als einmal gezeichnet werden. Die Art, wie ich das interpretiere, ist, dass die Schildkröte (ich benutze Schildkröte Grafiken BTW) kann nicht zwischen den gleichen zwei Ecken reisen, und nicht nur, dass Sie den Stift zu heben sind. Ich habe auch schon eine Weile versucht, eine Lösung dafür zu finden, aber ich kann es nicht herausfinden und habe mich gefragt, ob das überhaupt möglich ist.Zeichnen Diagonalen eines N-Gon, effektiv
Weiß jemand hier, ob es für alle n-gons möglich ist? Und wenn es so ist, könntest du mir einen Hinweis geben?
Hier sind zwei normale n-gons für diejenigen, die nicht wissen, was das ist. (ich todsicher tat nicht)
/Q
EDIT
Dank John Kahn konnte ich das lösbare Teil tun, wie er darauf hingewiesen hat, kann es nur auf regelmäßigen n-gonen eines ungleichmäßigen Grades getan werden. Hier ist der Code für meine Lösung. Was denken Sie?
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()