-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation_of_an_Engine.m
73 lines (73 loc) · 2.83 KB
/
Animation_of_an_Engine.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
%% Math 111: MATLAB (Summer 2019)
% TW Judge ([email protected])
%
%
% Lecture: Chapter 6 Supplemental: Animation Using movie Function
% Example #1: Animation of an Engine
%
%
% This program presents a method for animating the motion of an engine
% cylinder and saves the movie as a video file.
%
%
% ----- START OF PROGRAM -----
%
% Initiate Workspace
clear, clc
%
% We Opt to Divide the Engine Cycle Into 50 Frames...
n = 50;
theta = linspace(0, 2*pi, n);
a = 1.1; % Crank of engine/piston
b = 2.7; % Link of engine/piston
c = 0.6; % Radius of cylinder
d = 0.4; % Half height of piston length
aX = a*cos(theta); % Both aX and aY are the coordinates of a point on
aY = a*sin(theta); % circle where the crank attaches to the link
p = aX + sqrt(b^2 - aY.^2); % Calculates Distance traveled by piston
limits = [-a, a+b+d, -a, a]; % Determines limits for axes...
% - horizontal goes from -a to a+b+d;
% - vertical goes from -a to a/
cmap = colormap; % Retrieve current colormap parula
nc = length(colormap) - 1;
%
% Generate the Successive Images Required for the Animation
for k = 1:n
plot(aX, aY, 'k:') % Draw plot of Point A
hold on
cylinderX = [a, a+b+d, a+b+d, a]; % Current axis held so not erased
cylinderY = [-c, -c, c, c]; % Current axis held so not erased
plot(cylinderX, cylinderY, 'k') % Draw cylinder
pistonX = [p(k)-d, p(k)+d, p(k)+d, p(k)-d, p(k)-d];
pistonY = [-c, -c, c, c, -c];
fill(pistonX, pistonY, 'c'); % Draw cylinder space
fuelX = [p(k)+d, a+b+d, a+b+d, p(k)+d, p(k)+d];
fuelY = [-c, -c, c, c, -c];
color = cmap(ceil((p(k)-b+a) / (2*a)*(nc - 1)),:);
fill(fuelX, fuelY, color) % Draw fuel
crankX = [0, aX(k)]; % Draw crank
crankY = [0, aY(k)];
plot(crankX, crankY, 'ko', 'LineWidth', 4)
linkX = [aX(k), p(k)]; % Draw link
linkY = [aY(k), 0];
plot(linkX, linkY, 'ko', 'Linewidth', 4)
axis(limits)
axis off equal % Removes Axes and sets lengths
% of units equal in both the x-
% and y-axis
% ...And Capture Each Image Generated by Each Loop Iteration...
Frames(k) = getframe;
hold off
end
movie(Frames, 5, 30)
%
% Create the Video Object (so we can save frames as a video file)
videoObj = VideoWriter('Engine');
% ...Open a Video File...
open(videoObj);
% ...And Write the Frames to the File...
writeVideo(videoObj, Frames);
% ...And Then Close the File...
close(videoObj);
%
% ----- END OF PROGRAM -----