我很难弄清楚如何填充sierpinski三角形,使其看起来像这样:
我不确定如何“填充”三角形,因为在代码self.canvas.create_line(p1[0], p1[1], p2[0], p2[1], p3[0], p3[1], p1[0], p1[1], tags = 'line')
中执行 fill ='green '时,只有线条是绿色的,因为它不是形状,因此,我该如何填充它?如果有人可以指引我正确的方向,我将不胜感激!
self.label = tkinter.Label(self.frame1, text = 'Enter an order: ')
#Convert string to integer
self.order = tkinter.StringVar()
#Text variable needs to be string
self.entry = tkinter.Entry(self.frame1, textvariable = self.order, \
justify = 'right')
self.button = tkinter.Button(self.frame1, \
text = 'Display Sierpinski Triangle', \
command = self.display)
self.label.pack(side = 'left')
self.entry.pack(side = 'left')
self.button.pack(side = 'left')
self.main_window.mainloop()
def display(self):
self.canvas.delete('line')
p1 = [self.width/2, 10]
p2 = [10, self.height - 10]
p3 = [self.width - 10, self.height - 10]
#Convert self.order from string to integer
self.displayTriangle(int(self.order.get()), p1, p2, p3)
def displayTriangle(self, order, p1, p2, p3):
if order == 0: #Base Case
self.canvas.create_line(p1[0], p1[1], p2[0], p2[1], p3[0], p3[1], \
p1[0], p1[1], tags = 'line')
else: #General Case
p12 = self.midpoint(p1, p2)
p23 = self.midpoint(p2, p3)
p31 = self.midpoint(p3, p1)
self.displayTriangle(order - 1, p1, p12, p31)
self.displayTriangle(order - 1, p12, p2, p23)
self.displayTriangle(order - 1, p31, p23, p3)
def midpoint(self, p1, p2):
p = [0, 0]
p[0] = (p1[0] + p2[0])/ 2
p[1] = (p1[1] + p2[1])/ 2
return p