Computer Graphics Lecture Notes
Computer Graphics Lecture Notes
By
2019- 2020
Computer graphic
1
Computer graphic references:
1. [Link],” Computer Graphic with Pascal “, B/C Publishing Company,
1984.
2. [Link] & [Link],” Introduction to Computer Graphic “, Addison –
wesly, 1993.
3. [Link] & [Link],” Computer Graphics “, 2nd Ed., Prentice – Hall,
1994.
Computer Graphics Contents :
2
2-3-4) Ellipse.
3- Two Dimensional Geometric Transformations.
3-1) Introduction.
3-1-1) Translation.
3-1-2) Rotation.
3-1-3) Scaling.
3-1-4) Reflection.
3-1-5) Shearing.
3-2) Matrix Representation of Transformations.
3-2-1) Translation.
3-2-2) Rotation.
3-2-3) Scaling.
3-2-4) Reflection.
3-2-5) Shearing.
4- Two Dimensional Viewing Transaction and Clipping Windows and View Ports.
4-1) Viewing Transformation.
4-2) Clipping.
4-2-1) Rectangular Clipping Windows.
a) Point Clipping.
b) Line Clipping.
- Simple Visibility Algorithm.
- Find Intersection Points.
1) Midpoint Subdivision.
2) Line Intersections and Clipping.
c) Polygon Clipping Algorithm.
4-2-2) Convex and Concave Window.
5- Aspect Ration.
5-1) Graphics Primitives.
5-2) Normalized Device Coordinates.
- Normalization.
6- Three Dimensional Transformations.
6-1) Coordinate System.
6-2) The Transformations.
a) Translation.
b) Rotation.
c) Scaling.
7- 3D Models.
7-1) Why do you see the movie in 3D?
7-2) 3D Modeling.
7-3) 3D Modeling Operations.
7-4) Usage of 3D Modeling.
7-5) 3D Models Features.
3
7-6) The Process of 3D Modeling.
7-7) 3D Models Creating Method.
7-8) Stereoscopy.
4
1-1 Overview :
Computer graphic can be defined as the creation and manipulation of graphic
image by means of computer.
Computer graphic started as a technique to enhance the display of information
generated by a computer. This ability to interpret and represent numerical data in
pictures has significantly increased the computer’s ability to represent information
to the user in a clear and understandable form.
1-3 Remarks :
1- Mode:
divided to:
• Text mode: deal with characters, numbers and symbols.
• Graphics mode: deal with pixels.
2- Picture Elements:
5
• Pixel: is the smallest addressable screen element. It is the smallest piece of
the display screen which we can control.
• Line : has patron or type.
.[Link].[Link] Specification: color, lighting, type, width.
Types of line:
4- Resolution :
Means the number of scan lines and the number of pixels on a line or dots per
unit area.
• Low resolution 300 scan lines & 400 pixels ( lines ).
• High resolution more than 1000 scan lines & 1000 pixels ( lines ).
6
Figure ( 1 ) A raster display image
a) Frame buffer :
Each screen pixel corresponds to a particular entry in a two – dimensional array
residing in memory. This memory is called a frame buffer or bit map.
The number of rows in the frame buffer array equal the number of raster lines
on the display screen, and the number of columns in this array equals the number
of pixels on each raster line.
The current tends is to have the frame buffer accessible to central processing
unit ( CPU ) of the main memory, Thus allowing rapid of the storage image.
Whenever we wish to display a pixel on the screen, a specific value is placed
into the corresponding memory location in the frame buffer array. In figure ( 2 ), a
value of 1 placed in a location in the frame buffer results in the corresponding (
black ) pixel being displayed on the screen.
7
Light pixel
011001
100110
111011
Frame buffer
Number of pixels depending on screen type, so that we have many types likes :-
1) Monochrome 2 color ( black, green ).
2) CGA ( Color Graphic Adapter ) 16 color 320 200 .
3) EGA ( Enhanced Graphic Adapter ) 16 color 640 480 .
4) VGA ( Video Graphic Adapter ) 256 color 640 480 .
5) SVGA ( Super Video Graphic Adapter ) 256 color 1024 768 .
Each screen location pixel and corresponding memory location in the frame
buffer is accessed by an ( X,Y ) integer coordinate pair. The X value refers to
the columns, the Y value refers to the row position.
b) Display controller :
The hardware device reads the contents of frame buffer into a video buffer
which then converts the digital representation of a string of pixel values into analog
voltage signals that are sent serially to the video screen.
Whenever the display controller encounters a value of 1 in a single – bit –
plane frame buffer a high – voltage. Signal is sent to the CRT which truns on the
corresponding screen pixel.
8
c) Scan conversion :
Image are usually defined in terms of equation for example C + D = 5 or
graphic descriptions, such as “ draw a line from A to B “. Scan conversion is the
process of converting this abstract representation of an image into the appropriate
pixel values in the frame buffer.
9
Each pixel in C++ programming language is accessed by a positive integer (
x,y ) coordinate pair, the value x start at origin 0, and increase from left to right,
the y value start at 0 increase from top to bottom, as in figure ( 3 ).
x
0 1 2 3 4 5 6
(4,2)
(3,4)
y
Figure ( 3 ) Screen in Pascal programming language
10
For x := xstart to xend do
plotpoint ( x , y , white ) ;
If xstart > xend then ( to ) in the ( for ) loop must be replaced by down to.
Horizontal line
Y
Vertical line
11
To draw a diagonal line with a slope equal to +1 , we need only repeatedly
increment by one unit both the x and y values from the starting to the ending
pixels. The following Pascal codes draw a diagonal line:
y 2 − y1 y
Slope = =
x 2 − x1 x
x := xstart ;
y := ystart ;
i := 0 ;
while ( x + i ) <= xend do
begin
plotpoint ( x + i , y + i , white ) ;
i := i + 1 ;
end ;
To draw a line with a slope -1 , replace y+i by y-i in the code.
Example :
Show the tracing to draw a diagonal line from ( 0 , 0 ) to ( 3 , 3 ).
Solution:
Point 1 ( 0 , 0 ) , point 2 ( 3 , 3 ).
I ( x+i , y+i )
0 ( 0,0 )
1 ( 1,1 )
2 ( 3,3 )
3 Stop
12
It is a vector generation algorithms ( and curve generation ) which step along
the line ( or curve ) to determine the pixels which should be turned on by using the
numerical method for solving differential equations.
y 2 − y1
yi +1 = yi + x (1)
x2 − x1
Y2
Y
Y1
X1 X2
X
{ approximate the line length }
if | x2 – x1 | >= | y2 – y1 | then length = | x2 – x1 |
else length = | y2 – y1 |
end { if }
{ select the larger of y or x to be raster unit }
( x 2 − x1 )
x =
length
( y2 − y1 )
y =
length
{ round the value rather truncate using the sign function makes the algorithm work
in all quadrates }
13
x = x1 + 0.5 sign(x)
y = y1 + 0.5 sign(y )
begin { main loop }
for i := 1 to length do
begin
plotpoint ( integer ( x ) , integer ( y ) )
x = x + x
y = y + y
end { for loop }
end { main loop }
Example :
Draw a line between a points ( 0 , 0 ) and ( 4, 8 ).
Solution:
x = 0.5 y = 1 length = 8 x = 0.5 y = 0.5
I Plot x y
0.5 0.5
1 0,0 1 1.5
2 1,1 1.5 2.5
3 1,2 2 3.5
4 2,3 2.5 4.5
5 2,4 3 5.5
6 3,5 3.5 6.5
7 3,6 4 7.5
8 4,7 4.5 8.5
9 stop
H.W(1):
14
1 . Write program to draw a line between points ( x1 , y1 ) , ( x2 , y2 ) using
DDA algorithm.
2 . Find the points of a line where the first point ( 5 , 4 ) and the second point
( 8 , 6 ) by using DDA algorithm.
x y
x>y
x = x1
y = y1
x = x 2 − x1
y = y 2 − y1
y 1
e= −
x 2
begin { the main loop }
for i := 1 to x
plot ( x , y )
while ( e >= 0 )
y=y+1
e=e–1
15
end { while }
x=x+1
e = e + y x
end { for }
finish
Example :
By using Bresenham`s algorithm draw a line by used the following points
( 0 , 0 ) and ( 5 , 3 ).
Solution:
x=0 y =0 x = 5 y = 3
i plot X y E
0 0 0.1
1 (0,0) 1 -0.9
1 -0.3
2 (1,1) 2 0.3
3 (2,1) 2 -0.7
3 -0.1
4 (3,2) 4 0.5
5 (4,2) 3 -0.5
5 0.1
6 stop
H.W(2):
16
1 . Find a point of a line between point ( 4 , 6 ) and ( 10 , 9 ) using Bresenham’s
algorithm.
x = x1
y = y1
x = x 2 − x1
y = y 2 − y1
e = 2y − x
begin { the main loop }
for i = 1 to x
plot ( x , y )
while ( e` >= 0 )
y=y+1
e = e − 2x
end { while }
x=x+1
e = e + 2y
end { for }
17
Finish
H.W(3):
1 . Find points of a line between ( 0 , 0 ) and ( 5 , 3 ) using Bresenham’s algorithm,
without division and without floating point.
x = x1
y = y1
x = x 2 − x1
y = y 2 − y1
s1 = sign ( x2 – x1 )
s2 = sign ( y2 – y1 )
{ Interchange x and y depending on the slope of the line. }
if y x then
temp = x
x = y
y = temp
interchange = 1
else
interchange = 0
endif
e = 2y − x
18
{ Main loop }
for i = 1 to x
plot ( x , y )
while ( eَ >= 0 )
if interchange = 1 then
x = x + s1
else
y = y + s2
endif
e = e − 2x
end { while }
if interchange = 1 then
y = y + s2
else
x = x + s1
endif
e = e + 2y
end { for }
Finish
H.W(4):
1 . Find points of a line between points ( 0 , 0 ) , ( -2 , -4 ) using General
Bresenham’s algorithm.
19
4 . Write a program to draw a triangle in any location in the screen without using
a line instruction.
For any given point on the circle in clockwise to generation of it there are
only three possible selections for the next pixel which best represents the circle
horizontally to the right, diagonally downward to the right and vertically
downward. These are labeled: m H , m D , mV , respectively in Figure 4. The
algorithm chooses the pixel which minimizes the square of the distance between
one of these pixels and the true circle, i.e. the minimum of:
mH = ( xi + 1) + ( yi ) − R 2
2 2
mD = ( xi + 1) + ( yi − 1) − R 2
2 2
mV = ( xi )2 + ( yi − 1) − R 2
2
( xi , y i ) mH (xi + 1, yi )
20
mD
mV
Figure 4: First quadrant pixel selections.
The algorithm
X=xC
Y=YC + R
While (y >= 0)
Plot (x,y)
Eh = |(x+1)²+ y²- R²|
Ed = |(x+1)² + (y-1)² - R²|
Ev = |x² +(y-1)²-R²|
Min = minimum (Eh, Ed, Ev)
If min = Eh then X=X+1: go to 1
If min = Ed then X=X+1:Y=Y-1: go to 1
If min = Ev then y = y - 1
1 Endwhile
Solution:
21
X Y Plot Eh Ed Ev MIN
0 8 0,8 1 14 15 Ed=1
1 8 1,8 4 11 14 Eh=4
2 8 2,8 9 6 11 Ed=6
3 7 3,7 1 12 19 Eh=1
4 7 4,7 10 3 12 Ed=3
5 6 5,6 8 3 14 Ed=3
6 5 6,5 10 1 12 Ed=1
7 4 7,4 16 9 6 Ev=6
7 3 7,3 9 4 11 Ed=4
8 2 8,2 21 18 1 Ev=1
8 1 8,1 18 17 0 Ev=0
8 0 8,0 17 17 0 Stop
obtained by reflection through the line y=x to yield the first quadrant. The results
in the first quadrant are reflected through the line x=0 to obtain those in the
second quadrant.
y=x
y=0 5 8 x
Figure 5: Generation of a complete circle from the fist octant
The combined result in the upper semicircle are reflected through the line y=0 to
complete the circle. Bresenham`s Algorithm is consider the first quadrant of an
origin- centered circle. If the algorithm begins at x=0 , y=R, then for clockwise
generation of the circle y is a monotonically decreasing function of x in the
first quadrant. Here the clockwise generation starting at x=0, y=R is chosen.
The center of the circle is ( 0,0 ). See Figure ( 6 ).
(0,R)
R
(R,0)
(0,0) x
The circle equation, when the center ( a,b ), and the radius R, is:
( x − a )2 + ( y − b )2 = R2 ( 1 )
23
And when the center of this circle is the origin (0, 0), then the equation:
x2 + y2 = R2 Because of a, b = 0.
The different between the square of the distance from the center of the circle to
the diagonal pixel at (xi + 1 , yi − 1) and the distance to a point on the circle R² is:
i = (xi + 1) + ( yi − 1) − R 2
2 2
( 2 )
If i < 0 then find = m H − m D ( 3 )
= ( x n ) + ( y n − 1) − R 2 + 2 x n + 1
2 2
= ( x + 1) + ( y − 1) − R 2 + 2 x n + 1
2 2
n = + 2 xn + 1
x n = x + 1 , y n = y
2- Diagonal Case:
n = ( x n + 1) + ( y n − 1) − R 2
2 2
= (xn ) + ( y n ) − R 2 + 2 xn − 2 y n + 2
2 2
= ( x + 1) + ( y − 1) − R 2 + 2 x n − 2 y n + 2
2 2
n = + 2 xn − 2 y n + 2
x n = x + 1 , y n = y − 1
3- Vertical Case:
24
n = ( xn + 1) + ( y n − 1) − R 2
2 2
= ( xn + 1) + ( y n ) − R 2 − 2 y n + 1
2 2
= ( x + 1) + ( y − 1) − R 2 − 2 y n + 1
2 2
n = − 2 yn + 1
x n = x , y n = y − 1
Limit = 0
1 plot ( xi , y i )
If yi limit then 4
If i 0 then 2
If i 0 then 3
If i = 0 then 20
2 = 2 i + 2 y i − 1
If 0 then 10
If 0 then 20
3 = 2 i − 2 xi − 1
25
If 0 then 20
If 0 then 30
Perform the moves
Move m H
xi = xi + 1
10
i = i + 2 xi + 1
Goto 1
Move m D
xi = xi + 1
20 y i = yi − 1
i = i + 2 xi − 2 y i + 2
Goto 1
Move mV
yi = yi − 1
30
i = i − 2 yi + 1
Goto 1
4 finish.
Example : find the pixels in first quadrant of circle where R=6, center =(0,0) by
using Bresenham`s algorithm, and draw them.
26
Solution:
x = 0 , y = r = 6 , i = −10 , limit = 0
Plot i x y
(0,6) -10 - - 0 6
(1,6) -7 -9 - 1 6
(2,6) -2 -3 - 2 6
(3,5) -4 7 - 3 5
(4,4) -2 1 - 4 4
(5,3) 4 3 - 5 3
(6,2) 14 - -3 6 2
(6,1) 13 - 15 6 1
(6,0) 14 - 13 6 0
2 R=6
1 2 3 4 5 6 7 8 9
27
( χ-χc )² + ( у-уc )² = R²
χ , у :- point on the boundary
χc , уc :- the center of the circle
R :- ½ Radios
У = уc ± R² - ( χ – χc )²
0, yc+ R
0 , уc - R
У := Yc ± R² - ( X – Xc )²
Example : Draw a circle by using circle equation, when R=5, Xc=0 , Yc=0.
Solution:
28
For X: = -R To +R
У= R² - X²
For x:= -5 to +5
Y= 52 - X²
X Y Plot
-5 0 (-5,0)
-4 -3,+3 (-4,-3),(-4,3)
-3 -4,+4 (-3,-4),(-3,4)
-2 (-2,-5),(-2,5)
-1 (-1,-5).(-1,5)
0 -5,+5 (0,-5),(0,5)
1 (1,-5),(1,5)
2 (2,-5),(2,5)
3 -4,+4 (3,-4),(3,4)
4 -3,+3 (4,-3),(4,3)
5 0 (5,0)
2.3.4 Ellipse
- An ellipse is a variation of a circle.
29
- Stretching a circle in one direction produce an ellipse
- We shall examine only ellipse that are stretched in the x or y direction.
- The polar equation for this type of ellipse centered at (χc,уc) are:-
χ = χc+ a * cos ( )
У= уc+ b * sin ( ) (1)
b
b
a a
χc,у χc,уc
c
The values of (a) and (b) effects the shape of the ellipse :
- If b>a the ellipse is longer in the у-direction.
- If a>b the ellipse is longer in the χ- direction.
The ellipse can be drawn using four-points summitry:
- If (c,d) lies on the ellipse, so do the points (-c,d),(c,-d) and (-c,-d).
The following incremental equations for an ellipse are derived from equation
(1)
X2 = X1 * cos (d ) – (a/b) Y1 * sin ( d )
Y2 = Y1 * cos (d ) + (b/a) X1 * sin ( d )
30
H.W ( 5 ):
1 . Draw a circle by using Circle generation Algorithm , when C=0, R= 10.
3. Find the pixels of quadrant circle where center=(0,0) and Radius=10, by using
Bresenham`s incremental circle algorithm. Then draw these pixels.
6. write program to draw the following figure (without using ellipses instruction )
when xc=300, yc=200, a=130, b=30.
31
3.1 Introduction:
Geometric transformations provide a mean by which an image can be
constructed or modified. The transformations we examine in this sheet are
translation, scaling, rotations, reflection and sharing.
The advantage of used the translation: -
• Details appear more clearly.
• Reduces a picture more of if is visible.
• Change the scale of a symbol.
• Rotate it through some angle.
3.1.1 Translation:
a point ( x , y ) is translated to a new position ( x ` , y` ) by move it H units in
the horizontal direction and V units in the vertical direction ( figure ( 7 )).
( x` , y` )
(x,y) V
H
x
32
The H and V represent the horizontal and vertical displacement or distance that
the point has moved. If H is positive, the point moves to the right, if H is negative
the point moves to the left, similarly, a point V moves the point up, a negative V
moves it down. Remember that to move object we must translate every point
describing the object.
To translate an object in an image we must translate every point defining the
object. All point, are displaced the same distance and the object is draw using these
transformed points.
Example :
Consider a triangle defined by it three vertices ( 40 , 0 ), ( 80 , 0 ), ( 60 , 100 ) be
translated 120 units to the right and 20 units up.
Solution:
100
80
60
40
20
0
20 40 60 80 100 120
33
120
100
80
60
40
20
0
20 40 60 80 100 120 140 160 180 200
40 0 160 20
80 0 200 20
rightup
⎯⎯ ⎯
⎯→
( 120 , 20 )
60 100 180 120
before after
H.W(6):
1 . Write procedure to translated any picture ( up , down , right , left ).
34
3.1.2 Rotation:
Another useful transformation is the rotation of an object about a specified
pivot point. After the object has been rotated, it is still the same distance away from
the pivot point, however its orientation has been changed. It is possible to rotate
one or clockwise ( negative angle ) or counterclockwise ( positive angle ) direction.
Any point ( x , y ) can be represented by its radial distance, r, from the origin and
y ( x` , y` )
r
(x,y)
r
0 x
x = r * cos ( )
y = r * sin ( ) (1)
35
If ( x , y ) is rotated an angle in the counterclockwise direction. The transformed
point ( x` , y` ) is represented as :
x = r * cos ( + )
y = r * sin ( + ) ( 2)
Using the laws of sines and cosines from trigonometry, the equation ( 2 ) become:
x` = x * cos ( ) – y * sin ( )
y` = y * cos ( ) + x * sin ( ) (4)
Step 1: Translate
Translate the pivot point ( xp , yp ) to the origin. Every point ( x , y ) defining
the object is translated to a new point ( x` , y` ) where:
x` = x - xp
y` = y - yp
Step 2: Rotate
Use these translated points ( x` , y` ), degree about the origin to obtain the new
point ( x`` , y`` ) where:
36
By substituting for x` and y` :
Step 3: Translate
Translate the center of rotation back to the pivot point ( xp , yp ).
x``` = x`` + xp
y``` = y`` + yp
x` = x cos ( ) + y sin ( )
y` = - x sin ( ) + y cos ( )
y y y
xp , yp
xp , yp
Example :
37
y` = - x sin ( ) + y cos ( )
0 100 x
H.w(7):
1 . Rotate the triangle ( 10 , 0 ) , ( 30 , 0 ) , ( 50 , 80 ) 45o counterclockwise about
the origin.
38
5 . Write an equation to rotate any picture clockwise about the pivot point.
3.1.3 Scaling :
An object can be made by larger by increasing the distance between the points
describing the object. In general, we can change the size of an object, or the entire
image, by multiplying the distance between points by an enlargement or reduction
factor. This factor is called the “ scaling factor “, and the operation that changes
the size is called scaling. If the scaling is greater than 1, the object is enlarge, if the
factor is less than 1, the object is made smaller, a factor of 1 has no effect on the
object. Whenever scaling is performed, there is one point that remains at the same
location. This is called the fixed point of the scaling transformation.
x` = x * Sx
y` = y * Sy
Example :
Scale the triangle ( 4 , 4 ) , ( 7 , 8 ) , ( 10 , 5 ) by Sx = 2 and Sy = 2, about the
origin point.
Solution:
The new points are:
( 8 , 8 ) , ( 14 , 16 ) , ( 20 , 10 )
39
10 16 (14,16)
8 (7,8) 14
6 12 (20,10)
4 (4,4) (10,5) 10
2 8 (8,8)
2 4 6 8 10 8 10 12 14 16 18 20
Step 2: Scaling
x` = x * Sx
y` = y * Sy
Step 3:
x``` = x`` + xp
y``` = y`` + yp
By substituting:
X```= ( x - xp)*Sx+ xp
Y```= ( y- yp )*Sy+yp
40
H.w(8):
1 . Magnify the triangle ( 0 , 0 ) , ( 8 , 10 ) , ( 12 , 4 ), 4 times its size, about the
origin point.
41
3.2.1 Translation:
1 0 0
x y 1 = x y 1 0 1 0
H V 1
3.2.2 Scaling:
S x 0 0
x y 1 = x y 1 0 S y 0
0 0 1
3.2.3 Rotation:
(a ) Counterclockwise direction :
cos ( x ) sin ( x ) 0
x y 1 = x y 1 − sin ( x ) cos ( x ) 0
0 0 1
(b ) Clockwise direction :
cos ( x ) − sin ( x ) 0
x y 1 = x y 1 sin ( x ) cos ( x ) 0
0 0 1
Example :
Consider a triangle defined by it three vertices ( 40 , 100 ), ( 20 , 0 ), ( 60 , 0 )
be translated 20 units to the right, using matrix representation.
42
Solution:
40 100 1 1 0 0 60 100 1
20 0 1 0 1 0 = 40 0 1
60 0 1 20 0 1 80 0 1
100
80
60
40
20
0 20 40 60 80 X
Example :
Rotate the triangle ( 7 , 8 ) , ( 4 , 4 ) , ( 10 , 5 ) 90o counterclockwise about the
point ( 7 , 8 ), using matrix representation.
Solution:
1) Translate:-
7 8 1 1 00 0 20 0 40
0 1 60 80 X
4 4 1 0 1 0 = − 3 − 4 1
10 5 1 − 7 − 8 1 3 − 3 1
2) Rotate: -
0 0 1 cos (90) sin (90) 0 0 0 1
− 3 − 4 1 − sin (90) cos (90) 0 = 4 − 3 1
3 − 3 1 0 0 1 3 3 1
43
3) Translate: -
0 0 1 1 0 0 7 8 1
4 − 3 1 0 1 0 = 11 5 1
3 3 1 7 8 1 10 11 1
10
0 2 4 6 8 10 X
Example :
Magnify the triangle ( 0 , 0 ) , ( 8 , 10 ) , ( 12 , 4 ), 4 times its size, using matrix
representation.
44
Solution:
0 0 1 4 0 0 0 0 1
8 10 1 0 4 0 = 32 40 1
12 4 1 0 0 1 48 16 1
50
40
30
20
10
0 10 20 30 40 50 X
3.2.4 Reflection :
It is a transformation that produced a mirror image of an object; the mirror image
is generated relative to an axis of reflection.
There are different types of reflection:
1 - Reflection about X – axis:
x = x
y = − y
1 0 0
x y 1 = x y 1 0 −1 0
0 0 1
45
2 - Reflection about Y – axis:
x = − x
y = y
− 1 0 0
x y 1 = x y 1 0 1 0
0 0 1
− 1 0 0
x y 1 = x y 1 0 − 1 0
0 0 1
0 1 0
x y 1 = x y 1 1 0 0
0 0 1
0 − 1 0
x y 1 = x y 1 − 1 0 0
0 0 1
46
Example :
Reflect the shape (20, 70), (40, 50), (60, 70), (40, 90), about:
1- X – axis 2- Y- axis
3- origin (0,0) 4- y = x 5- y = -x , by used matrix
representation, and draw the result.
Solution:
1- X – axis:
x = x
y = − y
20 70 1 20 − 70 1
40 1 0 0
50 1 40 − 50 1
0 − 1 0 =
60 70 1 60 − 70 1
0 0 1
40 90 1 40 − 90 1
2- Y- axis:
x = − x
y = − y
20 70 1 − 20 70 1
40 − 1 0 0
50 1 − 40 50 1
0 1 0 =
60 70 1 − 60 70 1
0 0 1
40 90 1 − 40 90 1
3- origin (0,0):
x = − x
y = − y
20 70 1 − 20 − 70 1
40 − 1 0 0
50 1 − 40 − 50 1
0 − 1 0 =
60 70 1 − 60 − 70 1
0 0 1
40 90 1 − 40 − 90 1
47
4- y = x:
x = y
y = x
20 70 1 70 20 1
40 0 1 0
50 1 40 1
1 0 0 =
50
60 70 1 70 60 1
0 0 1
40 90 1 90 40 1
5- y = -x:
x=− y
y=− x
20 70 1 − 70 − 20 1
40 0 − 1 0
50 1 − 50 − 40 1
− 1 0 0 =
60 70 1 − 70 − 60 1
0 0 1
40 90 1 − 90 − 40 1
48
y
100
90
y= -x 80 y=x
70
2 Origin
60
50
40
30
4
20
x` 10 x
- 100 -90 -80 -70 -60 -50 -40 -30 -20 -10 10 20 30 40 50 60 70 80 90 100
-10
-20
-30
5
-40
-50
-60
-70
3 1
-80
-90
-100
y`
49
3.2.5 Shearing :
A shearing transformation produces a distortion of an object or the entire
image. There are two types of shearing (Y-shear & X-shear).
1 ) Y-shear transforms the point (x,y) to the point ( x',y' ) , where :-
X = X
Shy # 0
Y = Shy * X + Y
A Y-shear moves a vertical line up or down, depending on the sign of the shear
factor Shy. A horizontal line is distorted into a slanted line with slop Shy.
2 ) X-shear has the opposite effect. That is, the point ( x',y' ) is transformed to
the point (x,y) , where :-
X = X + Shx * Y
Shx # 0
Y = Y
A vertical line becomes slanted line with slop Shx and the horizontal lines are
shifted to the right or left, depending on the sign of Shx.
50
3.2 Matrix Representation of Shearing
1:- Y-shear
1 shy
[ x y ] = [ x y ] 0 1
2x2
3x3
2:- X-shear
1 0
[ x y ] = [ x y ] shx 1
2x2
3x3
Example :
Shear the object (1,1), (3,1),(1,3), (3,3) with
a: shx = 2
b: shy =2
solution:
a: shx
51
b: shy
52
H . W ( 9 ):
2 . Rotate the above triangle 90o clockwise about the point ( 10 , 12 ), using matrix
representation.
3 . Translated the shape above 20 units to the left, and 10 units up. Using matrix
representation.
4 . Reflect the shape (2, 2), (4, 4), (6, 2), about:
a- X – axis b- Y- axis
c- origin (0,0) d- y = x e- y = -x , then draw the result.
53
4. Two – dimensional viewing
Transaction and clipping Windows and
view ports:
- If we do not wish to use the entire screen for display, we can imagine a box on
the screen and have the image confined to that box such a box in the screen space
is called a view port.
- When the window is hanged a different part of the object at the same positron is
displayed.
- If we change the view port, we see the same part of the object drawn at a different
place on the display.
54
4.1 Viewing Transformation :
Mapping from object space to image space,
1. Change the window size to become the size of the view port (Scaling).
2. Position the window at the desired location on the screen (Translate) by moving
the Lower-left corner of the window to the view port Lower-left corner location.
To do this we need 2 step:
Step 1:
Move the corner to the origin (to perform the necessary scaling without disturbing
the corner the corner's position).
55
Step 2:
Move it to the view port corner location.
Example :
A window has left and right boundaries of 3 of 5 and lower and upper
boundaries of 0 and 4. The view port is the upper-right quadrant of the screen with
boundaries at 0.5 and 1.0 for both X and Y direction, find the viewing
transformation?
1- Translate :
2- Scaling :
The length of the window is
3- Translate :
56
The viewing transformation is then:
V: View port
W: Window
X: Position of a vertical boundary
Y: Horizontal boundary
H: High boundary
L: Low boundary
57
4.2 CLIPPING:
If we wish to display only a portion of the total picture, we use a window to
select that portion of the picture which is to be viewed (like clipping or cutting out
a picture from a magazine). This is known as clipping.
The process of clipping determines which elements of the picture lie inside the
window and so are visible.
a ) POINT CLIPPING:
A point p(x,y) is inside the rectangular window (visible) if all the following
inequalities are true.
If any of these inequalities is false point P is outside the window and is not
displayed (invisible).
58
Point Clipping algorithm
P(x , y) نقطة إحداثياهتا, XL , XR , YT , YB معلومة
b) Line Clipping :
1001 1000 1010
0001 0000 0010
0101 0100 0110
1: Identify those lines which intersect the window and so need to be clipped.
59
All line segments fall into one of the following clipping categories:
1 – Visible:
Both endpoints of the line segment lie within the window (Line AB).
2 – Not visible:
The line segment definitely lies outside the window (Line CD and EF). This will
occur if the line segment from (X1, Y1) to (X2, Y2) satisfies any one of the
following four in qualities
X1, X2 > X max Y1, Y2 > Y max X1, X2 < X min Y1, Y2 < Y min
3 – Clipping candidate:
The line is in neither category 1 nor 2 (Line GH, IJ, and KL)
60
1- Simple visibility algorithm.
Check for totally visible lines.
If ((xb < xL ) OR (xb > xR)) then 1 .
If ((xe < xL ) OR (xe > xR)) then 1 .
If ((yb < yB) OR (yb > yT )) then 1 .
If ((ye < yB) OR (ye > yT )) then 1 .
Draw line
Go to 3
1 - Check for totally invisible lines
if ((xb < xL) AND (xe < xL)) then 2
if ((xb > xL) AND (xe > xL)) then 2
if ((yb < yL) AND (ye < yB)) then 2
if ((yb > yT) AND (ye > yT)) then 2
The line is partially visibly or diagonally crosses the corner; determine the
intersections go to 3
2 line is invisible
3 next line
61
Find Intersection Points
1 - Midpoint Subdivision :
The line segment is divided at its midpoint into two smaller line segments.
The clipping categories of the two new line segments are then determined. Each
segment in category 3 is divided again into smaller segment and categorized. The
bisection and categorization process continues until all segments are in category 1
(visible) or category 2 (invisible).
The midpoint coordination (Xm , Ym) of a line segment joining P1 (X1 , Y1) to
P2 (X2 , Y2) are given by
m= X1 + X2 m= Y1 + Y2
2 2
We determine the intersection points of the lines in category (3) with the
boundaries of the window. The intersection points subdivided the line segment
into several smaller line segments which can belong only to category 1 (visible)
or category 2 (not visible). The segment in category 1 will be the clipped line
segment.
62
INTERSECTION POINT:
If M is the slope of the line segment between points (X1, Y1) and (X2, Y2)
then if X1 ≠ X2
( Y2 – Y1 )
( X2 – X1 )
( Y - Y1 )
( X - X1 )
If the line segment crosses a left or right window edge then X1 ≠ X2 and M
has non denominator.
If the line crosses a top or bottom widow edge then Y1 ≠ Y2 and the reciprocal
of the slop 1/m has a nonzero denominator.
If we are testing against a left or right direction the X value is known (the left
or right edge value).
Y = M * (X – X1) + Y1
X = 1/M * (Y - Y1) + X1
63
c ) Polygon Clipping Algorithm :
Case – 1 :
If the first point and the second point inside the window then store second
point.
Case – 2:
If the first point inside and the second point outside the window then store
the intersection.
Case – 3:
If the first point and the second point outside the window then nothing.
Case – 4:
If the first point outside and the second point inside the window then store
the intersection and the second point .
64
) Bottom , right , top , left ( مرات4 نطبق العملية
Left : - V2 P1 P2 V1
Bottom : - P1 P2 V1 V2
Right : - P2 V1 V2 P1
Top : - V1 V2 P1 P2
- For each edge it inputs a list of vertices and outputs a new list of vertices.
- The input list is a sequence of consecutive vertices of the polygon obtained from
the previous edge clipping.
Example :
Case 1:
First and second V1,V2 inside the window, V2 is sent to the output list.
65
Case 2:
First vertex V2 inside and the second vertex V3 outside the window, the
intersection point (P1) of the side of the polygon joining the vertices and the edge
is added to the output list.
Case 3:
Case 4:
First vertex V4 outside the window and the second vertex V1 inside the
window, the intersection point (P2) and the second vertex V1 are added to the
output list.
66
4.2.2 Convex and Concave Window:
(10,-10) = V4
V1 = (x2 - x1 , y2 - y1 ) = ( 30 – 10 , 10 – 10 ) = ( 20 , 0 )
V2 = (x2 - x1 , y2 - y1 ) = ( 30 – 30 , 30 – 10 ) = ( 0 ,20 )
V3 = (x2 - x1 , y2 - y1 ) = ( 10 – 30 , 30 – 30 ) = ( -20 ,0 )
V4 = (x2 - x1 , y2 - y1 ) = ( 20 – 10 , 20 – 30 ) = ( 10 ,-10 )
67
20 0
V1 V 2 = = 400
0 20
0 20
V 2 V 3 = = 400
− 20 0
− 20 0
V 3 V 4 = = 200
10 − 10
10 − 10
V 4 V 5 = = −200
− 10 − 10
− 10 − 10
V 5 V1 = = 200
20 0
H . W ( 10 ) :
1. A window has been set to set-window ( 2,5,3,7 ), and a view port has been set
to set-viewport ( 0,0.5,0.5,1 ). Find the viewing transformation.
a) P1(6,7).
a) P1(7,7)
b) P2(18,20)
c) P3(8,16)
68
4 . If the clipping window is XL = 5, XR = 10, YB = 5, YT = 10. Clip the lines,
using simple visibility algorithm:
a) AB = ( 6,6 ) – ( 8,7 )
b) EF = ( 14,8 ) – ( 12,14 )
c) IJ = ( 1,6 ) – ( 7,8 )
polygon
window
69
5- Aspect Ratio
display screen.
Example :
If we plot eight pixels horizontally on the display screen and
eight pixels vertically on the display screen and then we measure the
= 10.64 ≈ 11 pixels.
70
5.1 Graphics Primitives
71
) 0,0 ( ( (
0,1
Center
) 0.5 , 0.5 (
) 1,0( ) 1,1 (
72
Xs width
ـــ = ــ
Xn 1
Screen normalized
Normalization
هي عملية تحويل إحداثيات الشاشة إلى إحداثيات جديدة وتفيد في عملية تنفيذ البرامج على جميع أنواع
الشاشات ومهما كان حجم الشاشة .
وتجري عملية التحويل وكاالتي- :
-أخذ جميع إحداثيات الصورة على ) max (x-1و ( max )y-1وتكون محصورة ما بين ( )1 , 0
سواء على المحور xأو المحور . Y
Example :
If we have points P1(0,0) , p2(200,200) p3(1023,1023) , convert these points
? from screen 1024 * 1024 to appropriate coordinates in screen 800*600
العملية األولى -:تقسيم إحداثيات الشاشة المصدر ناقص واحد.
MaxX = 1023(1024 = 0..1023).
MaxY = 1023(1024 = 0..1023).
We use the equation
Xr = X / maxX ; Yr = Y / maxY
For all points so we get:-
( p1 * = ) 0/1023 ,0/1023( ≈ ) 0,0
)P2 * = )200/1023 ,200/1023( ≈ (0.195,0.195
)P3 * = )1023/1023 ,1023/1023( ≈ (1,1
73
. نضرب النسبة في الشاشة الجديدة ناقص واحد-: العملية الثانية
MaxX = 799(800 = 0..799).
MaxY = 599(600 = 0..599).
By using the equation
X = Xr * maxX ; Y = Yr * maxY
For all points so we get:-
p1 * = (0*799, 0*599) ≈ )0,0(
P2 * = (0.195*799 , 0.195*599 ) ≈ (156 , 116)
P3 * = (1*799, 1*599) ≈ (799,599)
74
6- Three – Dimensional Transformations
The world composed of three – dimensional images so the object has height,
width and depth. The computer uses a mathematical model to create the image.
75
6.2 The Transformations:
a. Translation :-
A point ( x , y , z ) is translated to a new position
( x1 , y1 , z1 ) by move it dx units in the X – direction and by units in the Y –
direction and dz units in Z – direction. Mathematically this can be represented as:-
X 1 = X + dx
Y1 = Y + dy
Z1 = Z + dz
1 0 0 0
0 1 0 0
X 1 Y1 Z1 1 = X Y Z 1
0 0 1 0
dx dy dz 1
b. Scaling :-
Increasing the distance, between the points describing the object can make an
object. In general, we can change the size of an object, or the entire image, by
multiplying the distance between points by an enlargement or reduction factor. This
factor is called the scaling factor, and the operation that the size is called scaling.
If the scaling factor is greater than 1, the object is enlarge, if the factor is less than
1, the object is made smaller, a factor of 1 has no effect on the object. Whenever
scaling is performed, there is one point that remains at the same location. This is
called fixed point of the scaling transformation.
a) To scale an object from a origin point:-
We used the following matrix.
S x 0 0 0
0 Sy 0 0
0 0 Sz 0
0 0 0 1
76
b) To scale an object from fixed point ( xp , yp , zp ), we perform the following
three steps:
2. Scale these translate points with the origin as the fixed points:
x 2 = x1 S x
y 2 = y1 S y
z 2 = z1 S z
1 0 0 0 S x 0 0 0 1 0 0 0
0 0 0 S y 0 0
0 0 1 0
X 1 Y1
Z 1 1 = X Y Z 1
0
1
0
0
1
0 0 0 S z
0 0 0 1 0
− x p − y p − z p 1 1 1 1 1 x p y p z p 1
c. Rotation :-
77
1. Rotation about X – axis:
1 0 0 0
0 cos( ) sin( ) 0
R x ( ) =
0 − sin( ) cos( ) 0
0 0 0 1
cos( ) 0 − sin( ) 0
0 1 0 0
Ry ( ) =
sin( ) 0 cos( ) 0
0 0 0 1
cos( ) sin( ) 0 0
− sin( ) cos( ) 0 0
Rz ( ) =
0 0 1 0
0 0 0 1
Example :
Draw the figure ( 0 , 0 , 0 ) , ( 0 , 1 , 0 ) ,( 0 , 1 , 3 ) , ( 0 , 0 , 3 ) , ( 2 , 0 , 0 ) ,
( 2 , 1 , 0 ), ( 2 , 0 , 3 ) , ( 2 , 1 , 3 ), and find: Not: sin (90) = 1 , cos (90) = 0.
a) Translate it to the point ( 0 , 3 , 0 ).
b) Scaling 4 times its size about the origin point.
c) Rotate its ( 90o ) about the Z – axis.
78
Solution:
(0,0,3)
(2,0,3) (0,1,3)
(2,1,3)
(0,0,0)
(0,1,0)
(2,0,0)
X (2,3,0) Y
0 0 0 1 0 3 0 1
0 1 0 1 0 3 0 1
0 1 3 1 1 0 0 0 0 4 3 1
0 0 3 1 0 1 0 0 0 3 3 1
=
2 0 0 1 0 0 1 0 2 3 0 1
2 3 0 1 0 3 0 1 2 6 0 1
2 0 3 1 2 3 3 1
2 1 3 1 2 4 3 1
79
b) Scaling 4 times its size.
0 0 0 1 0 0 0 1
0 1 0 1 0 4 0 1
0 1 3 1 4 0 0 0 0 4 12 1
0 0 3 1 0 4 0 0 0 0 12 1
=
2 0 0 1 0 0 4 0 8 0 0 1
2 3 0 1 0 0 0 1 8 12 0 1
2 0 3 1 8 0 12 1
2 1 3 1 8 4 12 1
0 0 0 1 0 0 0 1
0 1 0 1 −1 0 0 1
0 1 3 1 cos(90) sin(90) 0 0 − 1 0 3 1
0 0 3 1 − sin(90) cos(90) 0 0 0 0 3 1
=
2 0 0 1 0 0 1 0 0 2 0 1
2 3 0 1 0 0 0 1 − 3 2 0 1
2 0 3 1 0 2 3 1
2 1 3 1 − 1 2 3 1
H . W ( 11 ):
Draw the figure A ( 4 , 4 , 0 ) , B ( -3 , 3 , 4 ) , C ( -2 , 3 , 3 ), D ( 3 , -3 , 4 ) ,
E( 3 , -2 , 3 ) and find:- Not: sin (180) = 0 , cos (180) = -1
a) Translate above shape to point ( -3 , 4 , 3 ).
b) Scaling the figure, twice in X direction, three in Y direction and once in Z
direction.
80
7- 3D Models:
7.2 3D Modeling Operations:
Modeling:
81