如何在python中绘制三角形?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23217965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Draw a triangle shape in python?
提问by user3515129
I want to draw the shape of a triangle using python. I have already drawn the shape of circle but I cannot draw the triangle. Could someone please help me with this?
我想使用python绘制三角形的形状。我已经画了圆形,但我不能画三角形。有人可以帮我解决这个问题吗?
This is my code for the circle and I want to use the same type of code for the triangle.
这是我的圆代码,我想对三角形使用相同类型的代码。
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()
Thanks!
谢谢!
采纳答案by monopole
This should create a single triangle, with random vertices (corners):
这应该创建一个带有随机顶点(角)的三角形:
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)
I hope this helps.
我希望这有帮助。