从一个点相对于该点的方向构造一个正方形 [英] Constructing a square from a point with respect to the direction of the point

查看:129
本文介绍了从一个点相对于该点的方向构造一个正方形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下图所示,我在给定点周围/下方的正方形以给定的宽度(w)和长度(l)

I make a square around/below a given point with given width (w) and length (l) like shown below

但是现在我必须归纳出该点的方向(或可以这样说).我想创建一个朝向该方向的矩形,如下所示.我以前从未研究过标题,如果有人可以向我指出正确的方向,那将会有所帮助.

But now I have to induce the direction of the point (or heading so to say). I want to create a rectangle directed towards that direction like shown below. I have never worked with headings before, if anyone can point me in the right directions, that would be helpful.

----------------- EDIT ------------------- 谢谢@MBo& @HansHirse,我按照您的人员的建议实施了.为简单起见,我选择了该点在矩形的顶线上,而不是远离矩形.我的代码如下所示:

-----------------EDIT--------------- Thank you @MBo & @HansHirse, I implemented as suggested by your people. for simplicity, I chose the point to be on the top line of the rectangle rather to be away from the rectangle. my code is as shown below:

% 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表现得很奇怪.您可以运行确切的代码,您将获得我得到的输出.

The way it is rotating is not what I wanted: I am not able to upload a pic, imgur is acting weird. You can run the exact code, you will get the output I am getting.

推荐答案

因此,您具有点P和矩形R(由坐标定义).

So you have point P, and rectangle R (defined by coordinates).

现在,您想围绕点P旋转矩形A角(据我了解)
每个顶点的新坐标为:

Now you want to rotate rectangle by angle A around point P (as far as I understand)
New coordinates for every vertex are:

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包含用于归一化的函数):

If you have direction vector D = [d1, d2], you don't need to operate with trig. functions: just exploit components of normalized vector (perhaps matlab contains function for normalizing):

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

您还可以省略创建轴对齐矩形的坐标并立即计算顶点(错误的可能性更大)

Also you can omit creating coordinates of axis-aligned rectangle and calculate vertices immediately (perhaps more possibilities for mistakes)

//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

这篇关于从一个点相对于该点的方向构造一个正方形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