如下图所示,我在给定点周围/下方的正方形以给定的宽度(w)和长度(l)
但是现在我必须归纳出该点的方向(或可以这样说)。我想创建一个朝向该方向的矩形,如下所示。我以前从未研究过标题,如果有人可以向我指出正确的方向,那将会有所帮助。
-----------------编辑--------------- 谢谢@MBo和@HansHirse,我按照您的人员的建议实施了。为简单起见,我选择了该点在矩形的顶线上,而不是远离矩形。我的代码如下所示:
% Point coordinates
P = [5; 5];
% Direction vector
d = [1; -5];
%normalizing
Len = sqrt(d(1)*d(1)+d(2)*d(2));
cs = d(1)/Len;
sn = d(2)/Len;
% Dimensions of rectangle
l = 200;
w = 100;
% Original corner points of rectangle
x1 = P(1) -w/2;
y1 = P(2);
x2 = P(1)+ w/2;
y2 = P(2);
x3 = P(1)+ w/2;
y3 = P(2) - l;
x4 = P(1) -w/2;
y4 = P(2) - l;
rect = [x1 x2 x3 x4; y1 y2 y3 y4];
%rotated rectangles coordinates:
x1_new = P(1)+ (x1 - P(1))* cs - (y1 - P(2))*sn;
y1_new = P(2) +(x1-P(1))*sn + (y1-P(2))*cs;
x2_new = P(1)+ (x2 - P(1))* cs - (y2 - P(2))*sn;
y2_new = P(2) +(x2-P(1))*sn + (y2-P(2))*cs;
x3_new = P(1)+ (x3 - P(1))* cs - (y3 - P(2))*sn;
y3_new = P(2) +(x3-P(1))*sn + (y3-P(2))*cs;
x4_new = P(1)+ (x4 - P(1))* cs - (y4 - P(2))*sn;
y4_new = P(2) +(x4-P(1))*sn + (y4-P(2))*cs;
new_rect = [x1_new x2_new x3_new x4_new; y1_new y2_new y3_new y4_new];
%plot:
figure(1);
plot(P(1), P(2), 'k.', 'MarkerSize', 21);
hold on;
plot([P(1) P(1)+d(1)*10], [P(2) P(2)+d(2)*10], 'b');
patch(new_rect(1,:),new_rect(2,:), 'b', 'FaceAlpha', 0.2);
patch(rect(1,:),rect(2,:),'b')
hold off
旋转的方式不是我想要的:我无法上传图片,imgur表现得很奇怪。您可以运行确切的代码,您将获得我得到的输出。
答案 0 :(得分:1)
因此,您具有点P和矩形R(由坐标定义)。
现在,您想围绕点P旋转矩形A角(据我了解)
每个顶点的新坐标为:
NewV[i].X = P.X + (V[i].X - P.X) * Cos(A) - (V[i].Y - P.Y) * Sin(A)
NewV[i].Y = P.Y + (V[i].X - P.X) * Sin(A) + (V[i].Y - P.Y) * Cos(A)
如果您有方向矢量D = [d1, d2]
,则无需使用trig进行操作。功能:仅利用归一化向量的成分(也许matlab包含用于归一化的功能):
Len = magnitude(D) = sqrt(d1*d1+d2*d2)
//normalized (unit vector)
dx = d1 / Len
dy = d2 / Len
NewV[i].X = P.X + (V[i].X - P.X) * dx - (V[i].Y - P.Y) * dy
NewV[i].Y = P.Y + (V[i].X - P.X) * dy + (V[i].Y - P.Y) * dx
您还可以省略创建轴对齐矩形的坐标并立即计算顶点(错误的可能性更大)
//unit vector perpendicula to direction
perpx = - dy
perpy = dx
V[0].X = P.X - dist * dx + w/2 * perpx
V[1].X = P.X - dist * dx - w/2 * perpx
V[2].X = P.X - dist * dx - w/2 * perpx - l * dx
V[3].X = P.X - dist * dx + w/2 * perpx - l * dx
and similar for y-components
答案 1 :(得分:1)
MBo's answer中已经提到了一般概念。不过,由于我正在编写一些代码,因此这是我的解决方案:
% Point coordinates
P = [5; 5];
% Direction vector
d = [1; -5];
% Dimensions of rectangle
l = 200;
w = 100;
% Distance P <-> rectangle
dist = 20;
% Determine rotation angle from direction vector
% (Omitting handling of corner/extreme cases, e.g. d(2) = 0)
theta = atand(d(1) / d(2));
if ((d(1) > 0) && (d(2) < 0))
theta = theta + 180;
elseif ((d(1) < 0) && (d(2) < 0))
theta = theta - 180;
end
% Original corner points of rectangle
xMin = P(1) - w/2;
xMax = P(1) + w/2;
yMin = P(2) - dist;
yMax = P(2) - dist - l;
rect = [xMin xMax xMax xMin; yMin yMin yMax yMax];
% Auxiliary matrix for rotation
center = repmat(P, 1, 4);
% Rotation matrix
R = [cosd(-theta) -sind(-theta); sind(-theta) cosd(-theta)];
% Rotation
rotrect = (R * (rect - center)) + center;
% Plot
figure(1);
hold on;
plot(P(1), P(2), 'k.', 'MarkerSize', 21); % Point
plot([P(1) P(1)+d(1)*10], [P(2) P(2)+d(2)*10], 'b'); % Direction vector
patch(rect(1, :), rect(2, :), 'b', 'FaceAlpha', 0.2); % Original rectangle
patch(rotrect(1, :), rotrect(2, :), 'b'); % Rotated rectangle
hold off;
xlim([-250 250]);
ylim([-250 250]);
这将产生如下输出图像:
注意:在我的旋转矩阵R
中,您会找到-theta
,因为确定theta
的方式只是一种简单的方法。可以对此进行改进,以便也可以正确设置R
。