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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/10 python 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python绘制繁花曲线代码_使用python和pygame绘制繁花曲线的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

進一步分析,小圓的運動可以分解為兩個部分:小圓圓心繞大圓圓心公轉、小圓繞自身圓心自轉。設大圓圓心為A,半徑為Ra,小圓圓心為B,半徑為Rb,軌跡點為C,半徑為Rc(BC距離),設小圓公轉的弧度為θ [0,∞),如圖:

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

第一步:求小圓圓心坐標

小圓圓心的公轉軌跡是一個半徑為 RA- RB 的圓,求小圓圓心坐標,相當于是求半徑為 RA- 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代碼實現如下:

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

效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。

本文標題: 使用python和pygame繪制繁花曲線的方法

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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。