2014-04-22 190 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