0: result[index] = [x - 0.5, y - 0.5, z - 0.5] # 向内扩散 len = 0.7 result[index] = result[index] * (1 - len * inside_rand(0.2)) # 重新赋予深度 newY = random.random() - 0.5 rheartValue = heart_func(result[index][0] + 0.5, newY + 0.5, result[index][2] + 0.5, heartScales[0]) if rheartValue > 0: continue result[index][1] = newY # 删掉肚脐眼 dist = distance(result[index]) if dist < 0.12: continue index = index + 1 if index % 100 == 0: print("{ind} generated {per}%".format(ind=index, per=((index / pointCount) * 100))) return result # 生成随机心 def genRandPoints(pointCount, heartScales, maxVar, ratio): result = np.empty((pointCount, 3)) index = 0 while index < pointCount: x = random.random() y = random.random() z = random.random() mheartValue = heart_func(x, 0.5, z, heartScales[1]) mid_ignore = random.random() if mheartValue < 0 and mid_ignore < mid_point_ignore: continue heartValue = heart_func(x, y, z, heartScales[0]) sheartValue = heart_func(x, y, z, heartScales[1]) if heartValue < 0 and sheartValue > 0: result[index] = [x - 0.5, y - 0.5, z - 0.5] dist = distance(result[index]) if dist < 0.12: continue len = 0.7 result[index] = result[index] * (1 - len * inside_rand(0.2)) index = index + 1 for i in range(pointCount): var = maxVar * ratio randScale = 1 + random.normalvariate(0, var) result[i] = result[i] * randScale return result # 世界坐标到相机本地坐标 def world_2_cameraLocalSapce(world_point): new_point = world_point.copy() new_point[1] = new_point[1] + camera_position[1] return new_point # 相机本地坐标到相机空间坐标 def cameraLocal_2_cameraSpace(cameraLocalPoint): depth = distance(cameraLocalPoint) cx = cameraLocalPoint[0] * (camera_close_plane / cameraLocalPoint[1]) cz = -cameraLocalPoint[2] * (cx / cameraLocalPoint[0]) cameraLocalPoint[0] = cx cameraLocalPoint[1] = cz return cameraLocalPoint, depth # 相机空间坐标到屏幕坐标 def camerSpace_2_screenSpace(cameraSpace): x = cameraSpace[0] y = cameraSpace[1] # convert to view space centerx = canvas_width / 2 centery = canvas_height / 2 ratiox = canvas_width / world_width ratioy = canvas_height / world_heigth viewx = centerx + x * ratiox viewy = canvas_height - (centery + y * ratioy) cameraSpace[0] = viewx cameraSpace[1] = viewy return cameraSpace.astype(int) # 绘制世界坐标下的点 def draw_point(worldPoint): cameraLocal = world_2_cameraLocalSapce(worldPoint) cameraSpsace, depth = cameraLocal_2_cameraSpace(cameraLocal) screeSpace = camerSpace_2_screenSpace(cameraSpsace) draw_size = int(random.random() * 3 + 1) draw_on_buffer(screeSpace, depth, draw_size) # 绘制到缓存上 def draw_on_buffer(screenPos, depth, draw_size): if draw_size == 0: return elif draw_size == 1: draw_point_on_buffer(screenPos[0], screenPos[1], color_strength, depth) elif draw_size == 2: draw_point_on_buffer(screenPos[0], screenPos[1], color_strength, depth) draw_point_on_buffer(screenPos[0] + 1, screenPos[1] + 1, color_strength, depth) elif draw_size == 3: draw_point_on_buffer(screenPos[0], screenPos[1], color_strength, depth) draw_point_on_buffer(screenPos[0] + 1, screenPos[1] + 1, color_strength, depth) draw_point_on_buffer(screenPos[0] + 1, screenPos[1], color_strength, depth) elif draw_size == 4: draw_point_on_buffer(screenPos[0], screenPos[1], color_strength, depth) draw_point_on_buffer(screenPos[0] + 1, screenPos[1], color_strength, depth) draw_point_on_buffer(screenPos[0], screenPos[1] + 1, color_strength, depth) draw_point_on_buffer(screenPos[0] + 1, screenPos[1] + 1, color_strength, depth) # 根据色调和颜色强度获取颜色 def get_color(strength): result = None if strength >= 1: result = colorsys.hsv_to_rgb(hue, 2 - strength, 1) else: result = colorsys.hsv_to_rgb(hue, 1, strength) r = min(result[0] * 256, 255) g = min(result[1] * 256, 255) b = min(result[2] * 256, 255) return np.array((r, g, b), dtype=int) # 可以根据深度做一些好玩的 def draw_point_on_buffer(x, y, color, depth): if x < 0 or x >= canvas_width or y < 0 or y >= canvas_height: return # 混合 strength = float(color) / 255 strength_buffer[x, y] = strength_buffer[x, y] + strength # 绘制缓存 def draw_buffer_on_canvas(output = None): render_buffer.fill(0) for i in range(render_buffer.shape[0]): for j in range(render_buffer.shape[1]): render_buffer[i, j] = get_color(strength_buffer[i, j]) im = Image.fromarray(np.uint8(render_buffer)) im = im.rotate(-90) if output is None: plt.imshow(im) plt.show() else: im.save(output) def paint_heart(ratio, randratio, outputFile = None): global strength_buffer global render_buffer global points # 清空缓存 strength_buffer.fill(0) for i in range(fixed_point_size): # 缩放 point = points[i] * lerp_vector(min_scale, max_scale, ratio) # 球型场 dist = distance(point) radius = 0.4 sphere_scale = radius / dist point = point * lerp_float(0.9, sphere_scale, ratio * 0.3) # 绘制 draw_point(point) # 生成一组随机点 randPoints = genRandPoints(random_point_szie, random_scale_range, random_point_maxvar, randratio) for i in range(random_point_szie): # 绘制 draw_point(randPoints[i]) # 高斯模糊 for i in range(1): strength_buffer = gaussian_filter(strength_buffer, sigma=0.8) # 绘制缓存 draw_buffer_on_canvas(outputFile) def show_images(): img = None for i in range(total_frames): save_name = "{name}.{fmt}".format(name=i, fmt=image_fmt) save_path = os.path.join(output_dir, save_name) img = cv2.imread(save_path, cv2.IMREAD_ANYCOLOR) cv2.imshow("Img", img) cv2.waitKey(25) def gen_images(): global points if not os.path.isdir(output_dir): os.mkdir(output_dir) # 尝试加载或生成中间心 if not os.path.exists(points_file): print("未发现缓存点,重新生成中") points = genPoints(fixed_point_size, fixed_scale_range) np.savetxt(points_file, points) else: print("发现缓存文件,跳过生成") points = np.loadtxt(points_file) for i in range(total_frames): print("正在处理图片... ", i) frame_ratio = float(i) / (total_frames - 1) frame_ratio = frame_ratio ** 2 ratio = math.sin(frame_ratio * math.pi) * 0.743144 randratio = math.sin(frame_ratio * math.pi * 2 + total_frames / 2) save_name = "{name}.{fmt}".format(name=i, fmt=image_fmt) save_path = os.path.join(output_dir, save_name) paint_heart(ratio, randratio, save_path) print("图片已保存至", save_path) if __name__ == "__main__": gen_images() while True: show_images()">

