Python 海龟子类不';不要画一条直线
我正在尝试构建一个继承自Python 海龟子类不';不要画一条直线,python,turtle-graphics,python-turtle,Python,Turtle Graphics,Python Turtle,我正在尝试构建一个继承自turtle.turtle类的子类,并希望创建一个自动绘制多边形的函数。但我发现初始线总是有点倾斜。 我不知道哪里出了问题 代码如下: import turtle class Polygon(turtle.Turtle): def __init__(self, point_list): self.point_list = point_list def add_point(self, point): self.point_
turtle.turtle
类的子类,并希望创建一个自动绘制多边形的函数。但我发现初始线总是有点倾斜。
我不知道哪里出了问题
代码如下:
import turtle
class Polygon(turtle.Turtle):
def __init__(self, point_list):
self.point_list = point_list
def add_point(self, point):
self.point_list.append(point)
return self.point_list
def over_write_points(self, new_points):
self.point_list = new_points
return self.point_list
def perimeter(self):
length_peri = 0
for i in range(len(self.point_list)):
point1 = self.point_list[i-1]
point2 = self.point_list[i]
x1, y1 = point1
x2, y2 = point2
length = ((x1 - x2)**2 + (y1 - y2)**2)*0.5
length_peri += length
return length_peri
def area(self):
area = 0
for i in range(len(self.point_list)):
point1 = self.point_list[i-1]
point2 = self.point_list[i]
x1, y1 = point1
x2, y2 = point2
trapezoid = ((x2 - x1) * (y1 + y2)) / 2
area = area + trapezoid
area = abs(area)
return area
def bound_points(self):
unzip_list = list(zip(*self.point_list))
x_list = unzip_list[0]
y_list = unzip_list[1]
bound1 = max(x_list), min(y_list)
bound2 = max(x_list), max(y_list)
bound3 = min(x_list), max(y_list)
bound4 = min(x_list), min(y_list)
bound_points = [bound1, bound2, bound3, bound4]
return bound_points
def move_poly(self, dx, dy):
new_point_list = []
for i in self.point_list:
x = i[0] + dx
y = i[1] + dy
new_point = (x, y)
new_point_list.append(new_point)
self.point_list = new_point_list
return self.point_list
def draw_poly(self, lineColour="green", fillColour="yellow"):
start = self.point_list[-1]
turtle.pencolor(lineColour)
turtle.fillcolor(fillColour)
turtle.penup()
turtle.pendown()
turtle.begin_fill()
x, y = start
for point in self.point_list: # go through a list of points
dx, dy = point
turtle.goto(x + dx, y + dy)
turtle.end_fill()
turtle.penup()
turtle.mainloop()
return f'The polygon is finished'
test_polygon = Polygon([(50,0), (50,50), (0,50)])
print(test_polygon.add_point((0, 0)))
print(test_polygon.over_write_points([(100,0), (100,100), (0,100), (0,0)]))
print(test_polygon.perimeter())
print(test_polygon.area())
print(test_polygon.bound_points())
print(test_polygon.move_poly(-10,-10))
print(test_polygon.draw_poly())
您的代码中有几个问题。首先,正如@martineau所指出的(+1),您从海龟的原始位置开始绘制,而不是从列表中的第一个位置开始绘制。(您需要通过返回到第一个位置来关闭多边形。)
permiture()
函数中的数学似乎有误:
length = ((x1 - x2)**2 + (y1 - y2)**2)*0.5
计算平方根可能应该是**0.5
,而不是一半。您忘了在\uuu init\uuu
函数中调用super()
另外,您的draw\u poly()
正在调用模块turtle
中的函数,而不是调用self
上的方法。这就是为什么我使用导入屏幕,而不是导入海龟
,只是为了避免这个错误
以下是使用上述修复程序和其他修复程序对代码进行的返工:
from turtle import Turtle
class Polygon(Turtle):
def __init__(self, point_list):
super().__init__()
self.point_list = point_list
def add_point(self, point):
self.point_list.append(point)
return self.point_list
def over_write_points(self, new_points):
self.point_list[:] = new_points # reload existing list
return self.point_list
def perimeter(self):
length_peri = 0
for i in range(len(self.point_list)):
point1 = self.point_list[i - 1]
point2 = self.point_list[i]
x1, y1 = point1
x2, y2 = point2
length = ((x1 - x2)**2 + (y1 - y2)**2)**0.5
length_peri += length
return length_peri
def area(self):
area = 0
for i in range(len(self.point_list)):
point1 = self.point_list[i - 1]
point2 = self.point_list[i]
x1, y1 = point1
x2, y2 = point2
trapezoid = ((x2 - x1) * (y1 + y2)) / 2
area += trapezoid
return abs(area)
def bound_points(self):
unzip_list = list(zip(*self.point_list))
x_list = unzip_list[0]
y_list = unzip_list[1]
bound1 = max(x_list), min(y_list)
bound2 = max(x_list), max(y_list)
bound3 = min(x_list), max(y_list)
bound4 = min(x_list), min(y_list)
return [bound1, bound2, bound3, bound4]
def move_poly(self, dx, dy):
new_point_list = []
for x, y in self.point_list:
new_point = (x + dx, y + dy)
new_point_list.append(new_point)
return self.over_write_points(new_point_list)
def draw_poly(self, lineColour='green', fillColour='yellow'):
self.pencolor(lineColour)
self.fillcolor(fillColour)
start, *remaining_points = self.point_list
self.penup()
self.goto(start)
self.pendown()
self.begin_fill()
for point in remaining_points: # go through a list of points
self.goto(point)
self.goto(start)
self.end_fill()
self.penup()
return 'The polygon is finished'
if __name__ == '__main__':
from turtle import Screen
screen = Screen()
test_polygon = Polygon([(50, 0), (50, 50), (0, 50)])
print(test_polygon.add_point((0, 0)))
print(test_polygon.over_write_points([(100, 0), (100, 100), (0, 100), (0, 0)]))
print(test_polygon.perimeter(), "pixels")
print(test_polygon.area(), "pixels squared")
print(test_polygon.bound_points())
print(test_polygon.move_poly(-10, -10))
print(test_polygon.draw_poly())
screen.exitonclick()
请参见如何创建一个。尽可能多地删除代码,但仍然会使其不符合您的期望。您永远不会将海龟
移动到draw_poly()
方法中的初始start
位置(笔朝上),谢谢您的回答。这是非常清楚和有益的。事实上,我还有很多东西要学。我会继续练习!我建议用它来计算长度。它更快,更不容易出错。