2014-04-22 193 views
-1

我想用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

你是否认为三角形是一个三面的'多边形'? – jonrsharpe

+0

那么我该如何使用那个..你能说明一下还是在上面的程序中使用那个函数? – user3515129

+3

阅读[文档](http://mcsp.wartburg.edu/zelle/python/graphics/graphics/node9.html)。这不是一个代码写入服务。 – jonrsharpe

回答

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) 

我希望这有助于。

+1

谢谢你,我真棒.. Actuall我不使用你给我的方式BT我有想法..非常感谢! – user3515129