Python在线运行

版本:

编辑于 2022-11-21 23:52 累计访问:1243
点击了解高性能代码运行API
运行结果
代码简介
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
3、防SQL注入 发布于:2024-12-01 16:54 1、MySQL数据库 发布于:2024-12-01 17:16 时间计算器 发布于:2024-11-30 15:17 ### TCP服务器 发布于:2024-11-30 13:49 绘制圣诞树 发布于:2024-11-29 08:04 月份天数。 发布于:2024-11-28 22:15 皮老师讲有关python 语句 发布于:2024-11-28 11:49 Calculate normalized odds 发布于:2024-11-30 16:16 是否v啊随法国撒娇VS搞定了 发布于:2024-11-28 09:40 和如果不会反弹过后 发布于:2024-11-28 09:39 numpy学习测试 发布于:2024-11-27 23:12 测试2C-B!=A 发布于:2024-11-27 17:34 第一个学习用的程序 发布于:2024-11-27 16:50 圣诞树简单测试 发布于:2024-11-27 17:15 Python正则匹配中英文及标点符号 发布于:2024-11-27 09:32 待优化代码 发布于:2024-11-26 15:09 Steam_ID64转换Steam_ID32 发布于:2024-11-26 12:39 需求 设计一个 Game 类 属性: 定义一个 类属性 top_score 记录游戏的 历史最高分 定义一个 实例属性 player_name 记录 当前游戏的玩家姓名 方法: 静态方法 show_help 显示游戏帮助信息 类方法 show_top_score 显示历史最高分 实例方法 start_game 开始当前玩家的游戏 发布于:2024-11-25 21:10 代码测试1-生成直线预测 发布于:2024-11-25 13:10 医院的张欣啊啊 发布于:2024-11-25 12:42 五角星代码 发布于:2024-11-24 14:56 3、web服务器 发布于:2024-11-26 20:31 2、http协议介绍 发布于:2024-11-24 11:53 零基础学python小甲鱼 发布于:2024-11-27 23:06 建立格里高利 发布于:2024-11-23 13:28 录音自动加密 发布于:2024-11-22 21:18 # 这是根据base64编码转换为二进制的代码示例 v2.0版本 # 即,base64编码转二进制(8位二进制和6位二进制)Reshape and Balance) (包含了64个平衡码,原本应该有C8 4 = 70个排列组合,这里因为是base64,所以后面6个删除) 发布于:2024-11-22 23:37 复杂多项式 发布于:2024-11-22 16:30 靶心中标计算 发布于:2024-11-25 09:56 自动出数(输出值01234) 发布于:2024-11-25 16:02 自动出数(选择值几) 发布于:2024-11-22 00:37 迷宫一解法 发布于:2024-11-21 21:44 超多量重几提取 发布于:2024-12-03 00:16 # 这是根据base64编码转换为二进制的代码示例 # (即,base64编码转二进制(8位二进制和6位二进制)Reshape and Balance) (包含了64个平衡码,原本应该有C8 4 = 70个排列组合,这里因为是base64,所以后面6个删除) 发布于:2024-11-23 02:36 # 这是根据base64编码转换为二进制的代码示例 # (即,base64编码转二进制(8位二进制和6位二进制)Reshape and Balance) 发布于:2024-11-21 16:50 # 这份代码是text文本信息转换成Base64编码的示例代码 # (即,String字符串转换为Base64编码) 发布于:2024-11-21 20:27 ## 岩性识别_SVM 发布于:2024-11-21 13:15 判断是否为2.3.5的倍数 发布于:2024-11-21 12:49 球状图片展示 发布于:2024-11-21 11:23 测试运行项目 发布于:2024-11-21 10:21 if语句嵌套 发布于:2024-11-21 09:36 decimal测试 发布于:2024-11-27 15:56 士大夫撒发放 发布于:2024-11-22 09:32 把心计算加 发布于:2024-11-19 21:10 计算靶心价格 发布于:2024-11-19 21:08 自动抓取HTTP代理 发布于:2024-11-19 16:52 为了测试测试 发布于:2024-11-19 16:15 输入棋盘的边长,计算棋盘含有的长方形和长方形数量 发布于:2024-12-02 08:34 输入正偶数,输出两个素数 发布于:2024-11-17 13:19 作为测试使用代码 发布于:2024-11-15 14:02 [更多]

作者 旺仔牛奶(qwqawa)
编辑于:2022-11-21 23:52 VBsKp

爱心代码awa

提示:本站严禁涉政、违法等无关技术的内容
发送
学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

yout