打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用python和pygame绘制繁花曲线

  前段时间看了一期《最强大脑》,里面各种繁花曲线组合成了非常美丽的图形,一时心血来潮,想尝试自己用代码绘制繁花曲线,想怎么组合就怎么组合。

  

  真实的繁花曲线使用一种称为繁花曲线规的小玩意绘制,繁花曲线规由相互契合大小两个圆组成,用笔插在小圆上的一个孔中,紧贴大圆的内壁滚动,就可以绘制出漂亮的图案。这个过程可以做一个抽象:有两个半径不相等的圆,大圆位置固定,小圆在大圆内部,小圆紧贴着大圆内壁滚动,求小圆上的某一点走过的轨迹。

  进一步分析,小圆的运动可以分解为两个部分:小圆圆心绕大圆圆心公转、小圆绕自身圆心自转。设大圆圆心为A,半径为Ra,小圆圆心为B,半径为Rb,轨迹点为C,半径为Rc(BC距离),设小圆公转的弧度为 θ [0, ∞),如图:

  

 

  因为大圆的圆心坐标是固定的,要求得小圆上的某点的轨迹,需要先求出小圆当前时刻的圆心坐标,再求出小圆自转的弧度,最后求出小圆上某点的坐标。

 

  第一步:求小圆圆心坐标

  小圆圆心的公转轨迹是一个半径为 R- RB 的圆,求小圆圆心坐标,相当于是求半径为 R- RB 的圆上 θ 弧度对应的点的坐标。

  圆上的点的坐标公式为:

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

  小圆圆心坐标为:( xa + (Ra - Rb) * cos(θ),  ya + (Ra - Rb) * sin(θ) )

  第二步:求小圆自转弧度

  设小圆自转弧度为α,小圆紧贴大圆运动,两者走过的路程相同,因此有:

  Ra * θ = Rb * α

  小圆自转弧度 α = (Ra / Rb) * θ

  第三步:求点C坐标

  点C相对小圆圆心B的公转轨迹是一个半径为 Rc 的圆,类似第一步,有:

  轨迹点C的坐标为:( xa + Rc * cos(θ),  ya + 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.018     radius_B *= 1.019     radius_C *= 1.020     21     P2 = 2*math.pi # 一圈的弧度为 2PI22     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*P234         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             break37         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 = 150101 radius_B = 110102 radius_C = 50103 104 test_centers, test_points = get_points(center, radius_A, radius_B, radius_C)105 test_idx = 2106 draw_point_num_per_tti = 5107 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()

 

  效果:

 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
程序员奶爸必修课——用 pygame 写小游戏
processing 学习第一天笔记
ThingJS创建多边形区域使用js语法控制,采用世界坐标系
圆的算法相关
[转载]弧度制的历史简介
AS2.0精彩特效之位图的飘动
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服