0

我正在努力解决这个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)
4

2 回答 2

0

保持所有线条,在形成双鱼座形状的两个圆圈的交点内

在多次阅读你的问题并研究了这个数字之后,我相信我明白你想要什么。该解决方案与底层图形对象无关,而是与数学有关。我们需要找到割线与圆圈相交的位置以使它们成为和弦:

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

数学可能会被简化,但我已经写出来了,因为这不是我经常使用的东西。

输出

在此处输入图像描述

于 2017-06-26T17:40:07.300 回答
0

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

它可以绘制arc, chord, pie.

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

在此处输入图像描述

于 2016-12-11T11:07:32.640 回答