使用python和pygame繪制繁花曲線


  前段時間看了一期《最強大腦》,里面展示了各種繁花曲線組合成的非常美麗的圖形,一時心血來潮,想嘗試自己用代碼繪制繁花曲線,想怎么組合就怎么組合。

  

  真實的繁花曲線使用一種稱為繁花曲線規的小玩意繪制,繁花曲線規由相互契合的大小兩個圓組成,用筆插在小圓上的一個孔中,緊貼大圓的內壁滾動,就可以繪制出漂亮的圖案。

  這個過程可以做一個抽象:有兩個半徑不相等的圓,大圓位置固定,小圓在大圓內部,小圓緊貼着大圓內壁滾動,求小圓上的某一點走過的軌跡。

  進一步分析,小圓的運動可以分解為兩個部分:小圓圓心繞大圓圓心公轉、小圓繞自身圓心自轉。

  設大圓圓心為A,半徑為Ra,小圓圓心為B,半徑為Rb,軌跡點為C,半徑為Rc(BC距離),設小圓公轉的弧度為 θ [0, ∞),如圖:

  

 

  因為大圓的圓心坐標是固定的,要求得小圓上的某點的軌跡,就需要先求出小圓在當前時刻的圓心坐標,再求出小圓自轉的弧度,最后求出小圓上某點的坐標。

 

  第一步:求小圓圓心坐標

  小圓圓心(xb, yb)繞大圓圓心(xa, ya)公轉,公轉軌跡是一個半徑為 R- RB 的圓。求小圓圓心坐標,相當於是求半徑為 R- RB 的圓上 θ 弧度對應的點的坐標。

  圓上的點的坐標公式為:

  x = r * cos(θ), y = r * sin(θ)

  所以小圓圓心坐標(xb, yb)為:( xa + (Ra - Rb) * cos(θ),  ya + (Ra - Rb) * sin(θ) )

  

  第二步:求小圓自轉弧度

  設小圓自轉弧度為α,小圓緊貼大圓運動,兩者走過的路程相同,因此有:

  Ra * θ = Rb * α

  小圓自轉弧度 α = (Ra / Rb) * θ

  

  第三步:求點C坐標

  點C相對小圓圓心B的公轉軌跡是一個半徑為 Rc 的圓,計算方法同第一步,有:

  軌跡點C的坐標(xc, yc)為:( xb + Rc * cos(α),  yb + Rc * sin(α) )

 

  按照以上算法分析,用python代碼實現如下:

 1 # -*- coding: utf-8 -*-
 2 
 3 import math
 4 
 5 '''
 6 功能:
 7     已知圓的圓心和半徑,獲取某弧度對應的圓上點的坐標
 8 入參:
 9     center:圓心
10     radius:半徑
11     radian:弧度
12 '''
13 def get_point_in_circle(center, radius, radian):
14     return (center[0] + radius * math.cos(radian), center[1] - radius * math.sin(radian))
15 
16 '''
17 功能:
18     內外圓A和B,內圓A沿着外圓B的內圈滾動,已知外圓圓心、半徑,已知內圓半徑,已知公轉弧度和繞點半徑,計算繞點坐標
19 入參:
20     center_A:外圓圓心
21     radius_A:外圓半徑
22     radius_B:內圓半徑
23     radius_C:繞點半徑
24     radian:公轉弧度
25 '''
26 def get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, radian):
27     # 計算內圓圓心坐標
28     center_B = get_point_in_circle(center_A, radius_A - radius_B, radian)
29     # 計算繞點弧度(公轉為逆時針,則自轉為順時針)
30     radian_C = 2.0 * math.pi - ((radius_A / radius_B * radian) % (2.0 * math.pi))
31     # 計算繞點坐標
32     return get_point_in_circle(center_B, radius_C, radian_C)

  有兩點需要注意:

  (1)屏幕坐標系左上角為原點,垂直向下為Y正軸,與數學坐標系Y軸方向相反,所以第14行Y坐標為減法;

  (2)默認公轉為逆時針,則自轉為順時針,所以第30行求自轉弧度時,使用了2π - α%(2π);

 

  坐標已經計算出來,接下來使用pygame繪制。曲線軌跡的繪制思想是以0.01弧度為一個步長,不斷計算出新的坐標,把一系列坐標連起來就會形成軌跡圖。

  為了能夠形成一個封閉圖形,還需要知道繪制點什么時候會重新回到起點。想了一個辦法,以X軸正半軸為基准線,每次繪制點到達基准線,計算此時繪制點與起點的距離,達到一定精度認為已經回到起點,形成封閉圖形。

 1 ''' 計算兩點距離(平方和) '''
 2 def get_instance(p1, p2):
 3     return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])
 4     
 5 '''
 6 功能:
 7     獲取繞點路徑的所有點的坐標
 8 入參:
 9     center:外圓圓心
10     radius_A:外圓半徑
11     radius_B:內圓半徑
12     radius_C:繞點半徑
13     shift_radian:每次偏移的弧度,默認0.01,值越小,精度越高,計算量越大
14 '''
15 def get_points(center, radius_A, radius_B, radius_C, shift_radian=0.01):
16     # 轉為實數
17     radius_A *= 1.0
18     radius_B *= 1.0
19     radius_C *= 1.0
20     
21     P2 = 2*math.pi # 一圈的弧度為 2PI
22     R_PER_ROUND = int(P2/shift_radian/4) + 1 # 一圈需要走多少步(弧度偏移多少次)
23     
24     # 第一圈的起點坐標
25     start_point = get_point_in_child_circle(center, radius_A, radius_B, radius_C, 0)
26     points = [start_point]
27     # 第一圈的路徑坐標
28     for r in range(1, R_PER_ROUND):
29         points.append(get_point_in_child_circle(center, radius_A, radius_B, radius_C, shift_radian*r))
30     
31     # 以圈為單位,每圈的起始弧度為 2PI*round,某圈的起點坐標與第一圈的起點坐標距離在一定范圍內,認為路徑結束
32     for round in range(1, 100):
33         s_radian = round*P2
34         s_point = get_point_in_child_circle(center, radius_A, radius_B, radius_C, s_radian)
35         if get_instance(s_point, start_point) < 0.1:
36             break
37         points.append(s_point)
38         for r in range(1, R_PER_ROUND):
39             points.append(get_point_in_child_circle(center, radius_A, radius_B, radius_C, s_radian + shift_radian*r))
40         
41     return points

 

  再加上繪制代碼,完整代碼如下:

  1 # -*- coding: utf-8 -*-
  2 
  3 import math
  4 import random
  5 
  6 '''
  7 功能:
  8     已知圓的圓心和半徑,獲取某弧度對應的圓上點的坐標
  9 入參:
 10     center:圓心
 11     radius:半徑
 12     radian:弧度
 13 '''
 14 def get_point_in_circle(center, radius, radian):
 15     return (center[0] + radius * math.cos(radian), center[1] - radius * math.sin(radian))
 16 
 17 '''
 18 功能:
 19     內外圓A和B,內圓A沿着外圓B的內圈滾動,已知外圓圓心、半徑,已知內圓半徑、公轉弧度,已知繞點半徑,計算繞點坐標
 20 入參:
 21     center_A:外圓圓心
 22     radius_A:外圓半徑
 23     radius_B:內圓半徑
 24     radius_C:繞點半徑
 25     radian:公轉弧度
 26 '''
 27 def get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, radian):
 28     # 計算內圓圓心坐標
 29     center_B = get_point_in_circle(center_A, radius_A - radius_B, radian)
 30     # 計算繞點弧度(公轉為逆時針,則自轉為順時針)
 31     radian_C = 2.0*math.pi - ((radius_A / radius_B * radian) % (2.0*math.pi))
 32     # 計算繞點坐標
 33     center_C = get_point_in_circle(center_B, radius_C, radian_C)
 34     center_B_Int = (int(center_B[0]), int(center_B[1]))
 35     return center_B_Int, center_C
 36 
 37 ''' 計算兩點距離(平方和) '''
 38 def get_instance(p1, p2):
 39     return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])
 40     
 41 '''
 42 功能:
 43     獲取繞點路徑的所有點的坐標
 44 入參:
 45     center:外圓圓心
 46     radius_A:外圓半徑
 47     radius_B:內圓半徑
 48     radius_C:繞點半徑
 49     shift_radian:每次偏移的弧度,默認0.01,值越小,精度越高,計算量越大
 50 '''
 51 def get_points(center_A, radius_A, radius_B, radius_C, shift_radian=0.01):
 52     # 轉為實數
 53     radius_A *= 1.0
 54     radius_B *= 1.0
 55     radius_C *= 1.0
 56     
 57     P2 = 2*math.pi # 一圈的弧度為 2PI
 58     R_PER_ROUND = int(P2/shift_radian) + 1 # 一圈需要走多少步(弧度偏移多少次)
 59     
 60     # 第一圈的起點坐標
 61     start_center, start_point = get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, 0)
 62     points = [start_point]
 63     centers = [start_center]
 64     # 第一圈的路徑坐標
 65     for r in range(1, R_PER_ROUND):
 66         center, point = get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, shift_radian*r)
 67         points.append(point)
 68         centers.append(center)
 69     
 70     # 以圈為單位,每圈的起始弧度為 2PI*round,某圈的起點坐標與第一圈的起點坐標距離在一定范圍內,認為路徑結束
 71     for round in range(1, 100):
 72         s_radian = round*P2
 73         s_center, s_point = get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, s_radian)
 74         if get_instance(s_point, start_point) < 0.1:
 75             break
 76         points.append(s_point)
 77         centers.append(s_center)
 78         for r in range(1, R_PER_ROUND):
 79             center, point = get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, s_radian + shift_radian*r)
 80             points.append(point)
 81             centers.append(center)
 82     
 83     print(len(points)/R_PER_ROUND)
 84         
 85     return centers, points
 86 
 87 import pygame
 88 from pygame.locals import *
 89 
 90 pygame.init()
 91 screen = pygame.display.set_mode((600, 400))
 92 clock = pygame.time.Clock()
 93 
 94 color_black = (0, 0, 0)
 95 color_white = (255, 255, 255)
 96 color_red = (255, 0, 0)
 97 color_yello = (255, 255, 0)
 98 
 99 center = (300, 200)
100 radius_A = 150
101 radius_B = 110
102 radius_C = 50
103 
104 test_centers, test_points = get_points(center, radius_A, radius_B, radius_C)
105 test_idx = 2
106 draw_point_num_per_tti = 5
107 
108 while True:
109     for event in pygame.event.get():
110         if event.type==pygame.QUIT:
111             pygame.quit() 
112             exit(0)
113     
114     screen.fill(color_white)
115     
116     pygame.draw.circle(screen, color_black, center, int(radius_A), 2)
117     
118     if test_idx <= len(test_points):
119         pygame.draw.aalines(screen, (0, 0, 255), False, test_points[:test_idx], 1)
120         if test_idx < len(test_centers):
121             pygame.draw.circle(screen, color_black, test_centers[test_idx], int(radius_B), 1)
122             pygame.draw.aaline(screen, color_black, test_centers[test_idx], test_points[test_idx], 1)
123         test_idx = min(test_idx + draw_point_num_per_tti, len(test_points))
124     
125     clock.tick(50)
126     pygame.display.flip()

   

  關於pygame的使用,參考博客 eyehere.net/2011/python-pygame-novice-professional-index/

 

  效果:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM