From faa10847fbea17ef5e3f422a9bcd3bbd5e97b98b Mon Sep 17 00:00:00 2001 From: march3 Date: Sun, 26 Mar 2023 19:32:07 +0800 Subject: [PATCH] =?UTF-8?q?Python=E8=B6=85=E4=BA=BA-=E5=AE=87=E5=AE=99?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sim_scenes/interest/text_bodies.py | 6 ++-- sim_scenes/interest/the_eye_of_god.py | 52 ++++----------------------- 2 files changed, 9 insertions(+), 49 deletions(-) diff --git a/sim_scenes/interest/text_bodies.py b/sim_scenes/interest/text_bodies.py index 24f06d8..9fdf59c 100644 --- a/sim_scenes/interest/text_bodies.py +++ b/sim_scenes/interest/text_bodies.py @@ -1,6 +1,6 @@ # -*- coding:utf-8 -*- -# title :上帝之眼 -# description :上帝之眼 +# title :组成文本的星球群(自定义星球) +# description :组成文本的星球群(自定义星球) # author :Python超人 # date :2023-03-26 # link :https://gitcode.net/pythoncr/ @@ -16,7 +16,7 @@ from sim_scenes.interest.utils.body_utils import gen_eye_bodies def show_text_bodies(): """ - 上帝之眼 + 显示文本的星球群 """ D = 600 mass = 0.5e25 diff --git a/sim_scenes/interest/the_eye_of_god.py b/sim_scenes/interest/the_eye_of_god.py index 626f2cf..c61a700 100644 --- a/sim_scenes/interest/the_eye_of_god.py +++ b/sim_scenes/interest/the_eye_of_god.py @@ -1,6 +1,6 @@ # -*- coding:utf-8 -*- -# title :上帝之眼 -# description :上帝之眼 +# title :组成上帝之眼的星球群(自定义星球) +# description :组成上帝之眼的星球群(自定义星球) # author :Python超人 # date :2023-03-26 # link :https://gitcode.net/pythoncr/ @@ -9,7 +9,7 @@ from bodies import Sun, Earth, Moon, FixedStar, Body from common.consts import SECONDS_PER_HOUR, SECONDS_PER_HALF_DAY, SECONDS_PER_DAY, SECONDS_PER_WEEK from sim_scenes.func import mayavi_run, ursina_run -from bodies.body import AU +from sim_scenes.interest.utils.body_utils import gen_eye_bodies import random @@ -27,8 +27,9 @@ def show_eye_of_god(): return pos[0] + (scale - 1.0) * 300 * (random.randint(90, 110)) * D, pos[1], pos[2] # return pos[0], pos[1], pos[2] - bodies: list = gen_eye_bodies( - {"D": D, "Body": Body, "mass": mass, "get_position": get_position, "camera_pos": camera_pos}) + bodies: list = gen_eye_bodies(pixel_image="./images/eye.png", + params={"D": D, "Body": Body, "mass": mass, + "get_position": get_position, "camera_pos": camera_pos}) face = FixedStar(name="face", texture="fixed_star.png", mass=mass * 3000, color=(0xff, 0xf8, 0xd4), init_position=[2000 * D, 200 * D, 100 * D], # [ 远+近- , 左+右- , 上+下-] ignore_mass=True) @@ -41,46 +42,5 @@ def show_eye_of_god(): ursina_run(bodies, SECONDS_PER_WEEK * 2, position=camera_pos) -def gen_eye_bodies(params): - """ - 根据像素图片以及参数,自动生成星球,注意图片像素不能太多,否则会导致电脑运行太慢 - @param params: - @return: - """ - from PIL import Image - - img = Image.open("./images/eye.png").convert('RGBA') - width, height = img.size - interval_factor = 20 # 星球间距因子 - body_template = 'Body(name="%s", mass=mass, color=(%d, %d, %d), size_scale=%.4f, ' \ - 'init_position=get_position([0, %g * D, %g * D], %.4f), init_velocity=[0, 0, 0], ignore_mass=True)' - bodies_str = "[" - - # 以图片像素为坐标,对角线的距离 - distance_hw = pow(pow(width, 2) + pow(height, 2), 1 / 2) - - for h in range(0, height): - # row = [] - for w in range(0, width): - # 以图片像素为坐标,每个像素点到中心的距离 - distance_to_center = pow(pow(w - width / 2, 2) + pow(h - height / 2, 2), 1 / 2) - # 让 body 从中心开始,离摄像机越远, body 的缩放值越大(scale 就越大,) - scale = (distance_to_center / (distance_hw * 10) + 1) # 中心最近 1.0 ~ 1.05 - # TODO: 队列反向排列(中心最远 1.05 ~ 1.0) - # scale = 1.05 - scale + 1.0 - # print(scale) - # 获取像素的颜色 - pixel = img.getpixel((w, h)) - # 对于纯白色的颜色,就忽略,不生成星球(这样像素中纯白色越多,对电脑的压力就越少) - if pixel[0] >= 255 and pixel[1] >= 255 and pixel[1] >= 255: - continue - body_str = body_template % (f"星球{h}:{w}", pixel[0], pixel[1], pixel[2], scale, - (width-w) * interval_factor, (height-h) * interval_factor, scale) - bodies_str += body_str + ",\n" - - bodies_str += "]" - return eval(bodies_str, params) - - if __name__ == '__main__': show_eye_of_god() -- GitLab