How to make this crank slide animation do 3 revolutions?

조회 수: 1 (최근 30일)
Oliver Ries
Oliver Ries 2022년 11월 4일
댓글: Oliver Ries 2022년 11월 4일
I need to be able to use a while loop and some if statements to make this animation of this crank slider do 3 revolutions while still keeping the same amount of data displayed in the command window but I do not know exactly what to do for this while loop and what I have tried has not been working. Any help would be awesome.
p01fig = figure;
r = 2;
d = 5;
t = mylinspace(0,2*pi,45);
[x,y] = mycircle(r,t);
Ax = 0;
Ay = 0;
Bx = 0;
By = r*sin(t) + d * cos((r*cos(t))/d);
Cx = r*cos(t);
Cy = r*sin(t);
[A,B,C] = myangle(Ax, Ay, Bx, By, Cx, Cy);
hold on;
plot(x,y,'-g');
box on;
axis('square',[-6 6 -4 9]);
i = 1;
n = length(t);
while i <= n
p1 = plot(x(i), y(i), 'Marker','o', 'MarkerFaceColor', 'g', 'MarkerSize',7);
p2 = plot(Ax,Ay, 'Marker','o', 'MarkerFaceColor', 'b', 'MarkerSize',7);
p3 = plot(Bx, By(i),'Marker','o', 'MarkerFaceColor', 'r', 'MarkerSize',7);
p4 = plot([Cx(i) Ax], [y(i) By(i)], '-b');
p5 = plot([Ax Cx(i)], [Ay Cy(i)], '-b');
[A,B,C] = myangle(Ax, Ay, Bx, By(i), Cx(i), Cy(i));
p01(i,1) = A;
p01(i,2) = B;
p01(i,3) = C;
p01(i,4) = A + B + C;
s1 = 'Slider Height ';
s2 = num2str(By(i), '%0.2f');
sa = [s1 s2];
if A < 10
s3 = ' A (in Deg) ';
elseif A < 100
s3 = ' A (in Deg) ';
else
s3 = ' A (in Deg) ';
end
s4 = num2str(A, '%0.2f');
sb = [s3 s4];
if B < 10
s5 = ' B (in Deg) ';
elseif A < 100
s5 = ' B (in Deg) ';
else
s5 = ' B (in Deg) ';
end
s6 = num2str(B, '%0.2f');
sc = [s5, s6];
if C < 10
s7 = ' C (in Deg) ';
elseif C < 100
s7 = ' C (in Deg) ';
else
s7 = ' C (in Deg) ';
end
s8 = num2str(C, '%0.2f');
sd = [s7, s8];
s9 = 'sum([A,B,C]) ';
s10 = num2str(A+B+C, '%0.2f');
se = [s9, s10];
tx1 = text(1,5.5,sa,'Fontname', 'FixedWidth', 'FontSize',10);
tx2 = text(1,5,sb,'Fontname', 'FixedWidth', 'FontSize',10);
tx3 = text(1,4.5,sc,'Fontname', 'FixedWidth', 'FontSize',10);
tx4 = text(1,4,sd,'Fontname', 'FixedWidth', 'FontSize',10);
tx5 = text(1,3.5,se,'Fontname', 'FixedWidth', 'FontSize',10);
drawnow();
pause(.05);
i = i + 1;
if i <= n
delete([p1 p2 p3 p4 p5 tx1 tx2 tx3 tx4 tx5]);
end
end
display(p01);
function [y] = mylinspace(lo,hi,n)
if n <= 0
y = [];
else
y(1) = lo;
y(n) = hi;
i = 2;
v = (hi-lo)/(n-1);
while i < n
y(i) = y(i-1) + v;
i = i + 1;
end
end
end
function [x,y] = mycircle(radius, theta)
n = length(theta);
if n < 1
x = [];
y = [];
else
x = zeros(1,n);
y = zeros(1,n);
i = 1;
while i<=n
x(i) = radius * cos(theta(i));
y(i) = radius * sin(theta(i));
i = i + 1;
end
end
end
function [A,B,C] = myangle(Ax, Ay, Bx, By, Cx, Cy)
a = sqrt(((Bx - Ax).^2)+((By - Ay).^2)); % Distance Formula
c = sqrt(((Cx - Ax).^2)+((Cy - Ay).^2)); % Length of c is Always d = 5
b = sqrt(((Bx - Cx).^2)+((By - Cy).^2)); % Same with b
cv = ((b.^2)+(c.^2)-(a.^2))./(2.*b.*c); % Law of Cosines
bv = ((a.^2)+(c.^2)-(b.^2))./(2.*a.*c);
av = ((a.^2)+(b.^2)-(c.^2))./(2.*b.*a);
C = acosd(cv);
A = acosd(bv);
B = acosd(av);
end

채택된 답변

