1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| clc; clear; close all;
lambda = 550e-9; f = 20e-3; R = 10e-3; lens_z = 0; screen_z = 25e-3;
num_frames = 50; num_rays = 30; theta = linspace(-pi/12, pi/12, num_rays);
v = VideoWriter('newton_rings_color_dynamic.avi'); v.FrameRate = 15; open(v);
fig = figure('Color','k', 'Position', [100, 100, 800, 600]);
for t = 1:num_frames clf; hold on; axis equal; grid on; xlabel('X (m)', 'Color', 'w'); ylabel('Y (m)', 'Color', 'w'); zlabel('Z (m)', 'Color', 'w'); xlim([-0.005 0.005]); ylim([-0.005 0.005]); zlim([-0.005 0.03]); set(gca, 'Color', 'k', 'XColor', 'w', 'YColor', 'w', 'ZColor', 'w'); view(3);
title('彩色动态三维牛顿环', 'Color', 'w');
for i = 1:num_rays angle = theta(i); x0 = 0; y0 = 0; z0 = -5e-3; vx1 = sin(angle); vy1 = 0; vz1 = cos(angle); t1 = (lens_z - z0) / vz1; x1 = x0 + vx1 * t1; y1 = y0 + vy1 * t1; z1 = lens_z;
n1 = 1.0; n2 = 1.5; theta2 = asin(n1/n2 * sin(angle)); vx2 = sin(theta2); vy2 = 0; vz2 = cos(theta2); t2 = (screen_z - lens_z) / vz2; x2 = x1 + vx2 * t2; y2 = y1 + vy2 * t2; z2 = screen_z;
scale1 = min(1, t / num_frames); scale2 = max(0, t/num_frames - 0.5) * 2;
xa = x0 + vx1 * t1 * scale1; ya = y0 + vy1 * t1 * scale1; za = z0 + vz1 * t1 * scale1; plot3([x0 xa], [y0 ya], [z0 za], 'Color', [1 1 1], 'LineWidth', 1.2);
if t > num_frames/2 xb = x1 + vx2 * t2 * scale2; yb = y1 + vy2 * t2 * scale2; zb = z1 + vz2 * t2 * scale2; plot3([x1 xb], [y1 yb], [z1 zb], 'Color', [1 1 1]*0.8, ... 'LineStyle','--', 'LineWidth', 1); end end
[xg, yg] = meshgrid(linspace(-0.002, 0.002, 400)); rg = sqrt(xg.^2 + yg.^2); zg = screen_z + 0.0002 * sin(2*pi*t/num_frames) * sin(50 * rg); lambda_rgb = [620e-9, 550e-9, 470e-9]; img = zeros([size(xg), 3]);
for k = 1:3 phase = pi * rg.^2 / (lambda_rgb(k) * R) + 2*pi*t/num_frames; img(:,:,k) = 0.5 + 0.5 * cos(phase).^2; end
surf(xg, yg, zg, img, 'EdgeColor', 'none');
[X,Y,Z] = cylinder(linspace(0.001, 0.01, 50), 60); Z = -Z * 1e-3 + 0.5e-3; surf(X, Y, Z + lens_z, 'FaceAlpha', 0.3, 'EdgeColor', 'none', ... 'FaceColor', [0.3 0.6 1]);
raw_frame = getframe(fig); resized_frame = imresize(raw_frame.cdata, [600 800]); writeVideo(v, im2frame(resized_frame)); end
close(v);
|