Angle between two vectors is computed weirdly!
4 views (last 30 days)
Show older comments
Lama Hamadeh on 9 Apr 2021
Commented: Lama Hamadeh on 20 Apr 2021
I am trying to compute the angle between line L1v and the verticle norm Nv via the dot product using the follwoing code. However, I can see that the resulting angle is comouted between the xaxis (the horizontal norm) rather than the verticle and I can't see why. If you can run the follwoing piece of code you can see wha tI mean.
L = 1;
%number of points on S axis
ns = 25;
s = linspace(0,L,ns);
%construct coordinates meshgrid
[X,Y] = meshgrid(s,s);
% mesh needs X,Y and Z so create z
Z = zeros(size(X));
%Visualise the grid
mesh(X,Y,Z,'Marker','o','EdgeColor',"k") %or surf
axis equal tight
%Compute first line equation | L1: Ax + By + C = 0
%first line coordinates
x1L1 = 0.5; y1L1 = 0;
x2L1 = 1 ; y2L1 = 0.1;
%Setting the determinant to be zero to get the line's standrad formula
A = y1L1 - y2L1;
B = x2L1 - x1L1;
C = x1L1*y2L1 - x2L1*y1L1; %or C=-A*x1-B*y1
%L1 stamdard line equation
L1 = (-C/B)+(-A/B)*s; % -A/B is the gradient and -C/B is the intercept
%plotting on the grid
axis([min(s) max(s) min(s) max(s)])
%find the angle between L1 and the norm N
L1v = [x2L1,y2L1] - [x1L1,y1L1]; %compute the x and y projections of L1
%defining the vertical norm
%pos1 = randi(length(s)); vnorm = s(pos1);
vnorm = x1L1;
Nvx = [vnorm 0]; %the verticle normal vector
%plot the verticle norm line
%dot product between L1v and Nvx to get the angle
theta1 = acos((L1v(1,1) * Nvx(1,1)+L1v(1,2) * Nvx(1,2))/...
%show results on the screen
fprintf('The angle between the first line and the vertical norm: theta1=%g\n',...
Any comments on that would be appreicted!
Hrishikesh Borate on 20 Apr 2021
Edited: Hrishikesh Borate on 20 Apr 2021
It’s my understanding that you are trying to find angle between line L1v and vertical normal Nv. This can be achieved by modifying the assignment of Nvx as follows :-
Nvx = [0 vnorm]; %the vertical normal vector
More Answers (0)
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!