Computer Graphics Notes
Computer Graphics Notes
The primary use of clipping in computer graphics is to remove objects, lines, or line segments
that are outside the viewing pane. The viewing transformation is insensitive to the position of
points relative to the viewing volume – especially those points behind the viewer – and it is
necessary to remove these points before generating the view.
Point Clipping
Clipping a point from a given window is very easy. Consider the following figure, where the
rectangle indicates the window. Point clipping tells us whether the given point (X, Y) is within
the given window or not; and decides whether we will use the minimum and maximum
coordinates of the window.
The X-coordinate of the given point is inside the window, if X lies in between Wx1 ≤ X ≤ Wx2.
Same way, Y coordinate of the given point is inside the window, if Y lies in between Wy1 ≤ Y ≤
Wy2.
Line Clipping
The concept of line clipping is same as point clipping. In line clipping, we will cut the portion of
line which is outside of window and keep only the portion that is inside the window.
21
Computer Graphics
We will use 4-bits to divide the entire region. These 4 bits represent the Top, Bottom, Right,
and Left of the region as shown in the following figure. Here, the TOP and LEFT bit is set to 1
because it is the TOP-LEFT corner.
1. Line can be completely inside the window (This line should be accepted).
2. Line can be completely outside of the window (This line will be completely removed from
the region).
22
Computer Graphics
3. Line can be partially inside the window (We will find intersection point and draw only
that portion of line that is inside region).
Algorithm
Step 1: Assign a region code for each endpoints.
Step 2: If both endpoints have a region code 0000 then accept this line.
Step 3: Else, perform the logical AND operation for both region codes.
Step 3.1: If the result is not 0000, then reject the line.
Step 3.2.1: Choose an endpoint of the line that is outside the window.
Step 3.2.2: Find the intersection point at the window boundary (base on region code).
Step 3.2.3: Replace endpoint with the intersection point and update the region code.
Step 3.2.4: Repeat step 2 until we find a clipped line either trivially accepted or trivially
rejected.
23
Computer Graphics
Let Ni be the outward normal edge Ei. Now pick any arbitrary point PEi on edge Ei then the dot
product Ni∙[P(t) – PEi] determines whether the point P(t) is “inside the clip edge” or “outside”
the clip edge or “on” the clip edge.
Ni∙[P(t) – PEi] =0
Ni∙[P0 – PEi] = - Ni ∙ tD
24
Computer Graphics
First the polygon is clipped against the left edge of the polygon window to get new vertices of
the polygon. These new vertices are used to clip the polygon against right edge, top edge,
bottom edge, of the clipping window as shown in the following figure.
While processing an edge of a polygon with clipping window, an intersection point is found if
edge is not completely inside clipping window and the a partial edge from the intersection point
to the outside edge is clipped. The following figures show left, right, top and bottom edge
clippings:
25
Computer Graphics
Text Clipping
Various techniques are used to provide text clipping in a computer graphics. It depends on the
methods used to generate characters and the requirements of a particular application. There
are three methods for text clipping which are listed below:
26
Computer Graphics
In all or none string clipping method, either we keep the entire string or we reject entire string
based on the clipping window. As shown in the above figure, STRING2 is entirely inside the
clipping window so we keep it and STRING1 being only partially inside the window, we reject.
This clipping method is based on characters rather than entire string. In this method if the
string is entirely inside the clipping window, then we keep it. If it is partially outside the window,
then:
27
Computer Graphics
This clipping method is based on characters rather than the entire string. In this method if the
string is entirely inside the clipping window, then we keep it. If it is partially outside the window,
then
Bitmap Graphics
A bitmap is a collection of pixels that describes an image. It is a type of computer graphics that
the computer uses to store and display pictures. In this type of graphics, images are stored bit
by bit and hence it is named Bit-map graphics. For better understanding let us consider the
following example where we draw a smiley face using bit-map graphics.
Now we will see how this smiley face is stored bit by bit in computer graphics.
28
Computer Graphics
By observing the original smiley face closely, we can see that there are two blue lines which
are represented as B1, B2 and E1, E2 in the above figure.
In the same way, the smiley is represented using the combination bits of A4, B5, C6, D6, E5,
and F4 respectively.
We cannot resize the bitmap image. If you try to resize, the pixels get blurred.
Colored bitmaps can be very large.
29
Computer Graphics
7. 3D Graphics
In the 2D system, we use only two coordinates X and Y but in 3D, an extra coordinate Z is
added. 3D graphics techniques and their application are fundamental to the entertainment,
games, and computer-aided design industries. It is a continuing area of research in scientific
visualization.
Furthermore, 3D graphics components are now a part of almost every personal computer and,
although traditionally intended for graphics-intensive software such as games, they are
increasingly being used by other applications.
Parallel Projection
Parallel projection discards z-coordinate and parallel lines from each vertex on the object are
extended until they intersect the view plane. In parallel projection, we specify a direction of
projection instead of center of projection.
In parallel projection, the distance from the center of projection to project plane is infinite. In
this type of projection, we connect the projected vertices by line segments which correspond
to connections on the original object.
Parallel projections are less realistic, but they are good for exact measurements. In this type of
projections, parallel lines remain parallel and angles are not preserved. Various types of parallel
projections are shown in the following hierarchy.
38
Computer Graphics
Top
Front
Orthographic
Side
Parallel
Axonometric
Projection
Cavalier
Oblique
Cabinet
Orthographic Projection
In orthographic projection the direction of projection is normal to the projection of the plane.
There are three types of orthographic projections:
Front Projection
Top Projection
Side Projection
39
Computer Graphics
Oblique Projection
In orthographic projection, the direction of projection is not normal to the projection of plane.
In oblique projection, we can view the object better than orthographic projection.
There are two types of oblique projections: Cavalier and Cabinet. The Cavalier projection
makes 45˚ angle with the projection plane. The projection of a line perpendicular to the view
plane has the same length as the line itself in Cavalier projection. In a cavalier projection, the
foreshortening factors for all three principal directions are equal.
The Cabinet projection makes 63.4˚ angle with the projection plane. In Cabinet projection, lines
perpendicular to the viewing surface are projected at ½ their actual length. Both the projections
are shown in the following figure:
Isometric Projections
Orthographic projections that show more than one side of an object are called axonometric
orthographic projections. The most common axonometric projection is an isometric
projection where the projection plane intersects each coordinate axis in the model coordinate
system at an equal distance. In this projection parallelism of lines are preserved but angles are
not preserved. The following figure shows isometric projection:
40
Computer Graphics
Perspective Projection
In perspective projection, the distance from the center of projection to project plane is finite
and the size of the object varies inversely with distance which looks more realistic.
The distance and angles are not preserved and parallel lines do not remain parallel. Instead,
they all converge at a single point called center of projection or projection reference point.
There are 3 types of perspective projections which are shown in the following chart.
One Point
Two Point
Three Point
41
Computer Graphics
The following figure shows all the three types of perspective projection:
Translation
In 3D translation, we transfer the Z coordinate along with the X and Y coordinates. The process
for translation in 3D is similar to 2D translation. A translation moves an object into a different
position on the screen.
Figure: 3D Translation
A point can be translated in 3D by adding translation coordinate ( tx, ty, tz) to the original
coordinate (X, Y, Z) to get the new coordinate (X’, Y’, Z’).
42
Computer Graphics
1 0 0 0
0 1 0 0
𝑇 = [0 0 1 0]
t𝑥 t𝑦 t𝑧 1
P’ = P∙T
1 0 0 0
0 1 0 0
[𝑋 ′ 𝑌′ 𝑍′ 1] = [ 𝑋 1] [ 0
𝑌 𝑍 0 1 0]
t𝑥 t𝑦 t𝑧 1
= [𝑋 + t 𝑥 𝑌 + t𝑦 𝑍 + t𝑧 1]
Rotation
3D rotation is not same as 2D rotation. In 3D rotation, we have to specify the angle of rotation
along with the axis of rotation. We can perform 3D rotation about X, Y, and Z axes. They are
represented in the matrix form as below:
43
Computer Graphics
Figure: 3D Rotation
Scaling
You can change the size of an object using scaling transformation. In the scaling process, you
either expand or compress the dimensions of the object. Scaling can be achieved by multiplying
the original coordinates of the object with the scaling factor to get the desired result. The
following figure shows the effect of 3D scaling:
Figure: 3D Scaling
In 3D scaling operation, three coordinates are used. Let us assume that the original coordinates
are (X, Y, Z), scaling factors are (SX, SY, Sz) respectively, and the produced coordinates are
(X’, Y’, Z’). This can be mathematically represented as shown below:
𝑆𝑥 0 0 0
0 𝑆𝑦 0 0
𝑆= [ ]
0 0 1 0
0 0 0 1
P’ = P∙S
44
Computer Graphics
𝑆𝑥 0 0 0
0 𝑆𝑦 0 0
[𝑋 ′ 𝑌′ 𝑍′ 1] = [ 𝑋 𝑌 𝑍 1] [ ]
0 0 1 0
0 0 0 1
= [𝑋 ∙ 𝑆𝑥 𝑌 ∙ 𝑆𝑦 𝑍 ∙ 𝑆𝑧 1]
Shear
A transformation that slants the shape of an object is called the shear transformation. Like
in 2D shear, we can shear an object along the X-axis, Y-axis, or Z-axis in 3D.
As shown in the above figure, there is a coordinate P. You can shear it to get a new coordinate
P’, which can be represented in 3D matrix form as below:
𝑦
1 𝑆ℎ𝑥 𝑆ℎ𝑥𝑧 0
𝑆ℎ𝑦𝑥 1 𝑆ℎ𝑦𝑧 0
𝑆ℎ = 𝑦
𝑆ℎ𝑧𝑥 𝑆ℎ𝑧 1 0
[ 0 0 0 1]
P’ = P ∙ Sh
𝑦
X’ = X + 𝑆ℎ𝑥 Y + 𝑆ℎ𝑥𝑧 Z
Y’ = 𝑆ℎ𝑦𝑥 X + Y + 𝑆ℎ𝑦𝑧 Z
𝑦
Z’ = 𝑆ℎ𝑧𝑥 X + 𝑆ℎ𝑧 Y + Z
Transformation Matrices
Transformation matrix is a basic tool for transformation. A matrix with n x m dimensions is
multiplied with the coordinate of objects. Usually 3 x 3 or 4 x 4 matrices are used for
transformation. For example, consider the following matrix for various operation.
1 0 0 0 𝑆𝑥 0 0 0 𝑦
1 𝑆ℎ𝑥 𝑆ℎ𝑥𝑧 0
0 1 0 0 0 𝑆𝑦 0 0 𝑆ℎ𝑦𝑥 1 𝑆ℎ𝑦𝑧 0
𝑇 = [0 𝑆= [ ]
0 1 0] 0 0 1 0 𝑆ℎ = 𝑦
𝑆ℎ𝑧𝑥 𝑆ℎ𝑧 1 0
t𝑥 t𝑦 t𝑧 1 0 0 0 1 [ 0 0 0 1]
45
Computer Graphics
Rotation Matrix
46
Computer Graphics
8. Curves
In computer graphics, we often need to draw different types of objects onto the screen. Objects
are not flat all the time and we need to draw curves many times to draw an object.
Types of Curves
A curve is an infinitely large set of points. Each point has two neighbors except endpoints.
Curves can be broadly classified into three categories: explicit, implicit, and parametric
curves.
Implicit Curves
Implicit curve representations define the set of points on a curve by employing a procedure
that can test to see if a point in on the curve. Usually, an implicit curve is defined by an implicit
function of the form:
f(x, y) = 0
It can represent multivalued curves (multiple y values for an x value). A common example is
the circle, whose implicit representation is
x2 + y2 − R2 = 0
Explicit Curves
A mathematical function y = f(x) can be plotted as a curve. Such a function is the explicit
representation of the curve. The explicit representation is not general, since it cannot represent
vertical lines and is also single-valued. For each value of x, only a single value of y is normally
computed by the function.
Parametric Curves
Curves having parametric form are called parametric curves. The explicit and implicit curve
representations can be used only when the function is known. In practice the parametric curves
are used. A two-dimensional parametric curve has the following form:
The functions f and g become the (x, y) coordinates of any point on the curve, and the points
are obtained when the parameter t is varied over a certain interval [a, b], normally [0, 1].
Bezier Curves
Bezier curve is discovered by the French engineer Pierre Bézier. These curves can be
generated under the control of other points. Approximate tangents by using control points are
used to generate curve. The Bezier curve can be represented mathematically as:
47
Computer Graphics
∑ 𝑃𝑖 𝐵𝑖𝑛 (𝑡)
𝑘=0
Where 𝑃𝑖 is the set of points and 𝐵𝑖𝑛 (𝑡) represents the Bernstein polynomials which are given
by:
𝑛
𝐵𝑖𝑛 (𝑡) = ( ) (1 − 𝑡)𝑛−𝑖 𝑡 𝑖
𝑖
Where n is the polynomial degree, i is the index, and t is the variable.
The simplest Bézier curve is the straight line from the point P0 to P1. A quadratic Bezier curve
is determined by three control points. A cubic Bezier curve is determined by four control points.
They generally follow the shape of the control polygon, which consists of the segments
joining the control points.
They always pass through the first and last control points.
They are contained in the convex hull of their defining control points.
The degree of the polynomial defining the curve segment is one less that the number of
defining polygon point. Therefore, for 4 control points, the degree of the polynomial is
3, i.e. cubic polynomial.
The direction of the tangent vector at the end points is same as that of the vector
determined by first and last segments.
The convex hull property for a Bezier curve ensures that the polynomial smoothly follows
the control points.
No straight line intersects a Bezier curve more times than it intersects its control
polygon.
Bezier curves exhibit global control means moving a control point alters the shape of the
whole curve.
A given Bezier curve can be subdivided at a point t=t0 into two Bezier segments which
join together at the point corresponding to the parameter value t=t0.
B-Spline Curves
The Bezier-curve produced by the Bernstein basis function has limited flexibility.
First, the number of specified polygon vertices fixes the order of the resulting polynomial
which defines the curve.
The second limiting characteristic is that the value of the blending function is nonzero
for all parameter values over the entire curve.
The B-spline basis contains the Bernstein basis as the special case. The B-spline basis is non-
global.
A B-spline curve is defined as a linear combination of control points Pi and B-spline basis
function Ni, k (t) given by
Where,
k is the order of the polynomial segments of the B-spline curve. Order k means that the
curve is made up of piecewise polynomial segments of degree k − 1,
the Ni,k(t) are the “normalized B-spline blending functions”. They are described by the
order k and by a non-decreasing sequence of real numbers normally called the “knot
sequence”.
{ti : i = 0, ..., n + k}
The Ni, k functions are described as follows:
and if k > 1,
𝑡 − 𝑡𝑖 𝑡𝑖+𝑘 − 𝑡
𝑁𝑖,𝑘 (𝑡) = 𝑁𝑖,𝑘−1 (𝑡) + 𝑁 (𝑡)
𝑡𝑖+𝑘−1 − 𝑡𝑖 𝑡𝑖+𝑘 − 𝑡𝑖+1 𝑖+1,𝑘−1
and
t [ tk-1,tn+1 )
The sum of the B-spline basis functions for any parameter value is 1.
49
Computer Graphics
Each basis function has precisely one maximum value, except for k=1.
The maximum order of the curve is equal to the number of vertices of defining polygon.
B-spline allows the local control over the curve surface because each vertex affects the
shape of a curve only over a range of parameter values where its associated basis
function is nonzero.
Any affine transformation can be applied to the curve by applying it to the vertices of
defining polygon.
The curve line within the convex hull of its defining polygon.
50
Computer Graphics
11. Fractals
Others: For depicting clouds, coastline and borderlines, data compression, diffusion,
economy, fractal art, fractal music, landscapes, special effect, etc.
Figure: Fractals
Generation of Fractals
Fractals can be generated by repeating the same shape over and over again as shown in the
following figure. In figure (a) shows an equilateral triangle. In figure (b), we can see that the
triangle is repeated to create a star-like shape. In figure (c), we can see that the star shape in
figure (b) is repeated again and again to create a new shape.
63
Computer Graphics
Geometric Fractals
Geometric fractals deal with shapes found in nature that have non-integer or fractal dimensions.
To geometrically construct a deterministic (nonrandom) self-similar fractal, we start with a
given geometric shape, called the initiator. Subparts of the initiator are then replaced with a
pattern, called the generator.
Initiator Generator
As an example, if we use the initiator and generator shown in the above figure, we can construct
good pattern by repeating it. Each straight-line segment in the initiator is replaced with four
equal-length line segments at each step. The scaling factor is 1/3, so the fractal dimension is
D = ln 4/ln 3 ≈ 1.2619.
Also, the length of each line segment in the initiator increases by a factor of 4/3 at each step,
so that the length of the fractal curve tends to infinity as more detail is added to the curve as
shown in the following figure:
64
Computer Graphics
65
Computer Graphics
12. Computer Animation
Animation means giving life to any object in computer graphics. It has the power of injecting
energy and emotions into the most seemingly inanimate objects. Computer-assisted animation
and computer-generated animation are two categories of computer animation. It can be
presented via film or video.
The basic idea behind animation is to play back the recorded images at the rates fast enough
to fool the human eye into interpreting them as continuous motion. Animation can make a
series of dead images come alive. Animation can be used in many areas like entertainment,
computer aided-design, scientific visualization, training, education, e-commerce, and computer
art.
Animation Techniques
Animators have invented and used a variety of different animation techniques. Basically there
are six animation technique which we would discuss one by one in this section.
Keyframing
In this technique, a storyboard is laid out and then the artists draw the major frames of the
animation. Major frames are the ones in which prominent changes take place. They are the key
points of animation. Keyframing requires that the animator specifies critical or key positions for
the objects. The computer then automatically fills in the missing frames by smoothly
interpolating between those positions.
Procedural
In a procedural animation, the objects are animated by a procedure - a set of rules - not by
keyframing. The animator specifies rules and initial conditions and runs simulation. Rules are
often based on physical rules of the real world expressed by mathematical equations.
Behavioral
In behavioral animation, an autonomous character determines its own actions, at least to a
certain extent. This gives the character some ability to improvise, and frees the animator from
the need to specify each detail of every character's motion.
66
Computer Graphics
This technology has enabled a number of famous athletes to supply the actions for characters
in sports video games. Motion capture is pretty popular with the animators mainly because
some of the commonplace human actions can be captured with relative ease. However, there
can be serious discrepancies between the shapes or dimensions of the subject and the graphical
character and this may lead to problems of exact execution.
In contrast the applications based on key-framing and motion select and modify motions form
a pre-computed library of motions. One drawback that simulation suffers from is the expertise
and time required to handcraft the appropriate controls systems.
Key Framing
A keyframe is a frame where we define changes in animation. Every frame is a keyframe when
we create frame by frame animation. When someone creates a 3D animation on a computer,
they usually don’t specify the exact position of any given object on every single frame. They
create keyframes.
Keyframes are important frames during which an object changes its size, direction, shape or
other properties. The computer then figures out all the in-between frames and saves an extreme
amount of time for the animator. The following illustrations depict the frames drawn by user
and the frames generated by computer.
67
Computer Graphics
Morphing
The transformation of object shapes from one form to another form is called morphing. It is
one of the most complicated transformations.
A morph looks as if two images melt into each other with a very fluid motion. In technical terms,
two images are distorted and a fade occurs between them.
68