Transcript Computer Graphics - Intercollege Cyprus
Computer Graphics
Andreas Savva
Lecture Notes 1
Computer Graphics Definition Computer Graphics
is concerned with all aspects of producing pictures or images using a computer.
2
Rabbit Change
1960s
Display of data on hardcopy plotters and cathode ray tube (CRT) screens.
Today
Creation, storage, and manipulation of models and images of objects. These models include physical, mathematical, engineering, architectural, conceptual (abstract) structures, natural phenomena, etc. 3
Interactive Computer Graphics
Today Computer Graphics are largely interactive : the user control the contents, structure, and appearance of objects and their displayed images by using input devises.
4
CG Vs Image Processing
Until recently they have been quite separate disciplines. However, the overlap between the two is growing.
Computer Graphics task: to synthesize pictures and images on the basis of some description, or model, in a computer.
Image (Picture) Processing task: to reconstruct models of 2D or 3D objects from their pictures.
to improve or alter images that where created elsewhere (digitized from photographs or captured by a video recorder).
5
Examples of Image Processing
aerial photographs X-ray images computerized axial tomography (CAT) scans fingerprint analysis improving image quality by eliminating noise (extraneous or missing pixel data) or by enhancing contrast scan images, typewritten or even hand-printed characters reconstruction of 3D models from several 2D images 6
History of Computer Graphics
1960 The first computer graphics software “Sketchpad” was created by Evan Sutherland on his doctoral thesis at MIT.
A graph could be drawn on a digital computer. The computer had its own air-conditioning environment and it read in its program and data from hole punched paper tapes and output the results either to tape or a plotter. The capital cost of the equipment was about £1M + running cost.
Most applications were scientific, pure research funded by governments, universities, the army and international corporations, with no thought of recovering the cost of development.
There were rabid advances in display technology and in the algorithms used to manage pictorial information.
1970 Large corporations like Fort, Boeing and General Motors could draw drafting productivity improvements of their products and costs were recovered in about 3 years.
Random-scan graphics displays used were very expensive.
Computer graphics was a small specialized field, because the hardware was still expensive and there were not any easy-to-use and cost-effective graphics-based application programs.
7
History (continue)
1980 - Today After the production of low-cost personal computers with build-in raster graphics displays, easy-to-use and inexpensive graphics-based applications such as the Xerox Star and later the mass-produced, even less expensive Apple Macintosh and IBM PC and its clones followed, which made good quality high-speed graphics available to all.
Computer technology is rapidly spreading in all areas because the hardware/software system “ pays for itself ” by speeding up operations significantly increasing productivity giving organizations a competitive edge The direct-view storage tube greatly reduced the capital cost, and modern high-resolutions raster displays have made good quality high-speed graphics available to ALL.
Today, it is hard to find an area of human activity for which there are no Computer Graphics applications, i.e.
games, photography, animation, newspaper, Magazine, television, scientific research, computer-aided design and manufacture, image enlargement. Charting, e-commerce.
8
Applications of Computer Graphics
Science Engineering Medicine Business Industry & Government Art CAD (cars, airplanes) Cinema and TV Advertising Education & Training Flight simulators Space exploration Entertainment Computer Interfaces e-Commerce Home
40 30 20 10 0 90 80 70 60 50 Winter Spring Summer Automn
9
Why are graphics so popular?
A picture is worth ten thousand words.
A moving picture (animation) is worth ten thousand static ones.
10
Mathematical Background
Pythagoras theorem Trigonometry Natural logarithms Coordinate geometry Matrices Differentiation 11
Matrices
Applications in many areas Rectangular array – 2D array Colour Size Black White Gray Navy Red small medium 60 25 100 75 80 100 48 93 94 80 large 150 60 140 140 60 Xlarge 120 10 170 170 0 60 25 100 75 80 100 48 93 94 80 150 60 60 140 60 120 10 80 170 0 12
Definitions A matrix is a rectangular array of elements (usually numbers) arranged in rows and columns enclosed in brackets.
1 2 2 4 0
a A
0 1 4 2
B
3 2 3 1 5 2
C
b c
is a 3×2 matrix,
B
is a 3×3 matrix
C
is a 4×1 matrix .
Matrices are denoted by capital letters while their elements are written as lower case letters as in example
C
above.
We refer to a particular element by using notation that refers to the row and column containing the element, i.e. a 21 =-1, b 32 = 1, c 21 =
b.
Two matrices
A
and
B
are said to be equal, written
A = B,
if they have the same order and
a ij = b ij
for every
i
and
j
.
13
1.
Exercises
Write the elements
d
23 and
d
42 of matrix
D
.
D
60 25 100 75 80 100 48 93 94 80 150 60 60 140 60 120 10 80 170 0 2.
Find
x
,
y
, and
z
so that
x
6
y
2 3
x
2
z
2
y
y
18
z z y
2 6 6
z
3.
Write down the 3×4 matrix
A
, where
a ij
i
0 2
i
j j
if if
i i
j j
otherwise 14
Type of Matrices
Square matrix – a matrix with an equal number of rows and columns.
A
1 4 7 2 5 8 3 6 9 is a 3 3 square matrix Diagonal matrix – a square matrix with zeros everywhere except down the leading diagonal.
A
1 0 0 0 5 0 0 0 9 is a 3 3 diagonal matrix Unit matrix ( identity matrix ) – a diagonal matrix with ones down the leading diagonal. The identity matrix is denoted with the letter
I
.
I
3 1 0 0 0 1 0 0 0 1 is a 3 3 unit matrix 15
Type of Matrices (continue)
Zero matrix – a matrix with zeros everywhere, denoted by
O
.
Symmetric matrix – a square matrix whose ( the same as the (
ji
) th element for all
i
and
j
.
ij
) th element is
A
1 5 4 5 2 7 4 7 3 is a symmetric matrix Row matrix ( row vector ) – a matrix with only one row, i.e. its order is 1×
m
.
Column matrix ( column vector ) – a matrix with only one column, i.e. an
n
×1 matrix.
16
Matrix Addition and Subtraction
If A and B are two n×m matrices then – their sum
C = A + B
is the n×m matrix with
c ij = a ij + b ij
– their difference
C = A - B
is the n×m matrix with
c ij = a ij - b ij
Examples: 1 2 5 1 3 3 3 2 2 3 4 7 1 3 2 ( 2 ) ( 5 ) 2 ( 1 ) ( 3 ) 3 4 ( 3 ) 7 4 0 3 4 7 4 1 2 5 1 3 3 3 2 2 3 4 7 1 3 2 ( 2 ) ( 5 ) 2 ( 1 ) ( 3 ) 3 4 ( 3 ) 7 2 4 7 2 1 10 17
Scalar Multiplication of Matrices
If
A
is an
n
×
m
matrix and
k
the scalar multiple
C = kA
is a real number, then is the
n
×
m
matrix where
c ij = ka ij
. 2 3 2 2 3 4 7 2 2 3 ( 2 ) 2 2 2 ( 3 ) 2 4 2 7 6 4 4 6 8 14 18
The Transpose of a Matrix
If
A
is a matrix then the transpose of
A
matrix
A t
where
a t ij = a ji
.
is the
A
1 3 2 2 2 2 1 1 1 0 4 3
A t
1 2 1 3 2 0 2 1 4 2 1 3 19
Matrix Multiplication
If
A
is an
n×m
the product of matrix and
B A
is an
m
×
p
matrix then and
B
,
C = AB
, is an
n
×
p
matrix
C ij
= (row
i
of
A
) × (column
j
of
B
) =
a i1 b 1j + a i2 b 2j + a i3 b 3j + … a in b jp
3 2 1 2 3 0 3 3 4 1 2 3 3 1 4 2 0 3 1 2 3 4 ( 3 ) ( 2 ) 1 ( 3 ) 3 1 ( 3 ) 1 1 0 3 2 ( 3 ) 3 4 2 ( 2 ) 4 0 ( 2 ) 1 4 4 0 ( 3 ) ( 2 ) ( 2 2 ) 1 0 0 3 14 5 3 2 3 2 8 2 4 20
Properties
If
a
,
b
are scalars and
A
,
B
,
C
are matrices of the same dimension (order) then
A + B = B + A A + (B + C) = (A + B) + C a(bA) = (ab)A (a + b)A = aA + bA a(A + B) = aA + aB a(A - B) = aA - aB A + OA = A, and aO = O (A + B) t = A t + B t (A - B) t = A t - B t
21
Properties
If
a
,
b
are scalars and
A
,
B
,
C
are matrices of the same dimension (order) then assuming that the matrix dimensions are such that the products in each of the following are defined, then we have:
AB ≠ BA A(BC) = (AB)C (A + B)C = AC + BC A(B + C) = AB + AC OA = O, AO = O (aA)(bB) = abAB IA = AI = A (AB) T = B T A T
22
Inverse of a 2×2 Matrix
Not all Matrices have an inverse.
The Determinant – determines if a matrix has an inverse or not. If the determinant of a matrix
A
is zero then the matrix has no inverse.
A
a c b d
det
A
A
ad
bc
Example:
A
2 1 3 4 det
A
A
2 4 3 ( 1 ) 11 23
Inverse of a 2×2 Matrix
If
A
is a matrix then it is possible to find another matrix, called
A
-1 , such that
AA
-1 =
I
or
A
-1
A
=
I .
A
a c b d
A
1 1
A
d c
b a
Example:
A
2 1 3 4
A
1 1 11 4 1 3 2 4 11 1 11 3 11 2 11 24
2
x x
Solution of Systems of Simultaneous Equations
3
y y
11 4 2 1
A
3 1
x y x
11 4 =
b
Ax = b A -1 Ax = A -1 b x = A -1 b 25
2 1
A
3 1
x y x
11 4 =
b A
2 1 3 1
A
1 1 1 1 1 3 2 1 1 3 2
x
A
1
b
1 1 3 2 11 4 ( 1 1 ) 11 11 ( 3 2 ) 4 4 1 3
x
x y
1 3
x y
1 3 26
A
Exercises
1 3 2 4
B
4 1 3 2
C
2 3 2 1 2 1 3 0 Consider the above matrices and complete the following operations:
1.
2.
3.
4.
5.
6.
A + B B – A AB BA AI IA 7.
8.
9.
10.
11.
12.
BC CB C(A + B) BC t (AB) t A t B t
27
Exercises
Solve the following pairs of simultaneous equations using matrices.
a) 2
x x
y y
5 7 d)
x
3
x
y
2
y
6 7 b) 6
x
3
x
2
y
5
y
c)
x
3
x
2
y y
1 8 2 13 e) f) 5
x
4
x
3 7
y y
29 37 4 7
x x
6 5
y y
2 .
5 0 .
25 28
Inverse of a 3×3 matrix
A
a d g b e h i f c A t
a b c d f e g i h
det
A
A
(
aei
bfg
cdh
) (
ceg
bdi
afh
) From the determinan t
A t
:
a
11
a
21
d f f e a
31
d e A
1 1
A i h
ei
fh i g
di
fg g h
dh
eg
a a
11
a
21 31
a
12
a
22
a
32
a
12
b c a
22
a c a
32
a b g g h i h i
bi
ch
ai
ah
bg
a
13
a
23
a
33
cg
a
13
b c a
23
a c a
33
a b d f f e d e
bf ae
ce
af
cd
bd
29
Inverse of a 3×3 matrix (example)
A
1 0 1 1 3 4 0 4 3
A t
1 1 0 1 3 4 0 4 3 det
A
A
( 9 0 0 ) ( 0 16 3 ) 9 13 4 From the determinan t
A t
:
a a a
11 21 31 3 4 1 4 1 3 4 3 0 3 0 4 7 3 4
A
1 1 4 7 3 4 3 3 4
a a
12 22 4 4 4 1 0 1 0
a
32 1 1 4 3 0 0 4 3 3 3 4
a a
a
13 1 0 23 33 1 0 1 1 3 4 1 4 1 4 3 4 4 30
Exercises
1.
Find the inverse of the following matrix: 3
A
2 2 2 1 0 1 2 1 2.
Solve the following simultaneous equations using matrices.
2
x
3
x
x
y y
3
y
z
3
z
2
z
5 4 6 31
Row Operations
Solve any system of equations (that has a solution) Find the inverse of an
n
×
n
matrix
Rules
1.
2.
3.
Can interchange any two rows of a matrix.
Can replace any row by a non zero multiple of the same row.
Can replace any row by the sum of that row and a multiple of some other row.
32
Row Operations
Solve the system of equations
x
2
x
y
3
y
2 2 1 2 1 3
x y
2 2 Solution: 1 2 1 3 2 2 By using the rules transform the matrix into the form 1 0 0 1
h k
1 0 0 1
x y
h k
x y
h
k
33
1 2 1 3 2 2
R
2 =
r
2 – 2
r
1 1 0 1 1 2 2
Solution
1 0
R
2 = -
r
2 1 1 2 2
R
1 =
r
1 +
r
2 1 0 0 1 4 2
x y
4 2 34
Algorithm
n
×
n
1
0 0 0
a a
21
a a
11 31
n
1
a
12
a
22
a
32
a n
2
a
13
a
23
a
33
a
1
m
a n
3
a nm b b
21
b
31
b
11
n
1 0
1
0 0 0 0
1
0 0 0 0
1
a b
c
d
35
Solve the system of equations:
x
3
x
3
x
y
2
y y
z z
2
z
6 4 11 1 3 3 1 2 1 1 1 2 6 4 11
R
2 = (-1)
r
2 1 0 0 1 1 2 1 4 1 6 14 7
R
2
R
3 =
r
2 =
r
3 – 3
r
1 – 3
r
1 1 0 0 1 1 2 1 4 1 6 14 7
R
1
R
3 =
r
=
r
3 1 –
r
2 + 2
r
2 1 0 0 0 1 0 3 4 7 8 14 21
x
= 1,
y
= 2,
z
= 3 1 0 0
R
3 =
r
3 / 7 0 1 0 3 4 1 8 14 3
R
1
R
2 =
r
1 =
r
2 + 3
r
3 - 4
r
3 1 0 0 0 1 0 0 0 1 1 2 3 36
1 0 0
There is always a solution?
0 1 0 0 0 1 3 8 4
x
x x
3 2 1 3 8 4 1 0 0 0 1 0 3 2 0 0 1 1 1 0 0 0 1 0 0 0 1 2 1 3 5 2 4 Third row: 0x1 + 0x2 + 0x3 = -1 or 0 = -1 meaningless, so the system has no solution
x
1
x
2
x
3 2
x
4
x
4 3
x
4 5
x
1 2 or
x
2 4
x
3 5 2
x
4 2
x
4 4 3
x
4 Thus the system has infinitely many solutions 37
Exercises
1. Solve the system of equations: a) 2
x
x
2
x
4
y y
3
y
3
z
2
z z
2 5 3 b) 2 2 3
x x x x
3
y
2 2
y y y
z
2
z z z
3
w
2
w w w
8 2 2 11 2. Find a matrix
A
such that:
A
0 2 1 1 2 1 1 0 2. For what number x will the following be true?
x
4 1 2 1 0 1 0 2 0 2 4 5 4
x
7 0 38
Finding the Inverse using the row operation
A
AA
1 2 0 1 1
I
i.e.
2 0 1
a
1
c b d
1 0 0 1
A
|
I
2 2 0 1 1 1 0 0 1 1 0
R
1 =
r
1 / 2 1 2 1 1 2 0 0 1
R
1 =
r
1 - (1/2)
r
2 1 0 0 1 1 2 0 1 2 1
A
1 1 2 0 1 2 1 39
Example
Find the inverse of the matrix
A
1 2 1 1 1 2 2 0 2 1 2 1 1 1 2 2 0 2 1 0 0 0 1 0 0 0 1 1 0 0 1 1 1
R
2
R
3 =
r
2 =
r
3 - 2
r
1 -
r
1 2 4 0 1 2 1 0 1 0 0 0 1
R
2 = -
r
2 1 1 2 0 1 4 0 1 0 1 2 1 0 0 1 0 0 1 1 0 0 1 0 0
R
1 =
r
1
R
3 2 =
r
3 1 -
r
2 –
r
2 4 2 1 0 1 0 4 3 1 1 40
Example (continue)
1 0 0 1 0 0
R
3 2 = (-1/4)
r
3 1 1 4 1 2 3 4 1 1 4 0 0 1 4 1 0 0 0 1 0
R
1
R
2 =
r
1 + 2
r
3 =
r
2 - 4
r
3 0 0 1 1 2 1 3 4 1 2 0 1 4 4 1 2 1 1
A
1 1 2 1 3 4 1 2 0 1 4 4 1 2 1 1 Verify:
AA
-1 =
I
3 41
Exercises
Find the inverse of the following matrices using the row operations method:
A
2 4 3 1
B
2 2 2 2 1 0 1 3 1
C
2 2 1 1 1 1 0 2 1 3 1 3 2 1 1 0 42
Dimension of a Space
is the amount of freedom of movement that objects within the space have.
0D 1D
x
2D 3D
y y x x
z 43
3D Coordinate Systems
y
z Right-handed system (z comes out)
x y
z
x
Left-handed system (z goes in) 44
Distance
1D 2D
P 1 P 2
d
(
P
1 ,
P
2 )
x
1
x
2
y
P 1 P 2
x d
(
P
1 ,
P
2 ) (
x
1
x
2 ) 2 (
y
1
y
2 ) 2
3D
z
y
P 1 P 2
x d
(
P
1 ,
P
2 ) (
x
1
x
2 ) 2 (
y
1
y
2 ) 2 (
z
1
z
2 ) 2 45
?
3
Trigonometry
?
5 ?
10 20º 46
Equation of the Line
c y
(
x
1 ,
y
1 ) (
x
2 ,
y
2 )
x y
mx
c m
y
2
x
2
y x
1 1 ,
x
1
x
2 47
Equation of a circle at (0,0)
(0, 0)
r
(
x,y
)
Cartesian form
x
2
y
2
r
2
Polar form
x y
rCos
rSin
48
Equation of a circle at (
x c ,y c
)
(
x,y
) (
x c ,y c
)
r r
Cartesian form
(
x
x c
) 2 (
y
y c
) 2
r
2
Polar form
x
rCos
y
rSin
x c y c
49
Exercises
1.
2.
3.
4.
5.
6.
Find the equation of the straight line which passes from the two points (2,8) and (8,26).
Draw the graph
y = x
3 –
x.
Draw the graph
y = x / (x
2 – 1
).
Find the Cartesian coordinates x, y for the circle with center (0,0) and radius 4, when = 0º, 45º, 90º, 150º, 215º, and 340º.
Find the Cartesian coordinates x, y for the circle with center (4,3) and radius 10, when = 30º, 190º, and 325º.
Find the radius of the circle with center (4,6) and passes from point (-2,10).
50
Positions ( points ) & Directions ( vectors ) y z
v=(x,y,z)=(2,2,-1) P 2 =(4,3,1) P 1 =(2,1,2)
x A vector is the difference between two points, i.e.
vector = point
2
– point
1
v = P
2
– P
1 = (4, 3, 1) – (2, 1, 2) = (2, 2,-1)
point 2 = point 1 + vector P
2
= P
1 +
v
= (2, 1, 2) + (2, 2,-1) = (4,3,1) 51
Addition of Vectors
v
1 = (
x
1 ,
y
1 ,
z
1 )
v
2 = (
x
2 ,
y
2 ,
z
2 )
v
1 +
v
2 = (
x
1 +
x
2 ,
y
1 +
y
2 ,
z
1 +
z
2 ) (
x
1
+x
2 ,
y
1
+y
2 ,
z
1
+z
2 ) (
x
2 ,
y
2 ,
z
2 ) (
x
1 ,
y
1 ,
z
1 ) (0, 0, 0) 52
Vector addition
w v v+w
Vector difference
v + w = v + (-w) v -w v-w
Scalar multiplication of Vectors
v
(
x
,
y
,
z
) Vector
OP 2v v (-
1
)v
P
(
1/2
)v
O 53
Length of a vector
|
v
|
x
2
y
2
z
2
Norm of a vector
Only interested on the direction and not in its length.
So norm(
v
) has the same direction but length 1.
norm (
v
) vector
v
length of
v
v
|
v
| 54
Inner (dot) product of two vectors
The dot-product , denoted by
v
1 · relationship between two vectors:
v
2 , is the angular
v
1
v
2
x
1
x
2
y
1
y
2
z
1
z
2 Normalizing each of the vectors
v
1 and
v
2 and then take the dot-product of the normalized vectors we find which is the angle between the two vectors .
cos |
v
1 1 2 | |
v
2 | cos 1 |
v
1 1 2 | |
v
2 | The dot product of a unit length vector with itself is 1.
Vectors that are perpendicular to one another (called orthogonal) have a zero dot product.
55
Exercise
Find the angle between vectors (3,7) and (-4,5).
y
(
-4,5
) (3,7)
x
cos 1 1 2 |
v
1 | |
v
2 | cos 1 58 41 cos 1 23 48.765
1 cos 0.472
61.86
56
Vector cross-product
The cross-product , denoted by
v
1
v
1
v
2 (
y
1
z
2
y
2
z
1 ,
x
2
z
1 ×
v
2 , is the vector:
x
1
z
2 ,
x
1
y
2
x
2
y
1 )
v
1 ×
v
2 is orthogonal to both
v
1 and
v
2 , i.e.
v
1 and
v
2 form a plane and their cross product is normal to that plane.
v
2
v
2
v
1
×v
2
v
1
v
1 Properties: |
v
1
v
1
v
2
v
2 | |
v
2
v
1 ||
v
1
v
2 | sin
-v
1
×v
2
= v
2
×v
1 How to remember the formula:
x
1
y
1
z
1
x
2
y
2
z
2 (
y
1
z
2
x
1
x
1
z y
2 2
y x x
2 2 2
z
1
z y
1 1 ) 57
Spherical coordinates
y P x
=
r
cos cos
r
P
x P y
=
r
sin
P z
=
r
cos sin
z r
is the radial distance of
P
from the origin (
latitude
) is the angle that
P
/2 ≤ < /2 makes with the
xz
-plane. (
azimuth
) is the angle between the
xy
-plane and the plane through
P
and the
y
-axis. 0 ≤ < 2 58
Spherical coordinates
Cartesian coordinates (
P x , P y , P z
) Spherical coordinates (
r
, , )
r
P x
2
P y
2
P z
2 sin 1
P y r
tan 1 tan
P z P x
1
P z P x
if if 2 2 if if
x x x x
0 0 0 and
y
0 and
y
0 0 59
Exercises
1.
Suppose that
P
is at a distance 2 from the origin, it is 60° up from the
xz
-plane and it is along the negative
x
-axis. Find
P
in Cartesian coordinates.
2.
3.
Convert the point (
x, y, z
) = (2, 4, -3) to spherical coordinates.
Convert the point (
r
, , ) = (5, 35°, -67°) to Cartesian coordinates.
60
Origin and Coordinate System
y z
(0,0,0)
x
y x
(0,0)
61