% 输入参数
R_disk = 10; % 圆盘的半径
d_eccentric = 2; % 偏心圆的偏心量
x0 = 15; % 固定点的x坐标
y0 = 15; % 固定点的y坐标
theta_range = 0:pi/36:2*pi; % 旋转角度范围,从0到2*pi,步长为10度
n_angles = length(theta_range); % 角度数量
% 偏心圆的半径数组
radii_eccentric = [8, 6]; % 两个同心偏心圆的半径,分别为8和6
n_radii = length(radii_eccentric); % 偏心圆数量
% 计算每个角度下激光器与各个偏心圆边缘的最短距离
distance_to_edges = NaN(n_radii, n_angles); % 存储每个角度、每个偏心圆的距离
% 图形初始化
figure;
hold on;
axis equal;
xlim([-R_disk-2, R_disk+R_disk]);
ylim([-R_disk-2, R_disk+R_disk]);
% 绘制固定点
plot(x0, y0, 'ro', 'MarkerSize', 8);
text(x0, y0, ' 固定点', 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'left');
% 绘制圆盘
theta_disk = linspace(0, 2*pi, 100);
plot(R_disk * cos(theta_disk), R_disk * sin(theta_disk), 'k--'); % 圆盘轮廓
% 遍历每个角度
for i = 1:n_angles
% 偏心圆圆心的位置
xc = d_eccentric * cos(theta_range(i));
yc = d_eccentric * sin(theta_range(i));
% 绘制内外偏心圆
% 内圆(蓝色)
rectangle('Position', [xc - radii_eccentric(2), yc - radii_eccentric(2), 2*radii_eccentric(2), 2*radii_eccentric(2)], ...
'Curvature', [1, 1], 'EdgeColor', 'b', 'LineWidth', 2);
% 外圆(红色)
rectangle('Position', [xc - radii_eccentric(1), yc - radii_eccentric(1), 2*radii_eccentric(1), 2*radii_eccentric(1)], ...
'Curvature', [1, 1], 'EdgeColor', 'r', 'LineWidth', 2);
% 计算激光器指向圆盘圆心的射线方向向量
ray_vector = [-x0, -y0]; % 从 (x0, y0) 指向原点 (0, 0)
% 绘制射线
plot([x0, 0], [y0, 0], 'r--', 'LineWidth', 2); % 从固定点到原点的射线
% 计算射线与每个偏心圆的交点
for j = 1:n_radii
% 使用参数方程表示射线:(x, y) = (x0, y0) + t * ray_vector
% 射线与偏心圆的交点满足 (x - xc)^2 + (y - yc)^2 = R_eccentric^2
% 方程化简后:A * t^2 + B * t + C = 0,解这个方程
A = ray_vector(1)^2 + ray_vector(2)^2;
B = 2 * (ray_vector(1) * (x0 - xc) + ray_vector(2) * (y0 - yc));
C = (x0 - xc)^2 + (y0 - yc)^2 - radii_eccentric(j)^2;
% 解一元二次方程 A * t^2 + B * t + C = 0
discriminant = B^2 - 4 * A * C;
if discriminant >= 0
% 计算交点的 t 值
t1 = (-B - sqrt(discriminant)) / (2 * A);
t2 = (-B + sqrt(discriminant)) / (2 * A);
% 找到交点的 t 值(最小的正 t 值)
t = min(t1, t2);
if t > 0 % 确保交点在射线方向上
% 计算交点的坐标
intersection_x = x0 + t * ray_vector(1);
intersection_y = y0 + t * ray_vector(2);
% 计算固定点到交点的距离
distance_to_edges(j, i) = sqrt((intersection_x - x0)^2 + (intersection_y - y0)^2);
% 绘制交点
plot(intersection_x, intersection_y, 'go', 'MarkerSize', 8); % 交点
% 绘制从固定点到交点的线段
plot([x0, intersection_x], [y0, intersection_y], 'g-', 'LineWidth', 2);
else
distance_to_edges(j, i) = NaN; % 如果没有交点,则设置为NaN
end
else
distance_to_edges(j, i) = NaN; % 如果没有交点,则设置为NaN
end
end
% 输出当前角度下的距离
fprintf('角度: %.2f°,到各个偏心圆边缘的距离: %.2f, %.2f\n', rad2deg(theta_range(i)), ...
distance_to_edges(1, i), distance_to_edges(2, i));
% 暂停更新图形
pause(0.1); % 控制更新速度
end
% 绘制距离变化图
figure;
hold on;
plot(rad2deg(theta_range), distance_to_edges(1, :), 'r-', 'LineWidth', 2); % 外圆(红色)
plot(rad2deg(theta_range), distance_to_edges(2, :), 'b-', 'LineWidth', 2); % 内圆(蓝色)
xlabel('旋转角度 \theta (°)');
ylabel('激光器到偏心圆边缘的距离');
title('激光器到多个偏心圆边缘的距离变化');
legend('外偏心圆 (半径 8)', '内偏心圆 (半径 6)');
grid on;
偏心圆旋转时边缘与某一固定点的距离计算
最新推荐文章于 2025-04-24 22:21:31 发布