Last active
September 13, 2015 13:41
-
-
Save sash13/d5e3ba0a6e2f1631cbab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %% Matlabe example | |
| %%Animated cycloid curve | |
| teta1 = 0:1:120/3-2; | |
| a=0:0.01:2*pi; | |
| ca=0:0.01:2*pi; | |
| %first frame | |
| R=10; | |
| al = (pi/3+pi/20); | |
| x0 = al*R; | |
| ang = al*180/pi; | |
| figure('Color', 'white') | |
| grp = plot(R*(a-sin(a)), R*(1-cos(a)), 'LineWidth', 2, 'Color', 'b') | |
| hold | |
| grid | |
| circle = plot(R*sin(a)+x0, R*cos(a)+10, 'LineWidth', 2, 'Color', 'r') | |
| xM = R*(al-sin(al)); | |
| yM = R*(1-cos(al)); | |
| l3 = plot([xM x0],[yM R], 'LineWidth', 2, 'Color', 'r') | |
| dot = plot(xM,yM, '*') | |
| ht = title(sprintf('Angle: %i', ang)); | |
| axis equal | |
| % Get figure size | |
| pos = get(gcf, 'Position'); | |
| width = pos(3); | |
| height = pos(4); | |
| % Preallocate data (for storing frame data) | |
| mov = zeros(height, width, 1, length(teta1), 'uint8'); | |
| for i = 1:length(teta1) | |
| al = (pi/3+pi/20)+pi/20*i; | |
| a=al-pi:0.01:al+pi; | |
| xM = R*(al-sin(al)); | |
| yM = R*(1-cos(al)); | |
| x0 = al*R; | |
| cX = R*sin(ca)+x0; | |
| cY = R*cos(ca)+10; | |
| ang = al*180/pi; | |
| set(grp, 'XData', R*(a-sin(a)), 'YData', R*(1-cos(a))) | |
| set(circle, 'XData', cX, 'YData', cY) | |
| set(l3, 'XData', [xM x0], 'YData', [yM R]) | |
| set(dot, 'XData', xM, 'YData', yM) | |
| set(ht, 'String', sprintf('Angle: %0.f', ang)) | |
| % Get frame as an image | |
| f = getframe(gcf); | |
| % Create a colormap for the first frame. For the rest of the frames, | |
| % use the same colormap | |
| if i == 1 | |
| [mov(:,:,1,i), map] = rgb2ind(f.cdata, 256, 'nodither'); | |
| else | |
| mov(:,:,1,i) = rgb2ind(f.cdata, map, 'nodither'); | |
| end | |
| end | |
| % Create animated GIF | |
| imwrite(mov, map, 'animation.gif', 'DelayTime', 0, 'LoopCount', inf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment