tiny_simulations.relativity のソースコード

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D


[ドキュメント] def main(): plt.rcParams['font.family'] = 'MS Gothic' plt.rcParams['font.size'] = 16 theta_deg = 60 theta = np.deg2rad(theta_deg) v = np.cos(theta) gamma = 1.0 / np.sqrt(1 - v**2) origin_shift = np.array([1.0, 1.0, 1.0]) a, b = 0.5, 1 / 3 L = 2 * max(a, b) x_axis = np.array([L, 0, 0]) y_axis = np.array([0, L, 0]) t_axis = np.array([0, 0, L]) x_p_axis = gamma * np.array([L, 0, -v * L]) y_p_axis = np.array([0, L, 0]) t_p_axis = gamma * np.array([-v * L, 0, L]) rect_rel = [ [0, 0, 0], [a, 0, 0], [a, b, 0], [0, b, 0], [0, 0, 0], ] x_rect, y_rect, t_rect = zip(*[ (origin_shift[0] + x, origin_shift[1] + y, origin_shift[2] + t) for x, y, t in rect_rel ]) x_p_rect = [gamma * (x - v * t) for x, t in zip(x_rect, t_rect)] y_p_rect = list(y_rect) t_p_rect = [gamma * (t - v * x) for x, t in zip(x_rect, t_rect)] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot([0, x_axis[0]], [0, x_axis[1]], [0, x_axis[2]], color='blue', linewidth=2) ax.plot([0, y_axis[0]], [0, y_axis[1]], [0, y_axis[2]], color='skyblue', linewidth=2) ax.plot([0, t_axis[0]], [0, t_axis[1]], [0, t_axis[2]], color='blue', linestyle='--', linewidth=2) ax.plot([0, x_p_axis[0]], [0, x_p_axis[1]], [0, x_p_axis[2]], color='red', linewidth=2) ax.plot([0, y_p_axis[0]], [0, y_p_axis[1]], [0, y_p_axis[2]], color='salmon', linewidth=2) ax.plot([0, t_p_axis[0]], [0, t_p_axis[1]], [0, t_p_axis[2]], color='red', linestyle='--', linewidth=2) ax.plot(x_rect, y_rect, t_rect, color='blue', linewidth=2) ax.plot(x_p_rect, y_p_rect, t_p_rect, color='red', linewidth=2) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('t') ax.set_xlim([0, origin_shift[0] + a + 0.2]) ax.set_ylim([0, origin_shift[1] + b + 0.2]) ax.set_zlim([ min(0, min(t_p_rect) - 0.2), max(origin_shift[2] + b + 0.2, t_p_axis[2] + 0.2) ]) legend_elements = [ Line2D([0], [0], color='blue', lw=2, label='静止系 x 軸'), Line2D([0], [0], color='skyblue', lw=2, label='静止系 y 軸'), Line2D([0], [0], color='blue', lw=2, linestyle='--', label='静止系 t 軸'), Line2D([0], [0], color='red', lw=2, label="運動系 x' 軸"), Line2D([0], [0], color='salmon', lw=2, label="運動系 y' 軸"), Line2D([0], [0], color='red', lw=2, linestyle='--', label="運動系 t' 軸"), Line2D([0], [0], color='blue', lw=2, label="静止系の矩形"), Line2D([0], [0], color='red', lw=2, label="運動系の矩形"), ] ax.legend(handles=legend_elements, fontsize=14, loc='upper left') plt.title( f"長方形を (1,1,1) にシフト+座標軸表示 " f"(θ={theta_deg}°, v/c={v:.2f})", pad=20 ) plt.show()
if __name__ == "__main__": main()