日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python绘制繁花曲线代码_使用python和pygame绘制繁花曲线的方法

發(fā)布時(shí)間:2023/12/10 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python绘制繁花曲线代码_使用python和pygame绘制繁花曲线的方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

真實(shí)的繁花曲線使用一種稱(chēng)為繁花曲線規(guī)的小玩意繪制,繁花曲線規(guī)由相互契合大小兩個(gè)圓組成,用筆插在小圓上的一個(gè)孔中,緊貼大圓的內(nèi)壁滾動(dòng),就可以繪制出漂亮的圖案。這個(gè)過(guò)程可以做一個(gè)抽象:有兩個(gè)半徑不相等的圓,大圓位置固定,小圓在大圓內(nèi)部,小圓緊貼著大圓內(nèi)壁滾動(dòng),求小圓上的某一點(diǎn)走過(guò)的軌跡。

進(jìn)一步分析,小圓的運(yùn)動(dòng)可以分解為兩個(gè)部分:小圓圓心繞大圓圓心公轉(zhuǎn)、小圓繞自身圓心自轉(zhuǎn)。設(shè)大圓圓心為A,半徑為Ra,小圓圓心為B,半徑為Rb,軌跡點(diǎn)為C,半徑為Rc(BC距離),設(shè)小圓公轉(zhuǎn)的弧度為θ [0,∞),如圖:

因?yàn)榇髨A的圓心坐標(biāo)是固定的,要求得小圓上的某點(diǎn)的軌跡,需要先求出小圓當(dāng)前時(shí)刻的圓心坐標(biāo),再求出小圓自轉(zhuǎn)的弧度,最后求出小圓上某點(diǎn)的坐標(biāo)。

第一步:求小圓圓心坐標(biāo)

小圓圓心的公轉(zhuǎn)軌跡是一個(gè)半徑為 RA- RB 的圓,求小圓圓心坐標(biāo),相當(dāng)于是求半徑為 RA- RB 的圓上θ 弧度對(duì)應(yīng)的點(diǎn)的坐標(biāo)。

圓上的點(diǎn)的坐標(biāo)公式為:

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

小圓圓心坐標(biāo)為:( xa+ (Ra - Rb) * cos(θ), ya + (Ra - Rb) * sin(θ) )

第二步:求小圓自轉(zhuǎn)弧度

設(shè)小圓自轉(zhuǎn)弧度為α,小圓緊貼大圓運(yùn)動(dòng),兩者走過(guò)的路程相同,因此有:

Ra *θ = Rb *α

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

第三步:求點(diǎn)C坐標(biāo)

點(diǎn)C相對(duì)小圓圓心B的公轉(zhuǎn)軌跡是一個(gè)半徑為 Rc 的圓,類(lèi)似第一步,有:

軌跡點(diǎn)C的坐標(biāo)為:( xa+ Rc* cos(θ), ya+ Rc* sin(θ))

按照以上算法分析,用python代碼實(shí)現(xiàn)如下:

# -*- coding: utf-8 -*-

import math

'''

功能:

已知圓的圓心和半徑,獲取某弧度對(duì)應(yīng)的圓上點(diǎn)的坐標(biāo)

入?yún)?#xff1a;

center:圓心

radius:半徑

radian:弧度

'''

def get_point_in_circle(center, radius, radian):

return (center[0] + radius * math.cos(radian), center[1] - radius * math.sin(radian))

'''

功能:

內(nèi)外圓A和B,內(nèi)圓A沿著外圓B的內(nèi)圈滾動(dòng),已知外圓圓心、半徑,已知內(nèi)圓半徑,已知公轉(zhuǎn)弧度和繞點(diǎn)半徑,計(jì)算繞點(diǎn)坐標(biāo)

入?yún)?#xff1a;

center_A:外圓圓心

radius_A:外圓半徑

radius_B:內(nèi)圓半徑

radius_C:繞點(diǎn)半徑

radian:公轉(zhuǎn)弧度

'''

def get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, radian):

# 計(jì)算內(nèi)圓圓心坐標(biāo)

center_B = get_point_in_circle(center_A, radius_A - radius_B, radian)

# 計(jì)算繞點(diǎn)弧度(公轉(zhuǎn)為逆時(shí)針,則自轉(zhuǎn)為順時(shí)針)

radian_C = 2.0*math.pi - ((radius_A / radius_B * radian) % (2.0*math.pi))

# 計(jì)算繞點(diǎn)坐標(biāo)

return get_point_in_circle(center_B, radius_C, radian_C)

有兩點(diǎn)需要注意:

(1)屏幕坐標(biāo)系左上角為原點(diǎn),垂直向下為Y正軸,與數(shù)學(xué)坐標(biāo)系Y軸方向相反,所以第14行Y坐標(biāo)為減法;

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

