필터 지우기
필터 지우기

How to create triangle and animate it?

조회 수: 2 (최근 30일)
Ron
Ron 2020년 9월 1일
댓글: Rena Berman 2020년 10월 8일
I'm using this code to start but hasnt worked. patch command is giving error and not sure how to resolve it. Please help.
w0 = 1 ;
w1 = 0.5 ;
N = 5 ;
ww = linspace(w1,w0,N) ;
W = [fliplr(ww) ww] ;
ar=0.866; % Aspect ratio for equilateral triangle
h=ar*W;%height of triangle
for i = 1:length(W)
x=[0 W W/2];%x coordinates of vertices
y=[0 0 h];%y coordinates of vertices
patch(x,y,'white')
set(gca,'Color','k','xticklabel',[],'yticklabel',[])
drawnow
pause(0.1)
end
  댓글 수: 4
Rik
Rik 2020년 10월 3일
Question originally posted by JD recovered from Google cache:
I'm using this code to start but hasnt worked. patch command is giving error and not sure how to resolve it. Please help.
w0 = 1 ;
w1 = 0.5 ;
N = 5 ;
ww = linspace(w1,w0,N) ;
W = [fliplr(ww) ww] ;
ar=0.866; % Aspect ratio for equilateral triangle
h=ar*W;%height of triangle
for i = 1:length(W)
x=[0 W W/2];%x coordinates of vertices
y=[0 0 h];%y coordinates of vertices
patch(x,y,'white')
set(gca,'Color','k','xticklabel',[],'yticklabel',[])
drawnow
pause(0.1)
end
Rena Berman
Rena Berman 2020년 10월 8일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 9월 2일
Try this
w0 = 1 ;
w1 = 0.5 ;
N = 5 ;
ww = linspace(w1,w0,N) ;
W = [fliplr(ww) ww] ;
ar=0.866; % Aspect ratio for equilateral triangle
h=ar*W;%height of triangle
fig = figure();
ax = axes();
set(ax,'Color','k','xticklabel',[],'yticklabel',[])
ax.XLim = [0 1];
ax.YLim = [0 1];
p = patch([0 0 0], [0 0 0], 'w');
for i = 1:length(W)
x = [0 W(i) W(i)/2];%x coordinates of vertices
y = [0 0 h(i)];%y coordinates of vertices
p.Vertices = [x.' y.'];
drawnow
pause(0.1)
end
  댓글 수: 2
Ron
Ron 2020년 9월 2일
Thank you so much! Very helpful!
Also, is it possible to add multiple triangles (say 20) to this, instead of 1.
Ameer Hamza
Ameer Hamza 2020년 9월 2일
I am glad to be of help!
Try this
w0 = 1 ;
w1 = 0.5 ;
N = 5 ;
ww = linspace(w1,w0,N) ;
W = [fliplr(ww) ww] ;
ar=0.866; % Aspect ratio for equilateral triangle
h=ar*W;%height of triangle
n = 200; % number of triangles
area = 20; % 10x10 square for all the triangles
triangle_locations = area*rand(n, 2); % location of triangles
fig = figure();
ax = axes();
set(ax,'Color','k','xticklabel',[],'yticklabel',[])
ax.XLim = [0 area];
ax.YLim = [0 area];
p = gobjects(1, n);
for i=1:n
p(i) = patch([0 0 0], [0 0 0], 'w');
end
for i = 1:length(W)
for k = 1:n
x = [0 W(i) W(i)/2] + triangle_locations(k, 1);%x coordinates of vertices
y = [0 0 h(i)] + triangle_locations(k, 2);%y coordinates of vertices
p(k).Vertices = [x.' y.'];
end
pause(0.1)
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by