2016-12-09 22 views
0

塑造我想通過這個Vesica Piscis形狀 有我疑惑地工作。我嘗試了不同的方法,因爲按代碼順序繪製對象的方式爲 ,我也試圖讓白色圓圈填充 ,但我們不能只將圓的一部分着色。Vesica南魚座中Graphics.py

截至目前,我被困在我應該嘗試什麼,這就是 爲什麼我打開自己的建議。

我聲明,我想實現的是有一個空白 保持所有的線,這兩個圓 形成Vesica南魚形狀的交集之內。

This is what i've done so far.

from graphics import * 

def canvas(): 

    win = GraphWin("Patch", 100, 100) 

    for i in range(10): 

     lineSet1 = Line(Point(0, 0), Point((i+1)*10, 100)) 
     lineSet1.draw(win) 

     lineSet2 = Line(Point(0, 0), Point(100, (i+1)*10)) 
     lineSet2.draw(win) 

     lineSet3 = Line(Point(100,100), Point(0, 100-(i+1)*10)) 
     lineSet3.draw(win) 

     lineSet4 = Line(Point(100,100), Point(100-(i+1)*10, 0)) 
     lineSet4.draw(win) 

    circle1 = Circle(Point(0, 100), 100) 
    circle1.setOutline("red") 
    circle1.draw(win) 

    circle2 = Circle(Point(100, 0), 100) 
    circle2.setOutline("blue") 
    circle2.draw(win) 
+0

是什麼'Vesica南魚座shape'?添加鏈接到一些描述和圖像。 – furas

回答

0

Graphics使用Tkinter在背景,其具有多個有用的功能。

它可以繪製arc,chord,pie

Tkinter的:Canvasmore

from graphics import * 

# --- constants --- 

WIDTH = 300 
HEIGHT = 300 

# --- main ---- 

win = GraphWin("Patch", WIDTH, HEIGHT) 

bbox = (5, 5, WIDTH-5, HEIGHT-5) 

win.create_arc(bbox, fill="red", outline='green', width=3, start=0, extent=90, style='arc') 
win.create_arc(bbox, fill="red", outline='green', width=3, start=95, extent=90, style='chord') 
win.create_arc(bbox, fill="red", outline='green', width=3, start=190, extent=90, style='pieslice') 

# --- wait for mouse click --- 

#win.getKey() 
win.getMouse() 

win.close() 

enter image description here


順便說一句:使用win.after(miliseconds, function_name)定期地執行功能,其移動對象。

from graphics import * 

# --- constants --- 

WIDTH = 300 
HEIGHT = 300 

# --- functions --- 

def moves(): 

    # move figure 1  
    s = win.itemcget(fig1, 'start')  # get option 
    win.itemconfig(fig1, start=float(s)+5) # set option 

    # move figure 2 
    s = win.itemcget(fig2, 'start') 
    win.itemconfig(fig2, start=float(s)+5) 

    # move figure 3 
    s = win.itemcget(fig3, 'start') 
    win.itemconfig(fig3, start=float(s)+5) 

    # run again after 100ms (0.1s) 
    win.after(100, moves) 

# --- main ---- 

win = GraphWin("Patch", WIDTH, HEIGHT) 

bbox = (5, 5, WIDTH-5, HEIGHT-5) 

fig1 = win.create_arc(bbox, fill="red", outline='green', width=3, start=0, extent=90, style='arc') 
fig2 = win.create_arc(bbox, fill="red", outline='green', width=3, start=95, extent=90, style='chord') 
fig3 = win.create_arc(bbox, fill="red", outline='green', width=3, start=190, extent=90, style='pieslice') 

# run first time 
moves() 

#win.getKey() 
win.getMouse() 

win.close() 

編輯:

from graphics import * 

# --- constants --- 

WIDTH = 300 
HEIGHT = 300 

# --- main ---- 

win = GraphWin("Patch", WIDTH, HEIGHT) 

win.create_arc((0, -75, 300, 300-75), fill="blue", outline="blue", extent=120, style='chord', start=30+180) 
win.create_arc((0, 75, 300, 300+75), fill="blue", outline="blue", extent=120, style='chord', start=30) 

win.create_oval((100, 100, 200, 200), fill="white", outline="white") 
win.create_oval((130, 130, 170, 170), fill="black", outline="black") 

#win.getKey() 
win.getMouse() 

win.close() 

enter image description here

0

保持所有的行,這兩個圓 形成的交叉點內的Vesica南魚形狀

經過多次閱讀您的問題和研究圖,我相信我明白你想要什麼。該解決方案與底層圖形對象無關,而是數學。我們需要找到的割線相交的圓圈,以使它們成爲和絃:

from graphics import * 

def intersection(center, radius, p1, p2): 

    dx, dy = p2.x - p1.x, p2.y - p1.y 

    a = dx**2 + dy**2 
    b = 2 * (dx * (p1.x - center.x) + dy * (p1.y - center.y)) 
    c = (p1.x - center.x)**2 + (p1.y - center.y)**2 - radius**2 

    discriminant = b**2 - 4 * a * c 
    assert (discriminant > 0), 'Not a secant!' 

    t = (-b + discriminant**0.5)/(2 * a) 

    x = dx * t + p1.x 
    y = dy * t + p1.y 

    return Point(x, y) 

def canvas(win): 
    radius = 100 

    center = Point(0, 100) # Red circle 

    circle = Circle(center, radius) 
    circle.setOutline('red') 
    circle.draw(win) 

    for i in range(1, 10 + 1): 

     p1 = Point(0, 0) 
     p2 = Point(100, i * 10) 
     p3 = intersection(center, radius, p1, p2) 

     Line(p1, p3).draw(win) 

     p1 = Point(100, 100) 
     p2 = Point(100 - i * 10, 0) 
     p3 = intersection(center, radius, p1, p2) 

     Line(p1, p3).draw(win) 

    center = Point(100, 0) # Blue circle 

    circle = Circle(center, radius) 
    circle.setOutline('blue') 
    circle.draw(win) 

    for i in range(1, 10 + 1): 

     p1 = Point(0, 0) 
     p2 = Point(i * 10, 100) 
     p3 = intersection(center, radius, p1, p2) 

     Line(p1, p3).draw(win) 

     p1 = Point(100, 100) 
     p2 = Point(0, 100 - i * 10) 
     p3 = intersection(center, radius, p1, p2) 

     Line(p1, p3).draw(win) 

win = GraphWin('Patch', 100, 100) 

canvas(win) 

win.getMouse() 

數學可能可以簡化,但我寫出來,因爲這是不是我經常工作。

輸出

enter image description here