diff --git a/bodies/body.py b/bodies/body.py index 90cd5536c8ca5dd79159eb57ef293b8e43f723c6..17e1cbef878fcafaf552a4ccb63878a7dc635834 100644 --- a/bodies/body.py +++ b/bodies/body.py @@ -63,6 +63,9 @@ class Body(metaclass=ABCMeta): self.__acceleration = np.array([0, 0, 0], dtype='float32') self.__record_history() + # 是否显示 + self.appeared = True + @property def has_rings(self): """ diff --git a/common/func.py b/common/func.py index 8235671cc280f5c71322c7a36056d961e1798adb..3ba17805e4922365e1edd182e8aae9b9f485d512 100644 --- a/common/func.py +++ b/common/func.py @@ -80,96 +80,20 @@ def get_position_force(angles, force=1, radius=1, radius_offset=None, force_offs # return pxs, pys, fxs, fys return np.round(pxs, 2), np.round(pys, 2), -np.round(fxs, 2), np.round(fys, 2) -# -# -# import math -# -# """ -# 用python写一个函数,根据不同角度的参数列表,返回坐标 pos_x, pos_y,以及分解力fx fy -# -# def get_force(angle_list): -# pos_x = 0 -# pos_y = 0 -# fx = 0 -# fy = 0 -# for angle in angle_list: -# force_x, force_y = get_force_from_angle(angle) -# pos_x += force_x -# pos_y += force_y -# fx += force_x -# fy += force_y -# -# return pos_x, pos_y, fx, fy -# -# def get_force_from_angle(angle): -# force_x = math.cos(math.radians(angle)) -# force_y = math.sin(math.radians(angle)) -# return force_x, force_y -# """ -# -# """ -# ```python -# def get_positions_forces_from_angles(angles,radius, force): -# -# ``` -# """ -# -# -# def get_position_force(angle_list, radius=1, force=1): -# fxs = [] -# fys = [] -# angle_list = np.array(angle_list * np.pi) -# pxs = radius * np.cos(angle_list) -# pys = radius * np.sin(angle_list) -# -# for angle in angle_list: -# fx, fy = get_force_from_angle(angle) -# fxs.append(fx * force) -# fys.append(fy * force) -# -# return np.round(pxs, 2), np.round(pys, 2), np.round(fxs, 2), np.round(fys, 2) -# -# -# def get_force_from_angle(angle): -# force_x = math.cos(angle) -# force_y = math.sin(angle) -# # force_x = math.cos(math.radians(angle)) -# # force_y = math.sin(math.radians(angle)) -# return force_x, force_y -# -# -# if __name__ == '__main__': -# # a = [0, 45, 90, 135, 180, 225, 270, 315, 360] -# # start:返回样本数据开始点 -# # stop:返回样本数据结束点 -# # num:生成的样本数据量,默认为50 -# points = np.linspace(0, 2 * np.pi, 10) -# # points = np.array([0, 45, 90, 135, 180, 225, 270, 315, 360]) -# # points = np.array([0. , 0.17453293, 0.34906585, 0.52359878, 0.6981317 , -# # 0.87266463, 1.04719755, 1.22173048, 1.3962634 , 1.57079633]) -# # x, y = circle_x_y(points, 1) -# # print(x, y) -# print(points) -# pos_x, pos_y, fx, fy = circle_x_y_f(points, radius=1.6, force=25.37) -# print(pos_x, pos_y, fx, fy) -# -# """ -# asteroids = [ -# Asteroid(size_scale=1e9, # 小行星放大 1000000000 倍,距离保持不变 -# init_position=[1.6 * AU, 0, 0], -# init_velocity=[0, 25.37, 0], -# distance_scale=1), -# Asteroid(size_scale=1e9, # 小行星放大 1000000000 倍,距离保持不变 -# init_position=[-1.6 * AU, 0, 0], -# init_velocity=[0, -25.37, 0], -# distance_scale=1), -# Asteroid(size_scale=1e9, # 小行星放大 1000000000 倍,距离保持不变 -# init_position=[0, 1.6 * AU, 0], -# init_velocity=[-25.37, 0, 0], -# distance_scale=1), -# Asteroid(size_scale=1e9, # 小行星放大 1000000000 倍,距离保持不变 -# init_position=[0, -1.6 * AU, 0], -# init_velocity=[25.37, 0, 0], -# distance_scale=1), -# ] -# """ + + +def calculate_distance(pos1, pos2=[0, 0, 0]): + """ + 计算两点间的距离 + :param pos1: + :param pos2: + :return: + """ + d = pow(pow(np.array(pos1[0]) - np.array(pos2[0]), 2) + + pow(np.array(pos1[1]) - np.array(pos2[1]), 2) + + pow(np.array(pos1[2]) - np.array(pos2[2]), 2), 1 / 2) + return d + + +if __name__ == '__main__': + print(calculate_distance([6, 8, 0], [3, 4, 0])) diff --git a/common/system.py b/common/system.py index 62224dfe92a764b281b6e432d2d7258838b59218..ea2c37b970c6622cf7680ceb1f32a20b0fd9f409 100644 --- a/common/system.py +++ b/common/system.py @@ -9,6 +9,7 @@ import numpy as np from common.consts import AU, G from bodies import Body, Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto +from common.func import calculate_distance class System(object): @@ -16,8 +17,9 @@ class System(object): 天体系统 """ - def __init__(self, bodies): + def __init__(self, bodies, max_distance=-1): self.bodies = bodies + self.max_distance = max_distance def add(self, body): self.bodies.append(body) @@ -64,9 +66,36 @@ class System(object): 计算加速度 :return: """ + + def valid_body(body): + """ + 有效的天体 + :param body: + :return: + """ + if not body.appeared: + return False + if self.max_distance > 0: + if calculate_distance(body.position) > self.max_distance: + body.appeared = False + return False + + return True + + self.bodies = list(filter(valid_body, self.bodies)) + for body1 in self.bodies: acceleration = np.zeros(3) for body2 in self.bodies: + if self.max_distance > 0: + if calculate_distance(body1.position) > self.max_distance: # 太远了,则小时 + body1.appeared = False + if calculate_distance(body2.position) > self.max_distance: # 太远了,则小时 + body2.appeared = False + + if False == body1.appeared or body2.appeared == False: + continue + if body1 is body2: continue elif body1.ignore_gravity(body2) or body2.ignore_gravity(body1): diff --git a/simulators/views/body_view.py b/simulators/views/body_view.py index 7ad5d42646ac85145dbd03f3a09fe682d82aa8ca..2a840792bffa48c81efbc55b49ecdbf4af4be72a 100644 --- a/simulators/views/body_view.py +++ b/simulators/views/body_view.py @@ -36,6 +36,8 @@ class BodyView(metaclass=ABCMeta): self.raduis = None self.velocity = None + self.appeared = True + def __repr__(self): return '<%s> m=%.3e(kg), r=%.3e(km), p=[%.3e,%.3e,%.3e](km), v=%s(km/s)' % \ (self.name, self.mass, self.raduis,