import pygame
import math
import sys

# 初始化 pygame
pygame.init()

# 屏幕设置 (4K分辨率适配，这里使用1920x1080作为基准，可全屏)
WIDTH, HEIGHT = 1920, 1080
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
pygame.display.set_caption("凹透镜光学原理演示 - Python/Pygame")

# 颜色定义
BG_COLOR = (10, 10, 15)         # 深色背景
LENS_COLOR = (173, 216, 230)    # 玻璃浅蓝色
LASER_COLOR = (255, 50, 50)     # 红色激光
VIRTUAL_LINE = (255, 255, 255)  # 白色虚线
GLOW_COLOR = (255, 100, 100)    # 光晕颜色

# 物理参数
LENS_X = WIDTH // 2             # 透镜中心X坐标
LENS_HEIGHT = 400               # 透镜高度
LENS_WIDTH = 60                 # 透镜中心厚度
FOCAL_LENGTH = 300              # 焦距 (像素)

# 镜头移动变量
camera_offset = 0.0
camera_speed = 0.5

def draw_concave_lens(surface, x, y):
    """绘制逼真的双凹玻璃透镜"""
    # 使用多边形模拟双凹形状
    points_left = []
    points_right = []
    
    # 生成贝塞尔曲线般的凹陷边缘
    for i in range(LENS_HEIGHT + 1):
        offset = int(LENS_WIDTH / 2 * (1 - (abs(i - LENS_HEIGHT/2) / (LENS_HEIGHT/2))**2))
        points_left.append((x - LENS_WIDTH//2 + offset, y - LENS_HEIGHT//2 + i))
        points_right.append((x + LENS_WIDTH//2 - offset, y - LENS_HEIGHT//2 + i))
    
    # 绘制玻璃主体 (带透明度)
    lens_surface = pygame.Surface((LENS_WIDTH + 20, LENS_HEIGHT), pygame.SRCALPHA)
    pygame.draw.polygon(lens_surface, (*LENS_COLOR, 100), [(p[0]-(x-LENS_WIDTH//2-10), p[1]-(y-LENS_HEIGHT//2)) for p in points_left + points_right[::-1]])
    surface.blit(lens_surface, (x-LENS_WIDTH//2-10, y-LENS_HEIGHT//2))
    
    # 绘制边缘高光 (电影级质感)
    pygame.draw.lines(surface, (200, 230, 255), False, points_left, 2)
    pygame.draw.lines(surface, (200, 230, 255), False, points_right, 2)

def draw_dashed_line(surface, color, start_pos, end_pos, dash_length=10, gap_length=10):
    """绘制虚线 (用于反向延长线)"""
    x1, y1 = start_pos
    x2, y2 = end_pos
    distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
    if distance == 0: return
    
    dash_ratio = dash_length / (dash_length + gap_length)
    num_dashes = int(distance / (dash_length + gap_length)) + 1
    
    for i in range(num_dashes):
        start_ratio = i / num_dashes
        end_ratio = min((i + dash_ratio) / num_dashes, 1.0)
        
        sx = x1 + (x2 - x1) * start_ratio
        sy = y1 + (y2 - y1) * start_ratio
        ex = x1 + (x2 - x1) * end_ratio
        ey = y1 + (y2 - y1) * end_ratio
        
        pygame.draw.line(surface, color, (sx, sy), (ex, ey), 2)

def draw_lens_flare(surface, x, y):
    """绘制微妙的镜头光晕"""
    for radius in range(10, 60, 10):
        alpha = max(0, 50 - radius)
        glow = pygame.Surface((radius*2, radius*2), pygame.SRCALPHA)
        pygame.draw.circle(glow, (*GLOW_COLOR, alpha), (radius, radius), radius)
        surface.blit(glow, (x - radius, y - radius), special_flags=pygame.BLEND_RGBA_ADD)

# 主循环
clock = pygame.time.Clock()
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    # 更新镜头位置 (从左向右平滑移动)
    camera_offset += camera_speed
    if camera_offset > 100: # 限制移动范围
        camera_offset = 100 

    # 填充深色背景
    screen.fill(BG_COLOR)

    # 绘制主光轴
    pygame.draw.line(screen, (50, 50, 60), (0, HEIGHT//2), (WIDTH, HEIGHT//2), 1)

    # 绘制透镜
    draw_concave_lens(screen, LENS_X - camera_offset, HEIGHT//2)

    # 定义三束平行光线的高度
    ray_heights = [-100, 0, 100]
    
    for h in ray_heights:
        start_y = HEIGHT//2 + h
        lens_hit_x = LENS_X - camera_offset
        
        # 1. 入射光线 (从左侧射入)
        pygame.draw.line(screen, LASER_COLOR, (0, start_y), (lens_hit_x, start_y), 3)
        
        # 2. 计算折射后的发散轨迹
        # 凹透镜发散原理：折射光线的反向延长线过虚焦点 (在透镜左侧 FOCAL_LENGTH 处)
        virtual_focus_x = lens_hit_x - FOCAL_LENGTH
        virtual_focus_y = HEIGHT//2
        
        # 计算折射光线的斜率 (从虚焦点指向透镜出射点)
        slope = (start_y - virtual_focus_y) / (lens_hit_x - virtual_focus_x)
        
        # 折射光线向右延伸
        end_x = WIDTH
        end_y = start_y + slope * (WIDTH - lens_hit_x)
        
        # 绘制发散后的实线
        pygame.draw.line(screen, LASER_COLOR, (lens_hit_x, start_y), (end_x, end_y), 3)
        
        # 3. 绘制反向延长线 (白色虚线，汇聚于虚焦点)
        if h != 0: # 中间光线不画虚线
            draw_dashed_line(screen, VIRTUAL_LINE, (lens_hit_x, start_y), (virtual_focus_x, virtual_focus_y))

    # 绘制虚焦点标记
    focus_x = LENS_X - camera_offset - FOCAL_LENGTH
    pygame.draw.circle(screen, VIRTUAL_LINE, (int(focus_x), HEIGHT//2), 5)
    font = pygame.font.SysFont("arial", 24)
    text = font.render("F (虚焦点)", True, VIRTUAL_LINE)
    screen.blit(text, (focus_x - 40, HEIGHT//2 + 15))

    # 绘制光晕特效 (在光线穿过透镜的位置)
    for h in ray_heights:
        draw_lens_flare(screen, LENS_X - camera_offset, HEIGHT//2 + h)

    # 刷新显示
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()