当前位置:   article > 正文

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

python等分圆弧

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

  1. import tkinter as tk
  2. import pyautogui as ag
  3. import random
  4. from time import sleep as Delay
  5. from math import sin
  6. from math import cos
  7. from math import pi
  8. from numpy import arange as np
  9. def Window_Open(W, H):
  10. X, Y = ag.size()
  11. winSize = str(W)+"x"+str(H)
  12. winPos = winSize + "+" + str((X - W) // 2)
  13. winPos += "+" + str((Y - H) // 2)
  14. win.geometry(winPos)
  15. win.resizable(False, False)
  16. title = u'桌面分辨率:' + str(X) + "x" + str(Y)
  17. title += ' ' * 5 + u'窗体大小:' + winSize
  18. win.title(title)
  19. win.update()
  20. if __name__ == '__main__':
  21. win = tk.Tk()
  22. Window_Open(480,480)
  23. tCanvas = tk.Canvas(win, width=win.winfo_width(), height=480, bg='white')
  24. tCanvas.pack(side="top")
  25. Color = ['red','blue','green','magenta','navy','lawngreen','orange']
  26. Rad = [60,45,30,15,10,5]
  27. for i in range(len(Rad)):
  28. step=Rad[i]*pi/180
  29. for t in np(-pi,pi,step):
  30. x = 240+200*cos(t)
  31. y = 240+200*sin(t)
  32. for r in np(-pi,pi,step):
  33. x0 = 240+200*cos(r)
  34. y0 = 240+200*sin(r)
  35. c = random.choice(Color)
  36. coord=x,y,x0,y0
  37. tCanvas.create_line(coord,fill=c)
  38. tCanvas.update()
  39. Delay(2)
  40. if i+1!=len(Rad):
  41. tCanvas.create_rectangle(0,0,480,480,outline='white',fill='white')
  42. win.mainloop()

效果图:

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

  1. import tkinter as tk
  2. import pyautogui as ag
  3. from time import sleep as Delay
  4. from math import sin
  5. from math import cos
  6. from math import pi
  7. from numpy import arange as np
  8. def Window_Open(W, H):
  9. X, Y = ag.size()
  10. winSize = str(W)+"x"+str(H)
  11. winPos = winSize + "+" + str((X - W) // 2)
  12. winPos += "+" + str((Y - H) // 2)
  13. win.geometry(winPos)
  14. win.resizable(False, False)
  15. title = u'桌面分辨率:' + str(X) + "x" + str(Y)
  16. title += ' ' * 5 + u'窗体大小:' + winSize
  17. win.title(title)
  18. win.update()
  19. if __name__ == '__main__':
  20. win = tk.Tk()
  21. Window_Open(480,480)
  22. tCanvas = tk.Canvas(win, width=win.winfo_width(), height=480, bg='white')
  23. tCanvas.pack(side="top")
  24. c_txt=tCanvas.create_text((225, 240),text='',anchor=tk.W, font=("宋体",20))
  25. step = pi
  26. for i in range(1,15):
  27. step/=2
  28. for t in np(-pi+pi/4,pi+pi/4,step):
  29. x = 240+200*cos(t)
  30. y = 240+200*sin(t)
  31. x0 = 240+200*cos(t+step)
  32. y0 = 240+200*sin(t+step)
  33. coord=x,y,x0,y0
  34. tCanvas.create_line(coord)
  35. tCanvas.itemconfig(c_txt, text=str(2**(i+1)))
  36. tCanvas.update()
  37. Delay(0.5)
  38. tCanvas.create_text((225, 280),text='End!',anchor=tk.W, font=("宋体",20))
  39. tCanvas.update()
  40. win.mainloop()

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/124805
推荐阅读
相关标签
  

闽ICP备14008679号