子类不画直线



我正在尝试构建一个从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(,你从乌龟的起始位置开始画画,而不是从你列表中的第一个位置开始。(你需要通过返回到第一个位置来关闭多边形。(

perimeter()函数中的数学运算似乎是错误的:

length = ((x1 - x2)**2 + (y1 - y2)**2)*0.5

这可能应该是**0.5来计算平方根,而不是一半。您忘记在__init__函数中调用super()

此外,您的draw_poly()正在调用模块turtle中的函数,而不是self上调用方法。这就是为什么我使用import Screen, Turtle而不是import turtle,只是为了避免这个错误。

以下是使用上述和其他修复程序对您的代码进行的返工:

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()

最新更新