2014-12-03 132 views
1

如何在Python的Zelle圖形包中製作一個半圓?此代碼使我成爲一個圓圈。如何在Python中使用Zelle圖形制作半圓?

balldistance=40; 
ball1=Circle(Point(spacing*b+spacing-150,FieldHeight-GroundDepth),ball1); 
ball1.setFill("red"); 
ball1.draw(Field); 
+0

您使用的是哪種python圖形庫? – Gerrat 2014-12-03 03:27:47

+0

由半圓形,你的意思是一個弧? – 2014-12-03 03:27:57

+0

我正在使用python 3.2與圖形包。如果一個弧形成一個半圈,那麼是的。 – 2014-12-03 03:43:54

回答

1

Zelle圖形模塊不提供直接繪製半圓(圓弧)的代碼。但是,由於該模塊是用Python編寫的,建立在Tkinter的,和Tkinter的提供了一個圓弧繪製函數,我們可以添加自己的弧子類,從Zelle橢圓形類繼承並實現弧:

from graphics import * 

class Arc(Oval): 

    def __init__(self, p1, p2, extent): 
     self.extent = extent 
     super().__init__(p1, p2) 

    def __repr__(self): 
     return "Arc({}, {}, {})".format(str(self.p1), str(self.p2), self.extent) 

    def clone(self): 
     other = Arc(self.p1, self.p2, self.extent) 
     other.config = self.config.copy() 
     return other 

    def _draw(self, canvas, options): 
     p1 = self.p1 
     p2 = self.p2 
     x1, y1 = canvas.toScreen(p1.x, p1.y) 
     x2, y2 = canvas.toScreen(p2.x, p2.y) 
     options['style'] = tk.CHORD 
     options['extent'] = self.extent 
     return canvas.create_arc(x1, y1, x2, y2, options) 


win = GraphWin("My arc example", 200, 200) 

arc = Arc(Point(50, 50), Point(100, 100), 180) 
arc.setFill("red") 
arc.draw(win) 

win.getMouse() 
win.close() 

輸出

enter image description here