Maik
Maik 2022년 11월 4일
편집: Maik 2022년 11월 4일
It can be done using two "for" statements.
p01fig = figure;
r = 2;
d = 5;
t = mylinspace(0,2*pi,45);
[x,y] = mycircle(r,t);
Ax = 0;
Ay = 0;
Bx = 0;
By = r*sin(t) + d * cos((r*cos(t))/d);
Cx = r*cos(t);
Cy = r*sin(t);
[A,B,C] = myangle(Ax, Ay, Bx, By, Cx, Cy);
hold on;
plot(x,y,'-g');
box on;
axis('square',[-6 6 -4 9]);
i = 1;
n = length(t);
% while i <= n
numOfRev = 3;
for j = 1:numOfRev
for i = 1:n
p1 = plot(x(i), y(i), 'Marker','o', 'MarkerFaceColor', 'g', 'MarkerSize',7);
p2 = plot(Ax,Ay, 'Marker','o', 'MarkerFaceColor', 'b', 'MarkerSize',7);
p3 = plot(Bx, By(i),'Marker','o', 'MarkerFaceColor', 'r', 'MarkerSize',7);
p4 = plot([Cx(i) Ax], [y(i) By(i)], '-b');
p5 = plot([Ax Cx(i)], [Ay Cy(i)], '-b');
[A,B,C] = myangle(Ax, Ay, Bx, By(i), Cx(i), Cy(i));
p01(i,1) = A;
p01(i,2) = B;
p01(i,3) = C;
p01(i,4) = A + B + C;
s1 = 'Slider Height ';
s2 = num2str(By(i), '%0.2f');
sa = [s1 s2];
if A < 10
s3 = ' A (in Deg) ';
elseif A < 100
s3 = ' A (in Deg) ';
else
s3 = ' A (in Deg) ';
end
s4 = num2str(A, '%0.2f');
sb = [s3 s4];
if B < 10
s5 = ' B (in Deg) ';
elseif A < 100
s5 = ' B (in Deg) ';
else
s5 = ' B (in Deg) ';
end
s6 = num2str(B, '%0.2f');
sc = [s5, s6];
if C < 10
s7 = ' C (in Deg) ';
elseif C < 100
s7 = ' C (in Deg) ';
else
s7 = ' C (in Deg) ';
end
s8 = num2str(C, '%0.2f');
sd = [s7, s8];
s9 = 'sum([A,B,C]) ';
s10 = num2str(A+B+C, '%0.2f');
se = [s9, s10];
tx1 = text(1,5.5,sa,'Fontname', 'FixedWidth', 'FontSize',10);
tx2 = text(1,5,sb,'Fontname', 'FixedWidth', 'FontSize',10);
tx3 = text(1,4.5,sc,'Fontname', 'FixedWidth', 'FontSize',10);
tx4 = text(1,4,sd,'Fontname', 'FixedWidth', 'FontSize',10);
tx5 = text(1,3.5,se,'Fontname', 'FixedWidth', 'FontSize',10);
drawnow();
pause(.05);
% i = i + 1;
if i <= n
delete([p1 p2 p3 p4 p5 tx1 tx2 tx3 tx4 tx5]);
else
break;
end
end
end
display(p01);
p01 = 45×4
90.0000 23.4744 66.5256 180.0000 81.8182 23.2254 74.9564 180.0000 73.6364 22.4851 83.8785 180.0000 65.4545 21.2731 93.2724 180.0000 57.2727 19.6209 103.1064 180.0000 49.0909 17.5709 113.3382 180.0000 40.9091 15.1734 123.9175 180.0000 32.7273 12.4846 134.7881 180.0000 24.5455 9.5638 145.8908 180.0000 16.3636 6.4704 157.1660 180.0000
function [y] = mylinspace(lo,hi,n)
if n <= 0
y = [];
else
y(1) = lo;
y(n) = hi;
i = 2;
v = (hi-lo)/(n-1);
while i < n
y(i) = y(i-1) + v;
i = i + 1;
end
end
end
function [x,y] = mycircle(radius, theta)
n = length(theta);
if n < 1
x = [];
y = [];
else
x = zeros(1,n);
y = zeros(1,n);
i = 1;
while i<=n
x(i) = radius * cos(theta(i));
y(i) = radius * sin(theta(i));
i = i + 1;
end
end
end
function [A,B,C] = myangle(Ax, Ay, Bx, By, Cx, Cy)
a = sqrt(((Bx - Ax).^2)+((By - Ay).^2)); % Distance Formula
c = sqrt(((Cx - Ax).^2)+((Cy - Ay).^2)); % Length of c is Always d = 5
b = sqrt(((Bx - Cx).^2)+((By - Cy).^2)); % Same with b
cv = ((b.^2)+(c.^2)-(a.^2))./(2.*b.*c); % Law of Cosines
bv = ((a.^2)+(c.^2)-(b.^2))./(2.*a.*c);
av = ((a.^2)+(b.^2)-(c.^2))./(2.*b.*a);
C = acosd(cv);
A = acosd(bv);
B = acosd(av);
end

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by