坐標(biāo)已經(jīng)計(jì)算出來(lái),接下來(lái)使用pygame繪制。思想是以0.01弧度為一個(gè)步長(zhǎng),不斷計(jì)算出新的坐標(biāo),把一系列坐標(biāo)連起來(lái)就會(huì)形成軌跡圖。

為了能夠形成一個(gè)封閉圖形,還需要知道繪制點(diǎn)什么時(shí)候會(huì)重新回到起點(diǎn)。想了一個(gè)辦法,以X軸正半軸為基準(zhǔn)線,每次繪制點(diǎn)到達(dá)基準(zhǔn)線,計(jì)算此時(shí)繪制點(diǎn)與起點(diǎn)的距離,達(dá)到一定精度認(rèn)為已經(jīng)回到起點(diǎn),形成封閉圖形。

''' 計(jì)算兩點(diǎn)距離(平方和) '''

def get_instance(p1, p2):

return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])

'''

功能:

獲取繞點(diǎn)路徑的所有點(diǎn)的坐標(biāo)

入?yún)?#xff1a;

center:外圓圓心

radius_A:外圓半徑

radius_B:內(nèi)圓半徑

radius_C:繞點(diǎn)半徑

shift_radian:每次偏移的弧度,默認(rèn)0.01,值越小,精度越高,計(jì)算量越大

'''

def get_points(center, radius_A, radius_B, radius_C, shift_radian=0.01):

# 轉(zhuǎn)為實(shí)數(shù)

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 # 一圈需要走多少步(弧度偏移多少次)

# 第一圈的起點(diǎn)坐標(biāo)

start_point = get_point_in_child_circle(center, radius_A, radius_B, radius_C, 0)

points = [start_point]

# 第一圈的路徑坐標(biāo)

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,某圈的起點(diǎn)坐標(biāo)與第一圈的起點(diǎn)坐標(biāo)距離在一定范圍內(nèi),認(rèn)為路徑結(jié)束

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

'''

功能:

已知圓的圓心和半徑,獲取某弧度對(duì)應(yīng)的圓上點(diǎn)的坐標(biāo)

入?yún)?#xff1a;

center:圓心

radius:半徑

radian:弧度

'''

def get_point_in_circle(center, radius, radian):

return (center[0] + radius * math.cos(radian), center[1] - radius * math.sin(radian))

'''

功能:

內(nèi)外圓A和B,內(nèi)圓A沿著外圓B的內(nèi)圈滾動(dòng),已知外圓圓心、半徑,已知內(nèi)圓半徑、公轉(zhuǎn)弧度,已知繞點(diǎn)半徑,計(jì)算繞點(diǎn)坐標(biāo)

入?yún)?#xff1a;

center_A:外圓圓心

radius_A:外圓半徑

radius_B:內(nèi)圓半徑

radius_C:繞點(diǎn)半徑

radian:公轉(zhuǎn)弧度

'''

def get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, radian):

# 計(jì)算內(nèi)圓圓心坐標(biāo)

center_B = get_point_in_circle(center_A, radius_A - radius_B, radian)

# 計(jì)算繞點(diǎn)弧度(公轉(zhuǎn)為逆時(shí)針,則自轉(zhuǎn)為順時(shí)針)

radian_C = 2.0*math.pi - ((radius_A / radius_B * radian) % (2.0*math.pi))

# 計(jì)算繞點(diǎn)坐標(biāo)

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

''' 計(jì)算兩點(diǎn)距離(平方和) '''

def get_instance(p1, p2):

return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])

'''

功能:

獲取繞點(diǎn)路徑的所有點(diǎn)的坐標(biāo)

入?yún)?#xff1a;

center:外圓圓心

radius_A:外圓半徑

radius_B:內(nèi)圓半徑

radius_C:繞點(diǎn)半徑

shift_radian:每次偏移的弧度,默認(rèn)0.01,值越小,精度越高,計(jì)算量越大

'''

def get_points(center_A, radius_A, radius_B, radius_C, shift_radian=0.01):

# 轉(zhuǎn)為實(shí)數(shù)

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 # 一圈需要走多少步(弧度偏移多少次)

# 第一圈的起點(diǎn)坐標(biāo)

start_center, start_point = get_point_in_child_circle(center_A, radius_A, radius_B, radius_C, 0)

points = [start_point]

centers = [start_center]

# 第一圈的路徑坐標(biāo)

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,某圈的起點(diǎn)坐標(biāo)與第一圈的起點(diǎn)坐標(biāo)距離在一定范圍內(nèi),認(rèn)為路徑結(jié)束

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()

效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

本文標(biāo)題: 使用python和pygame繪制繁花曲線的方法

本文地址: http://www.cppcns.com/jiaoben/python/221131.html

總結(jié)

以上是生活随笔為你收集整理的python绘制繁花曲线代码_使用python和pygame绘制繁花曲线的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。