猫史档案馆


【Python作品分享】繁花曲线(求助)【求助帖】

用户:凡尘飞絮凡尘飞絮查看:0 回复:3 评论:0 创建时间:2022-05-02T11:23:06


【作品展示】

center_image

 

【作品介绍】

繁花曲线

求助,哪里错了

 

【作品源代码】

# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
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


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第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
EricXiaoEricXiao

你是不是库导入错了啊,我看你导入的是pygame库,应该导入的是turtle库、

 

 

点赞0


评论


EricXiaoEricXiao

再说了繁花曲线不用那么多代码吧

 

点赞0


评论


凡尘飞絮凡尘飞絮

欧克

点赞0


评论