How to create randomly distributed triangles

I need to create a moving visual display consisting of 500 randomly distributed white triangles (3.4 × 3.4 × 3 cm) on a black background. The frame rate of the visual display needs to be 60 Hz, triangles move in fore-aft direction. Could you please help me figure this out? Thank you.

댓글 수: 10

Rik
Rik 2020년 8월 26일
What steps did you try yourself?
Also, 60 hertz is quite a lot for live generation. Matlab is not a game engine, so this will require a powerful computer. If you write a file you will have a better chance to reach 60 fps.
Ron
Ron 2020년 9월 1일
편집: Rik 2020년 10월 3일
w=10; %width of triangle
ar=0.866; % Aspect ratio for equilateral triangle
h=ar*w;%height of triangle
x=[0 w w/2];%x coordinates of vertices
y=[0 0 h];%y coordinates of vertices
patch(x,y,'white') %plotting triangle in white color
set(gca,'Color','k','xticklabel',[],'yticklabel',[])%setting background black and removing labels
daspect([1 1 1]);%equal data unit length along x and y axis
xlim([-5 15])
ylim([-5 15])
This code creates one triangle. How can I create randomly distributed multiple triangles?
Matt J
Matt J 2020년 9월 2일
편집: Matt J 2020년 9월 2일
Are the triangle positions supposed to be completely random in each frame, or does there need to appear to be gradual frame-to-frame motion? If so, what kind of motion? Could we just rotate the image like a merry-go-round?
Ron
Ron 2020년 9월 2일
편집: Rik 2020년 10월 3일
Thanks for your attention. Triangles needs to be randomly spaced, frame rate of visual display needs to be 60 Hz and then the triangles need to rotate 0.8 degrees about a horizontal axis at 0.2 Hz.
w = 3 ;
ar=0.866; % Aspect ratio for equilateral triangle
h=ar*w;%height of triangle
x=[0 w w/2];%x coordinates of vertices
y=[0 0 h];%y coordinates of vertices
p= patch(x,y,'white'); %plotting triangle in white color
set(gca,'Color','k','xticklabel',[],'yticklabel',[])%setting background black and removing labels
daspect([1 1 1]);%equal data unit length along x and y axis
xlim([-5 15])
ylim([-5 15])
pr = rotate(p, 0.8,[ 0 0 1], [0 0 0]);
I used rotate to rotate the triangle about x axis, but it didn't rotate.
Rik
Rik 2020년 9월 2일
That is an almost imperceptible speed. Why do you want 60 Hz if your actual movement is 0.2 Hz?
Ron
Ron 2020년 9월 2일
Visual movement in fore-aft direction as shown in this video is the reason for the 60 Hz frame rate. The 0.8 degree rotation at 0.2 Hz is superimposed on top of it.
Rik
Rik 2020년 9월 2일
If you want to create something like that I would still encourage you to produce a video, instead of creating the video in real time. The code in my answer will run at 200fps, but that will quickly drop when you put more graphics object in a figure. I also have a reasonly powerful computer, so you would have to verify the performance of your target computer.
Ron
Ron 2020년 9월 2일
Can I use matlab to produce the video? What other program would you recommend? I will need to start and stop the video at specific times in sync with start times of another device.
Rik
Rik 2020년 9월 2일
You can write videos with Matlab. I have never done that before, so I would have to do the same as you: google 'write video Matlab'. You can produce each frame as an image and write it to a video file.
Syncing with an external device might be tricky though. That depends mostly on how that sync should work.
Ron
Ron 2020년 9월 3일
Thank you, Rik. I'll look into making a video. And then use a DAQ to connect with other device.

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

 채택된 답변

Rik
Rik 2020년 9월 2일

0 개 추천

Slightly editing your code gets me to a max framerate of just under 200. Results will vary for different systems.
w = 3 ;
ar=0.866; % Aspect ratio for equilateral triangle
h=ar*w;%height of triangle
p=[];origins=cell(1,10);
figure(1),clf(1)
for k=1:10
x=[0 w w/2];%x coordinates of vertices
y=[0 0 h];%y coordinates of vertices
x=x+15*rand-5;y=y+15*rand-5;%add random
p(k)= patch(x,y,'white'); %#ok<SAGROW> plotting triangle in white color
origins{k}=[mean(x) mean(y) 0];%store explicitly to prevent having to retrieve it from an object
end
set(gca,'Color','k','xticklabel',[],'yticklabel',[])%setting background black and removing labels
daspect([1 1 1]);%equal data unit length along x and y axis
xlim([-5 15]),ylim([-5 15])
rotation_per_frame_in_deg=0.8;
target_fps=60;
frametime=1/target_fps;
h_tic=tic;
for n=1:60%do 60 frames
tic
for k=1:numel(p)
rotate(p(k),[0 0 1],rotation_per_frame_in_deg,origins{k})
end
drawnow
pause(frametime-toc)%comment this line to see the max framerate
end
t=toc(h_tic);
fprintf('the fps was %.1f Hz\n',n/t)

댓글 수: 3

Ron
Ron 2020년 9월 2일
편집: Rik 2020년 10월 3일
Thank you. the frame rate is to create illusion of continuous movement, the 0.8 degrees st 0.2 Hz is to create illusion of rotation on top of the movement. The visual display needs to continuously be looping for 3 minutes.
Ron
Ron 2020년 9월 2일
편집: Rik 2020년 10월 3일
Thanks so much for your help with the code above! Specially that I can appreciate the frame rate change.
Rik
Rik 2020년 10월 3일
편집: Rik 2020년 10월 3일
Why did you remove part of your question? Now my answer is much less useful to others with a similar problem.
Luckily there is a copy of this thread on the Wayback Machine, so I could undo your edits.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Contour Plots에 대해 자세히 알아보기

태그

질문:

Ron
2020년 8월 26일

편집:

Rik
2020년 10월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by