Rotate a vector so that it coincides with a line or a axis, and find the angle required.

조회 수: 13 (최근 30일)
Hi everyone,
I would like ask you guys for some pointers and suggestions.
I want to rotate a vector for 180 degrees at 1 degree interval each time. At each interval, i would like to know if the rotated vector coincides with 1) y=0 axis, or 2) coincides with a line, or 3)none of the above, and jot down the angles that satisfied each of the three conditions.
For example, the blue vector is the orignal vector, red vector is the vector after applying the rotational matrix, and the orange line is the line for condition (2).
At 30 degrees, red vector does not coincide with anything.
At 90 degrees, red vector coincide with y=0 axis
At 135 degrees, red vector coincide with orange line.
v = [30 0];
theta = 135; %%angle
R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
vR = v*R;
hold on
axis equal
I would imagine that I will need the combination of for loop and if statement, such as
for theta=1:180
if (conincides with y=0 axis)
create a column matrix and list all the angle satisfied the condition
elseif (conincides with the orange line)
create a column matrix and list all the angle satisfied the condition
create a column matrix and list all the angle satisfied the condition
But I dont really have a clue how that conditional statement can be written, please shed some light!
Thank you very much!

답변 (1개)

Raunak Gupta
Raunak Gupta 2020년 8월 8일
Hi Tingwei,
I assume the vector you want to rotate has two set of coordinates i.e. start point and end point taken as (x1,y1) and (x2,y2) respectively. Then since the rotation is happening for the end point so rotation matrix multiplication will happen only for the end point. So basically, after every rotation of 1 degree let’s say the coordinated of rotated vector are (x1,y1) and (xR,yR) then you can quickly figure out the conditions as follows. (Also assuming the orange line coordinates are (x3,y3) and (x4,y4)).
  • Coincides/parallel with y=0 axis :- if (xR-x1) == 0 then this is true.
  • Coincides with orange line :- dot product of rotated line and orange line is 1 or –1.
  • Else condition is trivial
Below code may be useful
% v1 = [x1 y1], v2 = [x2 y2], v3 = [x3,y3], v4 = [x4,y4], vR = [xR,yR]
% Reference Line Coordinates
v1 = [0 0];
v2 = [30 0];
countParallelY0 = 0;
countParallelorange = 0;
countElse = 0;
% Orange line Coordinates
v3 = [0 0];
v4 = [30 0];
% Since we cannot know the size of vector storing angle for specific
% condition we cannot preallocate the vectors. So size will change with
% every iteration
for theta=1:180
R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
vR = v2*R;
% orangeLineDirection :- oLD
% rotatedLineDirection :- rLD
oLD = [v4(1)-v3(1) v4(2)-v3(2)];
rLD = [vR(1)-v1(1) vR(2)-v1(2)];
cosAnglewithOrangeLine = dot(oLD,rLD)/(norm(oLD)*norm(rLD));
if (vR(1)-v1(1)==0)
countParallelY0 = countParallelY0 + 1;
angleY0(countParallelY0) = theta;
elseif (abs(cosAnglewithOrangeLine) == 1)
countParallelorange = countParallelorange + 1;
angleOrange(countParallelorange) = theta;
countElse = countElse + 1;
angleElse(countElse) = theta;


Help CenterFile Exchange에서 Assembly에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by