Python I';我正在尝试生成Sierpinski三角形的输出。我想知道如何在2d point类中使用midpt函数来实现这个输出?
我必须使用2d点类和tkinter画布生成Sierpinski三角形。midpt函数本质上是从随机选择的顶点和最后绘制的中点获取输入。可以选择任何顶点来打印第一个中点。这些点还需要是二维点类的实例。我迫切需要帮助,因为我似乎无法解决这个问题。下面是输出应该是什么样子。 这是我到目前为止所做的,但是它没有使用2d point类来生成三角形Python I';我正在尝试生成Sierpinski三角形的输出。我想知道如何在2d point类中使用midpt函数来实现这个输出?,python,python-2.7,class,tkinter-canvas,fractals,Python,Python 2.7,Class,Tkinter Canvas,Fractals,我必须使用2d点类和tkinter画布生成Sierpinski三角形。midpt函数本质上是从随机选择的顶点和最后绘制的中点获取输入。可以选择任何顶点来打印第一个中点。这些点还需要是二维点类的实例。我迫切需要帮助,因为我似乎无法解决这个问题。下面是输出应该是什么样子。 这是我到目前为止所做的,但是它没有使用2d point类来生成三角形 import math from fractions import Fraction from random import randrange from Tk
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"]
RADIUS = 0
color = POINT_COLORS
self.create_oval(x, y, x+2, y+2, outline = color, fill = color)
def plotV(self, x, y):
POINT_COLORS=["red"]
RADIUS = 3
color = POINT_COLORS
self.create_oval(x, y, x+RADIUS*2, y+RADIUS*2, 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 = (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()
问题是,您在
plotPoints()
方法中的midpt()
方法中复制了基于点的代码,但复制的是元组而不是点。由于三角形作为元组列表交给我们,因此我们将其转换为点列表,并修改plot()
方法以接受点,并根据点运行整个plotPoints()
方法:
from random import choice
from Tkinter import *
class Point(object):
''' a 2D point class '''
def __init__(self, x=0, y=0):
self.x = float(x)
self.y = float(y)
# Mutators and Accessors
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = float(value)
@property
def y(self):
return self._y
@y.setter
def y(self, value):
self._y = float(value)
# String function
def __str__(self):
return "({},{})".format(self.x, self.y)
# Midpoint function
def midpt(self, other):
x0 = (self.x + other.x) / 2
y0 = (self.y + other.y) / 2
return Point(x0, y0)
class ChaosGame(Canvas):
''' Plotting class that inherits from Canvas class of Tkinter '''
VERTEX_RADIUS = 3
VERTEX_COLORS = ["red"]
POINT_RADIUS = 1
POINT_COLORS = ["black"]
def __init__(self, master):
Canvas.__init__(self, master, bg="white")
self.pack(fill=BOTH, expand=1)
def plotPoints(self, triangle, NumberPoints):
point = Point(WIDTH / 2, HEIGHT / 2)
deltoid = [Point(x, y) for x, y in triangle] # tuples -> Points
for _ in range(NumberPoints):
point = point.midpt(choice(deltoid))
self.plot(point)
for vertex in deltoid:
self.plotV(vertex)
def plot(self, point):
radius = self.POINT_RADIUS
color = self.POINT_COLORS
x, y = point.x, point.y
self.create_oval(x, y, x + radius*2, y + radius*2, outline=color, fill=color)
def plotV(self, vertex):
radius = self.VERTEX_RADIUS
color = self.VERTEX_COLORS
x, y = vertex.x, vertex.y
self.create_oval(x, y, x + radius*2, y + radius*2, outline=color, fill=color)
除此之外的东西:
##########################################################
# ***DO NOT MODIFY OR REMOVE ANYTHING BELOW THIS POINT!***
这里没有重复。我还处理了您的Point方法中的一些古怪之处,他们坚持将数字转换为字符串并返回到数字,例如:
x0 = float(str(((self.x + other.x))))/2
而是:
x0 = (self.x + other.x) / 2
当点
创建时,强制self.x
成为一个float
。我必须向Point类添加def\uu iter\uuuuu(self)以使点可编辑。除此之外,它工作得非常好。