lec12 2d and 3d graphics.pdf
TRANSCRIPT
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
1/18
2D and 3D Graphics
Lecture Week 12
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
2/18
Making Images Move
How can wemake such an
animation?
One image
moving across
another
image
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
3/18
Making Images Move
An image is an object just like the rectangle Has a handle, and properties that can be changed
Example setting the location of an image within an
axis
h = i mage( 256*r and( 100) )
set(h, ' XDat a' , [ 10, 30] , ' YDat a' , [ 30, 50] ) ;
ex01_sprites.m
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
4/18
ex01_sprites.mclose all
bkgrd = imread('track.png');
[car, map, alpha] =
imread('car.png');
figure;
image([bkgrd; bkgrd]);
axis('equal');
holdon
yLoc = 1300;
xLoc = 250;
h = image(car, 'XData', ...
[xLoc, xLoc+size(car,2)-1], ...
'YData', ...
[yLoc, yLoc+size(car,1)-1], ...
'AlphaData', alpha);
for yLoc = 1300:-10:100
if rem(yLoc, 100) == 0
xOffset = 10*rand - 5;
end;
xLoc = xLoc + xOffset;
set(h, 'XData', ...
[xLoc, xLoc+size(car,2)-1], ...
'YData', ...
[yLoc, yLoc+size(car,1)-1]);
pause(0.01);
drawnow
end;
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
5/18
Simple 3D Curve Plott = 0:0.01:3*2*pi;
x = cos(t);
y = sin(t);
z = t/(2*pi*3);
plot3(x,y,z);axis equal
[az, el] = view;
for i = 1:300
view(az+i, el);
drawnow;
pause(0.01)
end;
ex02_3Dcurve.m
http://www.astrobio.nau.edu/~koerner/ast301/lecture/lecture1/lec1.html
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
6/18
Surface Plots - Exampleg = zeros(41);
for x = -10:0.5:10
for y = -10:0.5:10
g(round(2*x+21),round(2*y+21)) = ...
exp(-(x.^2 + y.^2)/50);end;
end;
surf(g)
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
7/18
Surface Plots - Exampleg = zeros(41);
for x = -10:0.5:10
for y = -10:0.5:10
g(round(2*x+21),round(2*y+21)) = ...
exp(-(x.^2 + y.^2)/50);end;
end;
surf(g)
ex03_gaussian2DLoops.m
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
8/18
Surface Plots meshgrid shortcut[x, y] = meshgrid(-10:0.5:10, -10:0.5:10);
g = exp(-(x.^2 + y.^2)/50);
surf(g)
ex04_gaussian2D.m
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
9/18
Surface Plots Making a Sphere[lon,lat] = meshgrid([0:15:360],[-90:15:90]);
x = cosd(lat).*cosd(lon);
y = cosd(lat).*sind(lon);
z = sind(lat);
surf(x,y,z)
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
ex05_sphere.m
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
10/18
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1-1
-0.5
0
0.5
1
Surface Plots Making an Earth[lon,lat] = meshgrid([0:360],[-90:90]);
x = cosd(lat).*cosd(lon);
y = cosd(lat).*sind(lon);
z = sind(lat);
im = imread('earth.png');surf(x,y,z,double(flipud(rgb2gray(im))))
shading interp
axis equal
el = 45;
az = 315;
view(az, el);
ex06_earth.m
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
11/18
Detailed 3D Drawing
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
12/18
Detailed 3D Drawing
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
13/18
Patch Object
A patch graphics object is composed of one or morepolygons that may or may not be connected
patch(X,Y,Z,C)
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
14/18
Patch Object
Making a cube:Help MATLAB 3D Visualization Creating 3D Models with Patches Multifaceted Patches
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
15/18
Patch Object
Making a cube:Help MATLAB 3D Visualization Creating 3D Models with Patches Multifaceted Patches
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
16/18
Cube ExampleX = [0 1 1 0 0 0;
1 1 0 0 1 1;
1 1 0 0 1 1;
0 1 1 0 0 0];
Y = [0 0 1 1 0 0;
0 1 1 0 0 0;
0 1 1 0 1 1;
0 0 1 1 1 1];
Z = [0 0 0 0 0 1;
0 0 0 0 0 1;
1 1 1 1 0 1;
1 1 1 1 0 1];
c = cat(3, X, Y, Z);
patch(X, Y, Z, c);
axis equal
view(45, 45);
ex07_cube.m
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
17/18
Random Walls Exampley = randi(10,1,10);
x = randi(10,1,10);
z = zeros(1,10);
patch([x;x+1;x+1;x],[y;y;y;y],[z;z;z+1;z+1],rand(4,10))
o = randi(2,1,10) - 1;
patch([x;x+o;x+o;x],[y;y+1-o;y+1-o;y],[z;z;z+1;z+1],rand(4,10))
view(20, 60);
ex08_randomWalls.m
-
7/28/2019 Lec12 2D and 3D Graphics.pdf
18/18
Camera ControlsHelp MATLAB 3D Visualization Defining the View Camera Graphics Functions