如何用python绘制三角形?

2024-05-19 02:50:27 发布

您现在位置: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()

谢谢!


Tags: 代码import类型randomwinpoint形状randint
1条回答
网友
1楼 · 发布于 2024-05-19 02:50:27

这应该创建一个具有随机顶点(角点)的三角形:

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)

我希望这能有帮助。

相关问题 更多 >

    热门问题