我正在尝试通过这个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)
答案 0 :(得分:0)
保持所有线条,在两个圆圈的交叉点内 形成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()
数学可能会被简化,但我已经把它写出来,因为这不是我经常使用的东西。
输出
答案 1 :(得分:-1)
Graphics
在后台使用Tkinter
,它具有更多有用的功能。
它可以绘制arc
,chord
,pie
。
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()
BTW:使用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()