椭圆旋转后对应点映射异常及形状变形问题求助
问题分析与修复方案
问题1:椭圆旋转后视觉上形状变化
这不是MATLAB的问题,是坐标轴比例不一致导致的视觉错觉。MATLAB默认会自动调整x、y轴的缩放比例以适应数据范围,旋转后的椭圆在不等比例轴上会被拉伸/压缩,看起来形状变了,但实际数据是正确的。
修复方法:在绘图时添加axis equal,强制x、y轴使用相同比例:
figure; hold on axis([0 nx 0 ny]) axis equal % 强制等比例显示 for i = 1:N_frames plot(x1_rot(i),y1_rot(i),'o') plot(x0_base_1,y0_base_1,'o') pause(.5) end
问题2:点相对椭圆的位置不一致
你计算点位置时错误使用了x轴旋转后的方向(cos(psi)、sin(psi)),但目标是沿旋转后的椭圆短轴方向偏移。原椭圆的短轴是y轴方向(向量[0;1]),经过旋转矩阵R1变换后,短轴的方向向量应为R1 * [0;1],即[-sin(psi); cos(psi)]。
修复后的点位置计算代码:
phi_1 = pi/12; % 开口角度 theta_1 = pi/4; % 倾斜角度 l_1 = a1/(sin(phi_1)); l_proj_1 = l_1*cos(phi_1)*cos(theta_1); % 计算旋转后的短轴方向向量 short_axis_dir = R1 * [0; 1]; % 沿短轴方向偏移指定距离 x0_base_1 = x1_0 + l_proj_1 * short_axis_dir(1); y0_base_1 = y1_0 + l_proj_1 * short_axis_dir(2);
如果你的椭圆短轴原本是x轴(即a1为短半轴、b1为长半轴),只需把[0;1]替换为[1;0]即可。
完整修复后代码片段
% 椭圆生成与旋转逻辑 angle = linspace(0, Num_periods*2*pi, N_frames); x1 = a1 * cos(angle); y1 = b1 * sin(angle); psi=pi/4; R1 = [cos(psi), -sin(psi); sin(psi), cos(psi)]; ellipse1 = R1 * [x1; y1]; x1_rot = ellipse1(1, :) + x1_0; y1_rot = ellipse1(2, :) + y1_0; % 修复后的点位置计算 phi_1 = pi/12; theta_1 = pi/4; l_1 = a1/(sin(phi_1)); l_proj_1 = l_1*cos(phi_1)*cos(theta_1); short_axis_dir = R1 * [0; 1]; x0_base_1 = x1_0 + l_proj_1 * short_axis_dir(1); y0_base_1 = y1_0 + l_proj_1 * short_axis_dir(2); % 绘图逻辑优化 figure; hold on axis([0 nx 0 ny]) axis equal for i = 1:N_frames plot(x1_rot(i),y1_rot(i),'o') plot(x0_base_1,y0_base_1,'o') pause(.5) end
额外优化:把hold on移到循环外,避免重复执行开启绘图保留的操作,提升效率。
内容的提问来源于stack exchange,提问作者David Merges
相关产品推荐
相关产品推荐

