使用python和pygame绘制繁花曲线
前段时间看了一期《最强大脑》,里面展示了各种繁花曲线组合成的非常美丽的图形,一时心血来潮,想尝试自己用代码绘制繁花曲线,想怎么组合就怎么组合。
真实的繁花曲线使用一种称为繁花曲线规的小玩意绘制,繁花曲线规由相互契合的大小两个圆组成,用笔插在小圆上的一个孔中,紧贴大圆的内壁滚动,就可以绘制出漂亮的图案。
这个过程可以做一个抽象:有两个半径不相等的圆,大圆位置固定,小圆在大圆内部,小圆紧贴着大圆内壁滚动,求小圆上的某一点走过的轨迹。
进一步分析,小圆的运动可以分解为两个部分:小圆圆心绕大圆圆心公转、小圆绕自身圆心自转。
设大圆圆心为A,半径为Ra,小圆圆心为B,半径为Rb,轨迹点为C,半径为Rc(BC距离),设小圆公转的弧度为 θ [0, ∞),如图:
因为大圆的圆心坐标是固定的,要求得小圆上的某点的轨迹,就需要先求出小圆在当前时刻的圆心坐标,再求出小圆自转的弧度,最后求出小圆上某点的坐标。
第一步:求小圆圆心坐标
小圆圆心(xb, yb)绕大圆圆心(xa, ya)公转,公转轨迹是一个半径为 RA - RB 的圆。求小圆圆心坐标,相当于是求半径为 RA - 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代码实现如下:
- # -*- coding: utf-8 -*-
- import math
- '''
- 功能:
- 已知圆的圆心和半径,获取某弧度对应的圆上点的坐标
- 入参:
- center:圆心
- radius:半径
- radian:弧度
- '''
- def get_point_in_circle(center, radius, radian):
- return (center[0] + radius * math.cos(radian), center[1] - radius * math.sin(radian))
- '''
- 功能:
- 内外圆A和B,内圆A沿着外圆B的内圈滚动,已知外圆圆心、半径,已知内圆半径,已知公转弧度和绕点半径,计算绕点坐标
- 入参:
- center_A:外圆圆心
- radius_A:外圆半径
- radius_B:内圆半径
- radius_C:绕点半径
- radian:公转弧度
- '''
- def get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, radian):
- # 计算内圆圆心坐标
- center_B = get_point_in_circle(center_A, radius_A - radius_B, radian)
- # 计算绕点弧度(公转为逆时针,则自转为顺时针)
- radian_C = 2.0 * math.pi - ((radius_A / radius_B * radian) % (2.0 * math.pi))
- # 计算绕点坐标
- return get_point_in_circle(center_B, radius_C, radian_C)
有两点需要注意:
(1)屏幕坐标系左上角为原点,垂直向下为Y正轴,与数学坐标系Y轴方向相反,所以第14行Y坐标为减法;
(2)默认公转为逆时针,则自转为顺时针,所以第30行求自转弧度时,使用了2π - α%(2π);
坐标已经计算出来,接下来使用pygame绘制。曲线轨迹的绘制思想是以0.01弧度为一个步长,不断计算出新的坐标,把一系列坐标连起来就会形成轨迹图。
为了能够形成一个封闭图形,还需要知道绘制点什么时候会重新回到起点。想了一个办法,以X轴正半轴为基准线,每次绘制点到达基准线,计算此时绘制点与起点的距离,达到一定精度认为已经回到起点,形成封闭图形。
- ''' 计算两点距离(平方和) '''
- def get_instance(p1, p2):
- return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])
- '''
- 功能:
- 获取绕点路径的所有点的坐标
- 入参:
- center:外圆圆心
- radius_A:外圆半径
- radius_B:内圆半径
- radius_C:绕点半径
- shift_radian:每次偏移的弧度,默认0.01,值越小,精度越高,计算量越大
- '''
- def get_points(center, radius_A, radius_B, radius_C, shift_radian=0.01):
- # 转为实数
- radius_A *= 1.0
- radius_B *= 1.0
- radius_C *= 1.0
- P2 = 2*math.pi # 一圈的弧度为 2PI
- R_PER_ROUND = int(P2/shift_radian/4) + 1 # 一圈需要走多少步(弧度偏移多少次)
- # 第一圈的起点坐标
- start_point = get_point_in_child_circle(center, radius_A, radius_B, radius_C, 0)
- points = [start_point]
- # 第一圈的路径坐标
- for r in range(1, R_PER_ROUND):
- points.append(get_point_in_child_circle(center, radius_A, radius_B, radius_C, shift_radian*r))
- # 以圈为单位,每圈的起始弧度为 2PI*round,某圈的起点坐标与第一圈的起点坐标距离在一定范围内,认为路径结束
- for round in range(1, 100):
- s_radian = round*P2
- s_point = get_point_in_child_circle(center, radius_A, radius_B, radius_C, s_radian)
- if get_instance(s_point, start_point) < 0.1:
- break
- points.append(s_point)
- for r in range(1, R_PER_ROUND):
- points.append(get_point_in_child_circle(center, radius_A, radius_B, radius_C, s_radian + shift_radian*r))
- return points
再加上绘制代码,完整代码如下:
- # -*- coding: utf-8 -*-
- import math
- import random
- '''
- 功能:
- 已知圆的圆心和半径,获取某弧度对应的圆上点的坐标
- 入参:
- center:圆心
- radius:半径
- radian:弧度
- '''
- def get_point_in_circle(center, radius, radian):
- return (center[0] + radius * math.cos(radian), center[1] - radius * math.sin(radian))
- '''
- 功能:
- 内外圆A和B,内圆A沿着外圆B的内圈滚动,已知外圆圆心、半径,已知内圆半径、公转弧度,已知绕点半径,计算绕点坐标
- 入参:
- center_A:外圆圆心
- radius_A:外圆半径
- radius_B:内圆半径
- radius_C:绕点半径
- radian:公转弧度
- '''
- def get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, radian):
- # 计算内圆圆心坐标
- center_B = get_point_in_circle(center_A, radius_A - radius_B, radian)
- # 计算绕点弧度(公转为逆时针,则自转为顺时针)
- radian_C = 2.0*math.pi - ((radius_A / radius_B * radian) % (2.0*math.pi))
- # 计算绕点坐标
- center_C = get_point_in_circle(center_B, radius_C, radian_C)
- center_B_Int = (int(center_B[0]), int(center_B[1]))
- return center_B_Int, center_C
- ''' 计算两点距离(平方和) '''
- def get_instance(p1, p2):
- return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])
- '''
- 功能:
- 获取绕点路径的所有点的坐标
- 入参:
- center:外圆圆心
- radius_A:外圆半径
- radius_B:内圆半径
- radius_C:绕点半径
- shift_radian:每次偏移的弧度,默认0.01,值越小,精度越高,计算量越大
- '''
- def get_points(center_A, radius_A, radius_B, radius_C, shift_radian=0.01):
- # 转为实数
- radius_A *= 1.0
- radius_B *= 1.0
- radius_C *= 1.0
- P2 = 2*math.pi # 一圈的弧度为 2PI
- R_PER_ROUND = int(P2/shift_radian) + 1 # 一圈需要走多少步(弧度偏移多少次)
- # 第一圈的起点坐标
- start_center, start_point = get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, 0)
- points = [start_point]
- centers = [start_center]
- # 第一圈的路径坐标
- for r in range(1, R_PER_ROUND):
- center, point = get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, shift_radian*r)
- points.append(point)
- centers.append(center)
- # 以圈为单位,每圈的起始弧度为 2PI*round,某圈的起点坐标与第一圈的起点坐标距离在一定范围内,认为路径结束
- for round in range(1, 100):
- s_radian = round*P2
- s_center, s_point = get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, s_radian)
- if get_instance(s_point, start_point) < 0.1:
- break
- points.append(s_point)
- centers.append(s_center)
- for r in range(1, R_PER_ROUND):
- center, point = get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, s_radian + shift_radian*r)
- points.append(point)
- centers.append(center)
- print(len(points)/R_PER_ROUND)
- return centers, points
- import pygame
- from pygame.locals import *
- pygame.init()
- screen = pygame.display.set_mode((600, 400))
- clock = pygame.time.Clock()
- color_black = (0, 0, 0)
- color_white = (255, 255, 255)
- color_red = (255, 0, 0)
- color_yello = (255, 255, 0)
- center = (300, 200)
- radius_A = 150
- radius_B = 110
- radius_C = 50
- test_centers, test_points = get_points(center, radius_A, radius_B, radius_C)
- test_idx = 2
- draw_point_num_per_tti = 5
- while True:
- for event in pygame.event.get():
- if event.type==pygame.QUIT:
- pygame.quit()
- exit(0)
- screen.fill(color_white)
- pygame.draw.circle(screen, color_black, center, int(radius_A), 2)
- if test_idx <= len(test_points):
- pygame.draw.aalines(screen, (0, 0, 255), False, test_points[:test_idx], 1)
- if test_idx < len(test_centers):
- pygame.draw.circle(screen, color_black, test_centers[test_idx], int(radius_B), 1)
- pygame.draw.aaline(screen, color_black, test_centers[test_idx], test_points[test_idx], 1)
- test_idx = min(test_idx + draw_point_num_per_tti, len(test_points))
- clock.tick(50)
- pygame.display.flip()
关于pygame的使用,参考博客 eyehere.net/2011/python-pygame-novice-professional-index/
效果:
使用python和pygame绘制繁花曲线的更多相关文章
- Python使用matplotlib绘制三维曲线
本文主要演示如何使用matplotlib绘制三维图形 代码如下: # -*- coding: UTF-8 -*- import matplotlib as mpl from mpl_toolkits. ...
- Python绘制正态分布曲线
使用Python绘制正态分布曲线,借助matplotlib绘图工具: \[ f(x) = \dfrac{1}{\sqrt{2\pi}\sigma}\exp(-\dfrac{(x-\mu)^2}{2 ...
- Python之pygame学习绘制文字制作滚动文字
pygame绘制文字 ✕ 今天来学习绘制文本内容,毕竟游戏中还是需要文字对玩家提示一些有用的信息. 字体常用的不是很多,在pygame中大多用于提示文字,或者记录分数等事件. 字体绘制基本分为以下几个 ...
- WPF 实现繁花曲线
原文:WPF 实现繁花曲线 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/nihang1234/article/details/83346919 X ...
- Python和Pygame游戏开发 pdf
Python和Pygame游戏开发 目录 第1章 安装Python和Pygame 11.1 预备知识 11.2 下载和安装Python 11.3 Windows下的安装说明 11.4 Mac OS X ...
- 用html5的canvas画布绘制贝塞尔曲线
查看效果:http://keleyi.com/keleyi/phtml/html5/7.htm 完整代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...
- Matlab 如何绘制复杂曲线的包络线
Matlab 如何绘制复杂曲线的包络线 http://jingyan.baidu.com/article/aa6a2c14d36c710d4c19c4a8.html 如果一条曲线(比如声音波形)波动很 ...
- Python使用Pygame.mixer播放音乐
Python使用Pygame.mixer播放音乐 frequency这里是调频率... 播放网络中的音频: #!/usr/bin/env python # -*- coding: utf-8 -*- ...
- 4. 绘制光谱曲线QGraphicsView类
一.前言 Qt的QGraphicsView类具有强大的视图功能,与其一起使用的还有QGraphicsScene类和QGraphicsItem类.大体思路就是通过构建场景类,然后向场景对象中增加各种图元 ...
随机推荐
- Android学习开发中如何保持API的兼容
Android学习开发中如何保持API的兼容: 1,采用良好的设计思路 在设计过程中,如果能按照下面的方式来进行设计,会让这个API生命更长久 面向用例的设计,收集用户建议,把自己模拟成用户,保证AP ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- CAN总线基础知识(一)
1.CAN总线是什么? CAN(Controller Area Network)是ISO国际标准化的串行通信协议.广泛应用于汽车.船舶等.具有已经被大家认可的高性能和可靠性. CAN控制器通过组成总线 ...
- 如何修改64位Eclipse中的代码字体大小
1.双击打开Eclipse,如下图所示: 2.找到菜单栏中的Window,单击它,选择Preferences 3.在左侧的树形菜单中找到General--->Appearance--->C ...
- CentOS源码包安装apache、nginx服务
1.通过官网下载apache.nginx源码包 2.进入apache源码包所在目录,解压tar -xf httpd-2.4.12.tar.gz 3.cd httpd-2.4.12 4.阅读README ...
- freemarker中的round、floor和ceiling数字的舍入处理(十七)
1.简易说明 (1)round:四舍五入 (2)floor:向下取整 (3)ceiling:向上取整 2.举例说明 <#--freemarker中的round.floor和ceiling数字的舍 ...
- VBA Excel WideCharToMultiByte Compile error on 64-bit System
Compile Error: The code in this project must be updated for use on64-bit systems. Please review and ...
- 关于protected关键字
protected,算是默认的访问作用域的超集,他们在相同包下时,都可以访问所声明的成员:但对于不同包的访问,默认访问域就不行,protected也必须是通过继承关系来访问. TestBase bas ...
- javaScript替换元素节点
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 学IT应该看些书?
第一阶段:<数据结构><数据库><算法><信息系统管理><互联网>第二阶段:<莫生气><佛经><老子>& ...