簡體   English   中英

Graphics.py中的Vesica Piscis形狀

[英]Vesica Piscis shape in Graphics.py

我正在嘗試解決讓我感到困惑的Vesica Piscis形狀。 我嘗試了不同的方法,因為對象是按代碼順序繪制的,並且還嘗試將圓圈填充為白色,但是我們不能僅將圓圈的一部分着色。

到目前為止,我一直在嘗試應該做的事情,這就是為什么我對建議持開放態度。

我聲明我要實現的目標是在形成Vesica Piscis形狀的兩個圓的交點內保留所有線條的空白。

這是我到目前為止所做的。

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)

保持所有線條在形成Vesica Piscis形狀的兩個圓的交點內

在對您的問題進行多次閱讀並研究了數字之后,我相信我了解您想要什么。 該解決方案與基礎圖形對象無關,而與數學無關。 我們需要找到割線與圓相交的位置,使它們成為和弦:

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()

數學可能會簡化,但是我已經寫出來了,因為這不是我經常使用的東西。

輸出值

在此處輸入圖片說明

Graphics在后台使用Tkinter ,具有更多有用的功能。

它可以畫arcchordpie

Tkinter的: 帆布更多

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()

在此處輸入圖片說明


順便說一句:使用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()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM