Python tkinter库之Canvas 直线等分圆弧(割圆术)

直线等分圆周,分隔得越多越接近于圆:

import tkinter as tk
import pyautogui as ag
import random
from time import sleep as Delay
from math import sin
from math import cos
from math import pi
from numpy import arange as np

def Window_Open(W, H):
    X, Y = ag.size()
    winSize = str(W)+"x"+str(H)
    winPos = winSize + "+" + str((X - W) // 2)
    winPos += "+" + str((Y - H) // 2)
    win.geometry(winPos)
    win.resizable(False, False)
    title = u'桌面分辨率:' + str(X) + "x" + str(Y)
    title += ' ' * 5 + u'窗体大小:' + winSize
    win.title(title)
    win.update()

if __name__ == '__main__':
    
    win = tk.Tk()
    Window_Open(480,480)
    tCanvas = tk.Canvas(win, width=win.winfo_width(), height=480, bg='white')
    tCanvas.pack(side="top")

    Color = ['red','blue','green','magenta','navy','lawngreen','orange']
    Rad = [60,45,30,15,10,5]
    
    for i in range(len(Rad)):
        step=Rad[i]*pi/180
        for t in np(-pi,pi,step):
            x = 240+200*cos(t)
            y = 240+200*sin(t)
            for r in np(-pi,pi,step):
                x0 = 240+200*cos(r)
                y0 = 240+200*sin(r)
                c = random.choice(Color)
                coord=x,y,x0,y0
                tCanvas.create_line(coord,fill=c)
            tCanvas.update()
        Delay(2)
        if i+1!=len(Rad):
            tCanvas.create_rectangle(0,0,480,480,outline='white',fill='white')
    win.mainloop()

效果图:

 改进一下代码:只连接相邻分隔点,加快程序速度;分割圆周2^15=32768等份,按照“割圆术”来算pi的话可以精确到3.1415926了。

import tkinter as tk
import pyautogui as ag
from time import sleep as Delay
from math import sin
from math import cos
from math import pi
from numpy import arange as np

def Window_Open(W, H):
    X, Y = ag.size()
    winSize = str(W)+"x"+str(H)
    winPos = winSize + "+" + str((X - W) // 2)
    winPos += "+" + str((Y - H) // 2)
    win.geometry(winPos)
    win.resizable(False, False)
    title = u'桌面分辨率:' + str(X) + "x" + str(Y)
    title += ' ' * 5 + u'窗体大小:' + winSize
    win.title(title)
    win.update()

if __name__ == '__main__':
    
    win = tk.Tk()
    Window_Open(480,480)
    tCanvas = tk.Canvas(win, width=win.winfo_width(), height=480, bg='white')
    tCanvas.pack(side="top")
    c_txt=tCanvas.create_text((225, 240),text='',anchor=tk.W, font=("宋体",20))
    step = pi
    for i in range(1,15):
        step/=2
        for t in np(-pi+pi/4,pi+pi/4,step):
            x = 240+200*cos(t)
            y = 240+200*sin(t)
            x0 = 240+200*cos(t+step)
            y0 = 240+200*sin(t+step)
            coord=x,y,x0,y0
            tCanvas.create_line(coord)
        tCanvas.itemconfig(c_txt, text=str(2**(i+1)))
        tCanvas.update()
        Delay(0.5)
    
    tCanvas.create_text((225, 280),text='End!',anchor=tk.W, font=("宋体",20))
    tCanvas.update()
    win.mainloop()

其中:函数 itemconfig( id, text='变更的内容') 可以在后期修改文本内容

c_txt = tCanvas.create_text((225, 240),text='变更前的文字',anchor=tk.W, font=("宋体",20))

tCanvas.itemconfig(c_txt, text='变更后的文字')

 效果图: (分到256份后已看不出变化了)

猜你喜欢

转载自blog.csdn.net/boysoft2002/article/details/115359958