Angles from different triangles calculated simultaneously - Matrix Dimension Problem and Storage of values

2 views (last 30 days)
Hello, I am trying to obtain 100 different triangles and calculated their respective angles, as seen the variable "x" contains the range. However, I am having troubles due to matrix dimensions and storage of the angles calculate.
clear
clc
x = -0.06:0.001:-0.012;
%x = -0.012;
Counter = 0:1:100;
while true
Counter = Counter + 1;
% Choose any 3 points of the rectangle
p1 = [-0.06541 0.2458];
p2 = [x 0];
p3 = [0 0];
% Make vectors along each edge of the triangle
v12 = p2-p1;
v13 = p3-p1;
v23 = p3-p2;
% Normalise those vectors
u12 = v12/norm(v12);
u13 = v13/norm(v13);
u23 = v23/norm(v23);
% Compute angles as the acos of their dot products
angle1 = acos(dot(u12, u13, 2));
angle2 = acos(dot(u23, -u12, 2));
angle3 = pi - angle1 - angle2;
% Transforming radians to Degrees
angle_d1 = angle1*180./pi;
angle_d2 = angle2*180./pi;
angle_d3 = angle3*180./pi;
% Display
%figure, plot(p1(1),p1(2),'b.',p2(1),p2(2),'g.',p3(1),p3(2),'m.'), axis equal, hold on
% text(p1(1),p1(2),sprintf('Pt1 (%d deg)',round(angle1/pi*180)))
% text(p2(1),p2(2),sprintf('Pt2 (%d deg)',round(angle2/pi*180)))
%text(p3(1),p3(2),sprintf('Pt3 (%d deg)',round(angle3/pi*180)))
break
end
  4 Comments
Jan
Jan on 14 Apr 2021
I still do not understand, what you want to achieve. (By the way, no need for apologies: This forum is thought for Matlab questions and of course it is the nature of problems that some details are not clear yet :-) )
What does this mean: "obtain 100 different triangles and calculated their respective angles"?
I suggest to start explaining, what your inputs are. Then mention, how you want to calculate which output. A small working example can help. The code you have tried is useful also.
Instead of "it appears an error of Matrix Dimensions" or "it was a problem of not being possible to use vectors", post a copy of the complete error message, because this can contain important details.
What is the purpose of this code:
Angles(i) = acos((diff(data1) * diff(data2).') / ...
(sqrt(sum(diff(data1).^2, 2)) * sqrt(sum(diff(data2).^2, 2))))
diff(data1) * diff(data2).' is a matrix. Then do you really mean the matrix division / or the elementwise division ./ ? In both cases, the result is not a scalar, so you cannot assign it to Angles(i). But what is correct? Do you expect the output to be a scalar or do you want to store a list of arrays in Angles?
The code of the question contains:
Counter = 0:1:100;
while true
Counter = Counter + 1;
...
break
end
Because the loop is stopped in the firt iteration, you can omit the WHILE/END. The variable Counter is not used anywhere, so what is its purpose? Do you really want to create a vector and increment all of its elements by 1?

Sign in to comment.

Accepted Answer

Jan
Jan on 15 Apr 2021
What about moving the code to display one triangle into a function?
%First Triangle Second Triangle
ShowTri([-0.06541 0.2458; ...
-0.06 0; ...
0, 0]);
ShowTri([-0.06541 0.2458; ...
-0.012 0; ...
0, 0]);
function ShowTri(Tri)
% Making vectors along each edge of the triangle
v = Tri([2,3,3], :) - Tri([1,1,2], :);
% Vector Normalization
u = v ./ vecnorm(v, 2);
% Computing angles as the acos of their dot products
angleD(1) = acosd(dot(u(1, :), u(2, :), 2));
angleD(2) = acosd(dot(u(3, :), -u(1, :), 2));
angleD(3) = 180 - angleD(1) - angleD(2);
%Display
FigH = figure();
AxesH = axes(FigH, 'NextPlot', 'add'); % As: hold('on')
Style = {'b.', 'g.', 'm.'};
for k = 1:3
plot(Tri(k, 1), Tri(k, 2), Style{k});
text(Tri(k, 1), Tri(k, 2), sprintf('Pt%d (%d deg)', k, round(angleD(k))));
end
axis equal
end

More Answers (1)

Edwin Espin
Edwin Espin on 14 Apr 2021
Basically, as seen in the attached code there are two triangles form by three vertices each one of them as specified in the first comments. I've done some calculation to normalise the vectors of the lines of the triangles and finally a plotting of the vectors. However, this procedure is too long and I want to iterate the vertices of the point 2. So, I was wondering if someone would know how to make an efficient code to iterate a point of the triangles in order to obtained the angles and positions of the new triangles formed.
clear
clc
% Vetices of 2 triangles
%First Triangle Second Triangle
p1 = [-0.06541 0.2458]; p4 = [-0.06541 0.2458];
p2 = [-0.06 0]; p5 = [-0.012 0];
p3 = [0 0]; p6 = [0 0];
% Making vectors along each edge of the triangle
v12 = p2-p1; v45 = p5-p4;
v13 = p3-p1; v46 = p6-p4;
v23 = p3-p2; v56 = p6-p5;
% Vector Normalization
u12 = v12/norm(v12); u45 = v45/norm(v45);
u13 = v13/norm(v13); u46 = v46/norm(v46);
u23 = v23/norm(v23); u56 = v56/norm(v56);
% Computing angles as the acos of their dot products
angle1_tri1 = acos(dot(u12, u13, 2)); angle1_tri2 = acos(dot(u45, u46, 2));
angle2_tri1 = acos(dot(u23, -u12, 2)); angle2_tri2 = acos(dot(u56, -u45, 2));
angle3_tri1 = pi - angle1_tri1 - angle2_tri1; angle3_tri2 = pi - angle1_tri2 - angle2_tri2;
% Transforming radians to Degrees
angle_d1_tri1 = angle1_tri1*180./pi; angle_d1_tri2 = angle1_tri2*180./pi;
angle_d2_tri1 = angle2_tri1*180./pi; angle_d2_tri2 = angle2_tri2*180./pi;
angle_d3_tri1 = angle3_tri1*180./pi; angle_d3_tri2 = angle3_tri2*180./pi;
%Display
figure, plot(p1(1),p1(2),'b.',p2(1),p2(2),'g.',p3(1),p3(2),'m.'), axis equal, hold on
text(p1(1),p1(2),sprintf('Pt1 (%d deg)',round(angle1_tri1/pi*180)))
text(p2(1),p2(2),sprintf('Pt2 (%d deg)',round(angle2_tri1/pi*180)))
text(p3(1),p3(2),sprintf('Pt3 (%d deg)',round(angle3_tri1/pi*180)))
figure, plot(p4(1),p4(2),'b.',p5(1),p5(2),'g.',p6(1),p6(2),'m.'), axis equal, hold on
text(p4(1),p4(2),sprintf('Pt1 (%d deg)',round(angle1_tri2/pi*180)))
text(p5(1),p5(2),sprintf('Pt2 (%d deg)',round(angle2_tri2/pi*180)))
text(p6(1),p6(2),sprintf('Pt3 (%d deg)',round(angle3_tri2/pi*180)))

Tags

Community Treasure Hunt

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

Start Hunting!