# 我正在尝试生成Sierpinski Triangle的输出。我想知道如何在2d点类中使用midpt函数来实现此输出？

I have to generate the Sierpinski Triangle using a 2d point class and tkinter canvas.The midpt function is essentially going to take input from a randomly chosen vertex and the last plotted midpoint. You can choose any vertices to plot the first midpoint. The points also need to be instances of the 2d point class. I desperately need help because I can't seem to figure this out. Here is what the output is supposed to look like.

``````import math
from fractions import Fraction
from random import randrange
from Tkinter import *
# the 2D point class
class Point(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y

# Mutators and Accessors
@property
def x(self):
return self._x

@x.setter
def x(self, value):
self._x = value

@property
def y(self):
return self._y

@y.setter
def y(self, value):
self._y = value

# String function
def __str__(self):
floatX = float(str(self.x))
floatY = float(str(self.y))
return "({},{})".format(floatX, floatY)

# Distance function
def dist(self, other):
distance = math.sqrt(((self.x - other.x)**2)+((self.y - other.y)**2))
return "{}".format(distance)

# Midpoint function
def midpt(self, other):
x0 = float(str(((self.x + other.x))))/2
y0 = float(str(((self.y + other.y)/2)))
return Point(x0, y0)

# the coordinate system class: (0,0) is in the top-left corner
# inherits from the Canvas class of Tkinter
class ChaosGame(Canvas):
def __init__(self, master):
Canvas.__init__(self, master, bg = "white")
self.pack(fill = BOTH, expand = 1)

def plotPoints(self, triangle, NumberPoints):
x0, y0 = WIDTH / 2, HEIGHT / 2
direction = center
for i in range(NumberPoints):
point = randrange(len(triangle))
direction = triangle[point]
x0 = (direction[0] + x0) / 2
y0 = (direction[1] + y0) / 2
color = direction[1]
self.plot(x0, y0)
self.plotV(5, 510)
self.plotV(290, 5)
self.plotV(590, 510)

def plot(self, x, y):
POINT_COLORS=["black"]
color = POINT_COLORS
self.create_oval(x, y, x+2, y+2, outline = color, fill = color)

def plotV(self, x, y):
POINT_COLORS=["red"]
color = POINT_COLORS

##########################################################
# ***DO NOT MODIFY OR REMOVE ANYTHING BELOW THIS POINT!***
# the default size of the canvas is 600x520
WIDTH = 600
HEIGHT = 520
# the number of points to plot
NumberPoints = 50000

# the vertices
A = (5, 510)
B = (290, 5)
C = (590, 510)
triangle = (A, B, C)
center = (WIDTH/2, HEIGHT/2)

# create the window
window = Tk()
window.geometry("{}x{}".format(WIDTH, HEIGHT))
window.title("2D Points...Plotted")
# create the chaos game as a Tkinter canvas inside the window
s = ChaosGame(window)
# plot some random points
s.plotPoints(triangle, NumberPoints)
# wait for the window to close
window.mainloop()
``````

x呆子x

``````import math
from random import choice
from Tkinter import *

class Point(object):
def __init__(self, x=0, y=0):
self._x = x
self._y = y

# Mutators and Accessors
@property
def x(self):
return self._x

@x.setter
def x(self, value):
self._x = value

@property
def y(self):
return self._y

@y.setter
def y(self, value):
self._y = value

# String Function
def __str__(self):
return "({},{})".format(float(self.x), float(self.y))

# Distance Function
def dist(self, other):
return math.sqrt(((self.x - other.x)**2) + ((self.y - other.y)**2))

# Midpoint Function
def midpoint(self, other):
x0 = float(((self.x + other.x))/2)
y0 = float(((self.y + other.y)/2))
return Point(x0, y0)

def __iter__(self):
yield self.x
yield self.y

# the Chaos Game class: (0,0) is in the top-left corner
# Inherits from the Canvas class of Tkinter
class ChaosGame(Canvas):
def __init__(self, master):
Canvas.__init__(self, master, bg="white")
self.pack(fill=BOTH, expand=True)

# Plots the vertices and midpoints
def plotPoints(self, triangle, NumberPoints):
for point in triangle:
self.plot(point, color='red', r=3)

direction = center
for i in range(NumberPoints):
chosen_vertex = choice(triangle)
point_to_plot = chosen_vertex.midpoint(direction)
self.plot(point_to_plot)
direction = point_to_plot

def plot(self, point, color="black", r=1):
x, y = point
self.create_oval(x-r, y-r, x+r, y+r, outline=color, fill=color)

##########################################################
# ***DO NOT MODIFY OR REMOVE ANYTHING BELOW THIS POINT!***
# the default size of the canvas is 600x520
WIDTH = 600
HEIGHT = 520
# the number of points to plot
NumberPoints = 50000

# the vertices
A = Point(5, 510)
B = Point(290, 5)
C = Point(590, 510)
triangle = (A, B, C)
center = Point(WIDTH/2, HEIGHT/2)

# create the window
window = Tk()
window.geometry("{}x{}".format(WIDTH, HEIGHT))
window.title("The Chaos Game")
# create the chaos game as a Tkinter canvas inside the window
s = ChaosGame(window)
# plot some random points
s.plotPoints(triangle, NumberPoints)
# wait for the window to close
window.mainloop()
``````