2013-11-29 42 views
2

我一直在嘗試使用Python複製此設計。在Python中複製此設計

我正在使用Graphics模塊,obtained here。目前我無法使用任何其他圖形模塊。

此處的代碼允許我在一個循環重複的行中繪製5個圓圈。我已經用這個代碼中發現

def fdShape(): 
    win = GraphWin(199,199) 
    centre = Point(20,100) 
    for y in range(5): 
     for x in range(5): 
      centre = Point(x * 40 + 20, y * 40 + 60) 
      circle = Circle(centre, 20) 
      circle.setOutline("red") 
      circle.draw(win) 

的一個問題是,它讓在窗口頂部的空行,並把最後環線底部,超出了窗口的邊界。這是第一個問題。

第二個是使用代碼來顯示以紅色顯示的半圓。正如您在本頁頂部的圖片中看到的那樣。我不確定如何使用Python複製此圖片。

謝謝!

+0

你用什麼版本的Python? –

回答

0

這看起來相當接近:

screenshot of program's output

from graphic import * 

def main(): 
    repeat = 5 
    diameter = 40 
    radius = diameter // 2 
    offset = radius // 2 

    win = GraphWin("Graphic Design", diameter*repeat + offset, diameter*repeat) 
    win.setBackground('white') 

    for i in range(repeat): 
     for j in range(repeat): 
      draw_symbol(win, i % 2, 
         Point(i*diameter + offset, j*diameter), radius, 'red') 

    win.getMouse() 
    win.close() 

def draw_symbol(win, kind, lower_left, radius, colour): 
    centre = Point(lower_left.x+radius, lower_left.y+radius) 
    circle = Circle(centre, radius) 
    circle.setOutline('') 
    circle.setFill(colour) 
    circle.draw(win) 

    if kind == 0: 
     rectangle = Rectangle(lower_left, 
          Point(lower_left.x+radius, lower_left.y+radius*2)) 
    else: 
     rectangle = Rectangle(lower_left, 
          Point(lower_left.x+radius*2, lower_left.y+radius)) 
    rectangle.setOutline('white') 
    rectangle.setFill('white') 
    rectangle.draw(win) 

    circle = Circle(centre, radius) 
    circle.setWidth(1) 
    circle.setOutline(colour) 
    circle.setFill('') 
    circle.draw(win) 

main() 
+0

非常好,非常感謝。做得好! – user1769878

0

我看到兩個問題。

GraphWin應初始化爲200x200,而不是199x199。

這條線:

centre = Point(x * 40 + 20, y * 40 + 60) 

應該最有可能是:

centre = Point(x * 40 + 20, y * 40 + 20) 
+0

這也幫助我獲得額外的圈子。謝謝! – user1769878