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 累计访问:822
点击了解高性能代码运行API
运行结果
代码简介
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
信息服务平台Python plotly 发布于:2024-04-18 16:41 爬取url信息 发布于:2024-04-15 19:21 实验二第一题:半成品——计算长方形面积 发布于:2024-04-15 15:07 4.4、while循环的嵌套,打印九九乘法表 发布于:2024-04-15 09:49 猴子吃桃问题 发布于:2024-04-14 12:28 python计算前缀表达式 发布于:2024-04-14 12:05 输入字符串计算 英文 数字 空格 其他字符的数量 发布于:2024-04-13 21:03 ## # pip install axolotl 发布于:2024-04-13 03:43 4.3、while循环的嵌套应用 发布于:2024-04-12 16:43 4.2 while 循环的基础案例 发布于:2024-04-12 16:33 爬虫执行代码 发布于:2024-04-10 14:28 支撑压力位计算 发布于:2024-04-10 11:19 python测试 发布于:2024-04-09 18:34 交易所量化 发布于:2024-04-08 18:51 2383101028 项兴米。 发布于:2024-04-07 18:51 从文本中,读取上一次更新的最后一条数据ID 发布于:2024-04-07 18:37 embedding,存在pinecone数据库 发布于:2024-04-07 18:41 向量化储存代码 发布于:2024-04-07 10:23 class CustomError(Exception): def __init__(self,message): self.message=message def __str__(self): return f'Error is : {self.message}' 发布于:2024-04-07 09:46 google验证生成 发布于:2024-04-05 00:07 科尔比布莱恩她她她 发布于:2024-04-02 12:55 三组平行数 发布于:2024-04-01 23:25 升级版 输入年月日,计算第几天 发布于:2024-03-31 17:24 输入年月日,确定是第几天 发布于:2024-03-31 12:44 发布于:2024-03-30 22:35 小球从100米自由落体,每次反弹会原来高度一半,求10次落地,共经过了多少米,第10次反弹高度 发布于:2024-03-30 23:33 12345,多少个不重复的三位数 发布于:2024-03-29 23:46 黑色金三角 发布于:2024-03-28 09:28 亦于二次方程买这个可以 发布于:2024-03-27 23:34 一元二次方程组 发布于:2024-03-27 23:51 鸡兔同笼问题 发布于:2024-03-27 00:11 创建10个元素的数组count,随便输入10个数,输出大于平均值的元素个数 发布于:2024-03-26 10:01 私人自己用 发布于:2024-03-25 22:03 输入10明星和圣成绩,计算做高分和总分 发布于:2024-03-25 19:42 # 第一个python程序 发布于:2024-03-25 10:36 for i in range(1, 10): for j in range(1, i+1): print("%d * %d = %d" % (j, i, i*j), end="\t") print() 发布于:2024-03-23 09:49 输入10个数字,从小到大输出【用的冒泡排序】,输入方式升级版 发布于:2024-03-22 22:41 输入三个数字,从小到大输出【用的冒泡排序】 发布于:2024-03-22 22:15 阶乘累加求和 发布于:2024-03-22 00:27 求前10个斐波那契数列 发布于:2024-03-21 23:38 求第10个斐波那契数是多少,方法2 发布于:2024-03-21 23:31 求第10个斐波那契数是多少 发布于:2024-03-21 23:27 数的阶乘的计算 发布于:2024-03-21 23:22 凯萨密码! 发布于:2024-03-20 19:05 正则匹配实验 发布于:2024-03-19 16:55 Get C From P 发布于:2024-03-17 13:27 线性拟合测试 发布于:2024-03-15 15:31 回文数重温:196无法被写成回文数 发布于:2024-03-13 17:47 RLC电路电流有效值及功率因数计算 发布于:2024-03-13 19:19 if and else 发布于:2024-03-11 10:41 [更多]

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

爱心代码awa

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

点击购买 固件广场

yout