旋转线通过其中心给出两个点

时间:2019-07-10 15:49:33

标签: matlab algebra

给出一条具有两个点(x1,y1)(x2,y2)的线A和另一条具有两个点(x1_2,y1_2)(x2_2,y2_2)的线B。

现在,我要旋转B线的中心,直到它与A线平行。

我想知道的是B(旋转)的坐标。 B行的长度必须保持不变。

我做了数学运算,但是找不到错误。这是我的代码:

% Line A
x1 = 125.6238;
x2 = 200.9999;  
y1 = 94.2222;
y2 = 211.2726;

% Line B
x1_2 = 133.8045;
x2_2 = 188.1170;
y1_2 = 87.6330;
y2_2 = 216.1425;

% Centroid of line B
c_x_2 = 160.9685;
c_y_2 = 151.8081;


theta1 = atan2(y2-y1,x2-x1);% Angle between line A and x-axis
theta2 = atan2(y2_2-y1_2,x2_2-x1_2); % Angle between line B and x-axis
theta=theta2-theta1; % Angle between line A and line B
% Create rotation matrix
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];

% Create matrix for coordinate points of line B

x = [x1_2 x2_2];
y = [y1_2 y2_2];
v = [x;y];

% Create matrix for centroid for line B
center = repmat([c_x_2; c_y_2], 1, length(x));

% Shift data points 
s = v - center;

% Rotate
so = R*s;

% Shift back to the desired center of rotation
vo = so + center;

x_rotated = vo(1,:);
y_rotated = vo(2,:);
% make a plot
% Line A
plot( [x1 x2], [y1 y2], 'r')
hold on
% Line B
plot([x1_2 x2_2], [y1_2 y2_2], 'g')
hold on
% Rotate line B at its center
plot(x, y, 'k-', x_rotated, y_rotated, 'm-', c_x_2, c_y_2, 'bo');
axis equal

Line A(red), Line B(green), rotated line B(magenta) should be parallel but it is not

1 个答案:

答案 0 :(得分:0)

您使用的theta是线A和x轴之间的角度。然后,您将B线旋转那个角度theta。这不会给您与A平行的线。

您要做的是找到线A与线B之间的角度,然后将线B旋转该角度以获得与线A平行的线。

这可以通过以下方式完成:

theta1 = atan2(y2-y1,x2-x1);% Angle between line A and x-axis
theta2 = atan2(y2_2-y1_2,x2_2-x1_2); % Angle between line B and x-axis
theta=theta1-theta2; % Angle between line A and line B