如何在Python中绘制三角形?
我想用Python画一个三角形。我已经会画圆形了,但三角形我不会。有人能帮我吗?
这是我画圆形的代码,我想用类似的代码来画三角形。
import graphics
import random
win=graphics.GraphWin("Exercise 7",500,500)
win.setBackground("white")
for i in range(1000):
x=random.randint(0,500)
y=random.randint(0,500)
z=random.randint(1,100)
point = graphics.Point(x,y)
circle=graphics.Circle(point,z)
colour=graphics.color_rgb(random.randint(0,255),
random.randint(0,255),
random.randint(0,255))
circle.setFill(colour)
circle.draw(win)
win.getMouse()
win.close()
谢谢!
1 个回答
3
这段代码应该会生成一个单独的三角形,三角形的三个角(顶点)是随机生成的。
vertices = []
for i in range(3): # Do this 3 times
x = random.randint(0, 500) # Create a random x value
y = random.randint(0, 500) # Create a random y value
vertices.append(graphics.Point(x, y)) # Add the (x, y) point to the vertices
triangle = graphics.Polygon(vertices) # Create the triangle
triangle.setFill(colour)
triangle.draw(win)
希望这对你有帮助。