LAB 3: COMPUTER GRAPHICS AND POLYGONS
Mathematics:
When
a three-dimensional object is projected onto a two-dimensional computer screen,
many aspects of the image may be lost for the user's eyes. For example, we will
be working with an object that is displayed by a finite number of straight line
segments, such as the truncated pyramid with hexagonal base. If the vertical
segments of the pyramid located at different three-dimensional faces merge and
collapse in the two-dimensional screen, then an user may get a wrong impression
about the original image.
The
three-dimenional object is defined by a number of vertices:
(x_{1},y_{1},z_{1}), (x_{2},y_{2},z_{2}),
…, (x_{n},y_{n},z_{n}),
and
a number of pairwise line segments connecting the vertices:
(k_{1}—k_{2}), (k_{2}—k_{3}),
…, (k_{m-1}—k_{m}).
The
truncated pyramid with hexagonal base shown above is defined by n = 12 vertices:
P_{1}(1,-0.8,0) |
P_{2}(0.5,-0.8,-0.866) |
P_{3}(-0.5,-0.8,-0.866) |
P_{4}(-1,-0.8,0) |
P_{5}(-0.5,-0.8,0.866) |
P_{6}(0.5,-0.8,0.866) |
P_{7}(0.84,-0.4,0) |
P_{8}(0.315,0.125,-0.546) |
P_{9}(-0.21,0.65,-0.364) |
P_{10}(-0.36,0.8,0) |
P_{11}(-0.210,0.65,0.364) |
P_{12}(0.315,0.125,0.546) |
and
by m = 18 line segments between the vertices:
(P_{1}—P_{2}) |
(P_{2}—P_{3}) |
(P_{3}—P_{4}) |
(P_{4}—P_{5}) |
(P_{5}—P_{6}) |
(P_{6}—P_{1}) |
(P_{7}—P_{8}) |
(P_{8}—P_{9}) |
(P_{9}—P_{10}) |
(P_{10}—P_{11}) |
(P_{11}—P_{12}) |
(P_{12}—P_{7}) |
(P_{1}—P_{7}) |
(P_{2}—P_{8}) |
(P_{3}—P_{9}) |
(P_{4}—P_{10}) |
(P_{5}—P_{11}) |
(P_{6}—P_{12}) |
The
two-dimensional screen projection is defined at the plane (x,y) with
z = 0. After the screen projection, the image of the truncated pyramid
with hexagonal base transforms to a set of three polygons shown here:
At
the plane (x,y) with z = 0, 12 vertices of the
pyramid collapse into 8 vertices of the image and 18 line segments of the
pyramid collapse into 10 edges of the three polygons. A better view of the
pyramid on the screen can be achieved by applying different linear
transformations to the vertices (x_{k},y_{k},z_{k}),
such as scaling, translation, and rotation. For example, the picture
below represents the truncated pyramid relatively well:
Objectives:
·
develop
three MATLAB functions performing scalings, translations, and rotations at a
given set of vertices of a three-dimensional object
·
understand
effects of scalings, translations, and rotations applied to vertices of a
three-dimensional object
·
develop
a MATLAB function drawing a two-dimensional image of a three-dimensional object
on the computer screen.
·
exploit
linear transformations to find the best view of a given three-dimensional
object on the computer screen.
Let
us define a 3-by-n matrix P for a given set of
vertices of a three-dimensional object:
P = _{}
This
matrix is called the coordinate matrix of the view. The linear
transformations such as scalings, translations, and rotations are applied to
the matrix P.
A 3-by-3
scaling matrix S is defined as
S = _{}
The
scaling matrix S transforms a unit cube (1,1,1)
into a scaled cube (_{},_{},_{}). The multiplication matrix P' = S*P
transforms vertices of the original picture into vertices of new
picture. Scaling in z is not visible after the projection of the
object onto the plane at z=0.
A 3-by-n
translation matrix T is defined as
T = _{}
The
sum matrix T + P moves each vertex (x_{k},y_{k},z_{k})
of the original picture into the vertex (x_{k}+x_{0},y_{k}+y_{0},z_{k}+z_{0}),
translating the whole picture in the direction of (x_{0},y_{0},z_{0}).
Translation in the z-direction is not
visible after the projection of the object onto the plane at z=0.
3-by-3
rotation
matrices R_{x},R_{y},R_{z} are defined as
R_{x} = _{}, R_{y} = _{}, R_{z} = _{}
The
matrix R_{z} rotates the (x,y) plane about
the z-axis at the angle _{} counter-clockwise.
The matrices R_{x} and R_{y }perform
rotations in the (y,z) and (z,x) planes,
respectively. The rotation matrices are orthogonal matrices, i.e. det(R_{z})
= 1 and R_{z}^{T} = R_{z}^{-1}.
The multiplication matrix R_{x}*R_{y}*R_{z}*P
rotates all vertices of the original picture onto the same angles in
the (x,y,z) space.
C = _{}
It
is recommended to use an advanced MATLAB data structure "struct" and
represent C as an array of structures. The
three-dimensional object can be displayed at the computer screen if P
and C are given. The two-dimensional object is displayed as a set
of polygons, where the line segments are connected the vertices at the plane (x,y),
i.e. the object is projected into the plane z = 0.
Exploiting
the MATLAB functions:
a)
Scale
the image by a factor of 0.5 in the x-direction, 2
in the y-direction, and 1/3 in the z-direction.
b)
Translate
0.5 unit in the x-direction
c)
Rotate
20^{0} about the x-axis
d)
Rotate
–45^{0} about the y-axis
e)
Rotate
90^{0} about the z-axis.
a)
Scale
the image by a factor of 0.3 in the x-direction and
0.5 in the y-direction.
b)
Rotate
45^{0} about the x-axis
c)
Translate
1 unit in the x-direction.
d)
Rotate
35^{0} about the y-axis
e)
Rotate
–45^{0} the z-axis.
f)
Translate
1 unit in the z-direction.
g)
Scale
by a factor of 2 in the x-direction.