0% found this document useful (0 votes)
124 views58 pages

رسوم نظري كامله

This document provides an introduction to computer graphics. It discusses that computer graphics is concerned with producing pictures or images using a computer through both hardware and software tools. It then discusses seven main applications of computer graphics: 1) paint programs, 2) illustration/design programs, 3) animation software, 4) CAD software, 5) desktop publishing, 6) virtual reality environments, and 7) entertainment. Finally, it outlines the basic elements used to create computer graphics pictures, including polylines, text, filled regions, and raster images.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views58 pages

رسوم نظري كامله

This document provides an introduction to computer graphics. It discusses that computer graphics is concerned with producing pictures or images using a computer through both hardware and software tools. It then discusses seven main applications of computer graphics: 1) paint programs, 2) illustration/design programs, 3) animation software, 4) CAD software, 5) desktop publishing, 6) virtual reality environments, and 7) entertainment. Finally, it outlines the basic elements used to create computer graphics pictures, including polylines, text, filled regions, and raster images.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Computer Graphics Mustansiriyah university

Third stage 2018-2019 Education college


Computer science department

CHAPTER 1
Introduction to Computer Graphics
1.1 What is computer graphics?

it would be difficult to overstate the importance of computer and


communication technologies in our lives. Activities as wide-ranging
as film making, publishing, banking, and education have undergone
revolutionary changes as these technologies alter the ways in which
we conduct our daily activities. The combination of computers,
networks, and the complex human visual system, through computer
graphics, has been instrumental in these advances and has led to new
ways of displaying information, seeing virtual worlds, and
communicating with both other people and machines.

Computer graphics is concerned with all aspects of producing


pictures or images using a computer.

“Computer graphics” also refers to the tools used to make such


pictures.
There are both hardware and software tools. Hardware tools
include video monitors and printers that display graphics, as well as
input devices like a mouse or trackball that let a user point to items
and draw figures. The computer itself, of course, is a hardware tool,
along with its special circuitry to facilitate graphical display or image
capture.
As for software tools, you are already familiar with the usual ones:
the operating system, editor, compiler, and debugger, that are found
in any programming environment. For graphics there must also be a
collection of “graphics routines” that produce the pictures
themselves.
Computer graphics is a very appealing field of study. You learn to
write programs that create pictures, rather than streams of text or
numbers. Humans respond readily to pictorial information, and are

1
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

able to absorb much more information from pictures than from a


collection of numbers.
People study computer graphics for many reasons. Some just want a
better set of tools for plotting curves and presenting the data they
encounter in their other studies or work. Some want to write
computer animated games, while others are looking for a new
medium for artistic expression. Everyone wants to be more
productive, and to communicate ideas better, and computer graphics
can be a great help.

1.2 Application of Computer Graphics

1. Paint programs: Allow you to create rough freehand


drawings. The images are stored as bit maps and can easily be
edited. It is a graphics program that enables you to draw
pictures on the display screen which is represented as bit maps
(bit-mapped graphics).

2. Illustration/design programs: Illustration/design programs


are often called draw programs. Presentation graphics
software: Lets you create bar charts, pie charts, graphics, and
other types of images for slide shows and reports. The charts
can be based on data imported from spreadsheet applications.

3. Animation software: Enables you to chain and sequence a


series of images to simulate movement. Each image is like a
frame in a movie. It can be defined as a simulation of
movement created by displaying a series of pictures, or frames.
A cartoon on television is one example of animation.

4. CAD software: Enables architects and engineers to draft


designs. It is the acronym for computer-aided design. A CAD
system is a combination of hardware and software that enables
engineers and architects to design everything from furniture to
airplanes. In addition to the software, CAD systems require a

2
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

high-quality graphics monitor; a mouse, light pen, or digitizing


tablet for drawing; and a special printer or plotter for printing
design specifications.
5. Desktop publishing: Provides a full set of word-processing
features as well as fine control over placement of text and
graphics, so that you can create newsletters, advertisements,
books, and other types of documents. It means by using
a personal computer or workstation high-quality printed
documents can be produced. A desktop publishing system
allows you to use different typefaces, specify various margins
and justifications, and embed illustrations and graphs directly
into the text. The most powerful desktop publishing systems
enable you to create illustrations; while less powerful systems
let you insert illustrations created by other programs.

6. Virtual –Reality Environments: a more recent application of


computer graphics is in the creation of Virtual –Reality
Environments in which a user can interact with the objects in a
three-dimensional scene.

7. Entertainment: Television production, motion picture ,and


music videos routinely use computer graphics method.

1.3 Elements of Pictures Created in Computer Graphics


What makes up a computer drawn picture? The basic objects out of
which such pictures are composed are called output primitives. One
useful categorization of these is:
• polylines
• text
• filled regions
• raster images
1. Polylines
A polyline is a connected sequence of straight lines.
When there are several lines in a polyline each one is called an
edge, and two adjacent lines meet at a vertex.

3
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

Figure 1.1 Example of Edge and Vertex.

A polyline need not form a closed figure, but if the first and last
points are connected by an edge the polyline is a polygon.

Figure 1.2 Examples of polygons.

For instance, the polyline shown in Figure 1 is given by the sequence


(2, 4), (2, 11), (6, 14), (12, 11),(12, 4), .... (what are the remaining
vertices in this polyline?).

Figure 1.3 An example polyline.

Attributes of Lines and Polylines

The attributes of a graphic primitive are the characteristics that affect


how it appears, and some Important attributes of a polyline are the
color and thickness of its edges, the manner in which the edges are
dashed, and the manner in which thick edges blend together at their
endpoints. Typically all of the edges of a polyline are given the same
attributes.

4
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

2. Text
Some graphics devices have two distinct display modes, a text
mode and a graphics mode. The text mode is used for simple
input/output of characters to control the operating system or
edit the code in a program.
Text displayed in this mode uses a built-in character generator.
The character generator is capable of drawing alphabetic,
numeric, and punctuation characters, and some selection of
special symbols such as ♥, ð, and ⊕ .
Compare with the graphic mode, the PC's text mode is easy to use.
Displaying information on the screen is a simple as placing ASCII char
in specific memory location. The text screen is divided into 80
column and 25 rows. The graphic mode requires a completely
different orientation instead of character; you have pixels, the
smallest picture element on your computer display. Today most
screens can display text and graphics.

Attributes of Text
There are many text attributes, the most important of which are
typeface, color, size, spacing, and Orientation.
A font is a specific set of character shapes (a typeface) in a particular
style and size.

3. Filled Regions
The filled region (sometimes called “fill area”) primitive is a
shape filled with some color or pattern. The boundary of a
filled region is often a polygon

Figure 1.4 Examples of filled Polygons.

5
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

4. Raster Image.
A raster image It is made up of many small “cells”, in different
shades of gray, The individual cells are often called “pixels”
(short for “picture elements”). Normally your eye can’t see the
individual cells; it blends them together and synthesizes an
overall picture.

A raster image is stored in a computer as an array of numerical


values. This array is thought of as being rectangular, with a certain
number of rows and a certain number of columns. Each numerical
value represents the value of the pixel stored there. The array as a
whole is often called a “pixel map”. The term “bitmap” is also used.
A simple figure represented as a bitmap.

Figure 1.5 shows a simple example where a figure is represented by a


17 by 19 array (17 rows by 19columns) of cells in three shades of
gray. Suppose the three gray levels are encoded as the values 1, 2,
and 7. Figure 1.20b shows the numerical values of the pixel map for
the upper left 6 by 8 portion of the image.

Figure 1.5 A simple figure represented as a bitmap.

6
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

How are raster images created?


The three principal sources are:
1. Hand designed images.
A designer figures out what values are needed for each cell, and types
them into memory. Sometimes a paint program can be used to help
automate this: the designer can draw and manipulate various
graphical shapes, viewing what has been made so far. When satisfied,
the designer stores the result in a file.

2. Computed Images.
An algorithm is used to “render” a scene, which might be modeled
abstractly in computer memory. As a simple example, a scene might
consist of a single yellow smooth sphere illuminated by a light source
that emanates orange light.

3. Scanned images.
A photograph or television image can be digitized as described above.
In effect a grid is placed over the original image, and at each grid
point the digitizer reads into memory the “closest” color in its
repertoire. The bitmap is then stored in a file for later use.

7
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

1.4 Raster Graphics


An image that is presented on the computer screen is made up of
pixels. The screen consists of a rectangular grid of pixels, arranged in
rows and columns. The pixels are small enough that they are not easy
to see individually. In fact, for many very high-resolution displays,
they become essentially invisible. At a given time, each pixel can
show only one color. Most screens these days use 24-bit color, where
a color can be specified by three 8-bit numbers, giving the levels of
red, green, and blue in the color. Any color that can be shown on the
screen is made up of some combination of these three “primary”
colors. As we can see from Figure 1.6

Figure 1. 6 graphical system

In any case, the color values for all the pixels on the screen are stored
in a large block of memory known as a frame buffer. Changing the
image on the screen requires changing color values that are stored in
the frame buffer. The screen is redrawn many times per second, so
that almost immediately after the color values are changed in the
frame buffer, the colors of the pixels on the screen will be changed to
match, and the displayed image will change.
A computer screen used in this way is the basic model of raster
graphics. The term “raster” technically refers to the mechanism used
on older vacuum tube computer monitors: An electron beam would
move along the rows of pixels, making them glow. The beam was

8
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

moved across the screen by powerful magnets that would deflect the
path of the electrons. The stronger the beam, the brighter the glow of
the pixel, so the brightness of the pixels could be controlled by
modulating the intensity of the electron beam. The color values
stored in the frame buffer were used to determine the intensity of the
electron beam. (For a color screen, each pixel had a red dot, a green
dot, and a blue dot, which were separately illuminated by the
beam.)
A modern flat-screen computer monitor is not a raster in the same
sense. There is no moving electron beam. The mechanism that
controls the colors of the pixels is different for different types of
screen. But the screen is still made up of pixels, and the color values
for all the pixels are still stored in a frame buffer. The idea of an
image consisting of a grid of pixels, with numerical color values for
each pixel, defines raster graphics.

1.5 Display Screens

Display screens are output devices that show programming


instructions and data as they are being and information after it is
processed, display screens are either CRT (Cathode-Ray-Tube) or
flat-panel display.

1. CRT Displays: use vacuum tube like that in a TV set.


2. Flat-panel displays: are thinner, weightless, and consume less
power than CRT displays but are not as clear. Principal flat-
panel displays are liquid-crystal displays (LCD) and gas-plasma
display.
The size of screen is measured diagonally from corner to corner in
inches.
1. The Cathode Ray Tube (CRT) Display:
The CRT display screens consist of three components:
a. Cathode Ray Tube (CRT).
b. Frame buffer.
c. Display controller.

9
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

a. Cathode Ray Tube (CRT):


Consists of electron gun that contains a cathode that when heated
emits a beam of negatively charged electrons towards a positively
charged phosphor coated screen. The electron beam passes through
the focusing and deflection system, which consist of an electrostatic
or magnetic field. A color CRT has three electron guns, one for each of
three primary colors: red, green, and blue.
The focusing system concentrates the beam so that by the time the
electrons reach the screen, they have converted to small dot. The
deflection system, which consists of two pairs of deflection plates
(horizontal and vertical) directs the electron beam to any point on
the screen.
When the electron beam strikes the screen, the phosphor emits a
spot of visible light that intensity depends on the number of electrons
on the beam. The duration of this light, called persistence, depends
on the type of phosphor that coats the screen. In order to give the
viewer the appearance of continuous flicker-free image, each dot on
the screen must be intensified many times per second. This type of
CRT is called a refresh CRT. Two types of refresh CRTs are available:
raster-scan and random vector.

Figure 1.7 The Cathode –Ray Tube(CRT)

b. 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 a bit map. The number of rows in the frame buffer array
equals the number of raster lines on the display screen. The number

10
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

of columns in this array equals the number of pixels on each raster


line.
The term pixel (picture element) is also used to describe the row and
column location in the frame buffer arrays that corresponds to the
screen location. A 512  512 display screen requires 262,144 pixel
memory location. 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. 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 to the
row position.
Each pixel in the frame buffer array is composed of a number of bits.
A black and white image that has only two intensity levels, on-off, has
single bit plane frame buffer. In order to display a color or a black
and white quality image with shades of gray, additional bit planes are
needed.

c. Display Controller:
The hardware device that read the contents of the frame buffer into
video buffer, which then converts the digital representation of a
string of pixel values into analogue voltage signals that are sent
serially to the video display screen (CRT).

2. The Flat Panel Display:


Compared to CRT displays, flat panel displays are much thinner,
weightless, and consuming less power. Thus they are better for
portable computers. Flat panel displays are made up of two plates of
glass with a substance between them, which is activating in different
ways. Flat panel displays are distinguished in two ways:

1- By the substance between the plates of glass.


2- By the arrangement of the transistors in the screens.

11
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

Two common types of technology used in flat panel display screens


are:
a. Liquid Crystal display (LCD):
It consists of a substance called liquid crystal, the molecules of which
line up in a way that alter their optical properties. As a result, light
usually backlighting behind the screen is blocked or allowed through
to create an image.

b. Gas Plasma Display:


It is like a neon bulb, in which the display uses a gas that emits light
in the presence of an electric current. That is, the technology uses
neon gas and electrodes above and below the gas. When electric
current passes between the electrodes, the gas glows. Although gas
plasma technology has better resolution than LCD technology, it is
more expensive and thus is not used as often as a LCD. On the other
hand, LCDs are not practical for screens larger than 20 inches and so
are not practical for TV size screen.

Figure 1.8 Generic flat panel display

12
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

1.6 Screen Clarity:


The screen clarity depends on three qualities:
1. Resolution:
Resolution is the numbers of pixels in digital image with N*M(N
horizontal pixels  M vertical pixels),the number of pixels give the
capacity to display the details in original image. Resolution is
expressed in terms of the formula Each pixel can be assigned a color
or particular shade of gray. A screen with 640  480 pixels multiplied
together equals 307200 pixels. This screen will be less clear and
sharp than a screen with 800  600 (equals 480000) 0r 1024  768
(equals 786432) pixels.

Resolution depend on :
1. Special resolution (number of pixels).
2. Brightness resolution (values of pixels).

2. Dot Pitch:
It is the amount of space between the center of adjacent pixels, the
closer the dots, the crisper the image. For crisp images, dot pitch
should be less than 0.31 millimeter.

3. Refresh Rate:
It is the number of times per second that the pixels are recharged so
that their glow remains bright. In general, displays are refreshed 45
to 100 times per second.

13
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
lecu.Amaal Khadum Computer science department

Chapter two

Two-Dimensional Graphics

Introduction
In order to draw a picture on a raster display, we must determine the
corresponding points in the frame buffer that make up the picture. To
perform this task we must write scan conversion point plotting
algorithms.
Both the frame buffer and the display screen are given a two-
dimensional coordinates system with the origin at the lower left
corner.

2.1 Pixels, Coordinates, and Colors


To create a two-dimensional image, each point in the image is
assigned a color. A point in 2D can be identified by a pair of
numerical coordinates. Colors can also be specified numerically.
However, the assignment of numbers to points or colors is
somewhat arbitrary.
So we need to spend some time studying coordinate systems, which
associate numbers to points, and color models, which associate
numbers to colors.

2.1.1 Pixel Coordinates


A digital image is made up of rows and columns of pixels. A pixel in
such an image can be specified by saying which column and which
row contains it. In terms of coordinates, a pixel can be identified by a
pair of integers giving the column number and the row number. For
example, the pixel with coordinates (3,5) would lie in column
number 3 and row number 5.
Conventionally, columns are numbered from left to right, starting
with zero. Most graphics systems, including the ones we will study in
this chapter, number rows from top to bottom, starting from zero.

1
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
lecu.Amaal Khadum Computer science department

The drawing on the screen starts from top to down, and from left
to right. The pixel coordinates on the screen (VGA 640  480) is
shown in figure below:

(0,0)

(639 , 479)

2
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
lecu.Amaal Khadum Computer science department

Scan conversion
The process of representing continuous graphics objects as a
collection of discrete pixel called Scan conversion.
Figure 2.1 suggests how an image is created and displayed.

Figure 2.1 Block diagram of a computer with raster display.

Line Drawing Algorithms

Many computer pictures are composed of straight line segments. A


line segment is displayed by turning on a string of adjacent pixels. In
order to draw a line, it is necessary to determine which pixels lie
nearest the line and provide the best approximation to the desired
line.

3
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
lecu.Amaal Khadum Computer science department

Above figure illustrates a line drawn on a raster display. Observe that


the accuracy and quality of the displayed line depends on the
resolution of the display device. High resolution displays (1024 ×
1024) draw lines that look straight and continuous, and that start
and end accurately. Lower resolution displays may draw lines with
gaps. Line drawing routines in high performance graphics systems
are implemented in hardware that rapidly generates the pixels
comprising the line when it is given the two end points. Most low cost
display systems still relay on slower software routines to accomplish
line drawing. In either case, the line drawing routine should be
accurate, fast, and easy to implement.

Horizontal and Vertical Lines:

The simplest lines to draw are horizontal and vertical lines.

1. Horizontal Lines:

The screen coordinates of the points on a horizontal line are obtained


by keeping the value of (y) constant and repeatedly incrementing the
(x) value by one unit as in algorithm (1).

Algorithm (1):
Input: Xstart, Xend, Yspecified.
Output: Horizontal line.

{ for x= Xstart to Xend


plot (x , yspecified , color);
}

If Xstart > Xend then replace Xend by Xstart and vice versa in for loop
at algorithm(1).

H.W.: Write complete program in order to draw horizontal line?

4
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
lecu.Amaal Khadum Computer science department

2. Vertical Lines:

The screen coordinates of the points on a vertical line are obtained


by keeping the value of (x) constant and repeatedly incrementing the
(y) value by one unit as in algorithm (2).

Algorithm (2):

Input: Ystart,Yend, Xspecified.


Output: Vertical line.
{
for y= Ystart to Yend
plot (xspecified , y , color);

If Ystart > Yend then replace Yend by Ystart and vice versa in for loop
at algorithm (2).

H.W.: Write complete program in order to draw vertical line?

5
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
lecu.Amaal Khadum Computer science department

3. Diagonal Lines:
To draw a diagonal line with a slope equal to (+1), we need only
repeatedly increment by one unit both x and y values from the start
to end pixels as shown in algorithm (3).

Algorithm (3):

Input: Xstart,Ystart, Xend, Yend.


i=0
Output: Diagonal line.
{
while (xstart + i) ≤ Xend )
{
plot (xstart + i, ystart + i, color);
i= i+1;
}
}

To draw a line with slop equals to (-1), replace (ystart+i) by (ystart-i)


in algorithm (3).

H.W.: Write complete program in order to draw diagonal line


using algorithm (3) with slope (+1) and slope (-1)?

6
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
lecu.Amaal Khadum Computer science department



What are the things a good line-drawing algorithms should do
well:
1. straight Lines must be appear as straight lines.
2. Lines must start and end accurately.
3. Lines should have constant brightness along their length.
4.Lines should be drawn rapidly.

Note

1. Vertical ,horizontal lines ,no jaggies.


2. Sloped lines end up having jaggies.
3. Rounded pixel value is off actual line path (jaggy).
4. Rounding may be required computed point
(10.48,20.51) rounded to (10,21).

Jaggy lines

7
Mustansiriyah university
Computer Graphics Education college
2018-2019
Third stage
Computer science department

Arbitrary Lines

Drawing lines with arbitrary slope creates several problems, such as:

1- The display screen can be illuminated only at pixels locations;


therefore a raster scan display has a staircase effect that only
approximates the actual line as shown in figure below:

Although it may not be possible to choose pixels that lie on the actual
line, we want to turn on pixels lying closest to it. For example, in
previous figure, the pixel at location B is a better choice than the one
at location A.

2- Determining the closest (best) pixels is not easy.

Different algorithms calculate different pixels to make up the


approximating line.

The choice of algorithm depends on:


1- The speed of line generation.
2- The appearance of the line.
Therefore, to understand these criteria better let's look at several
different line generating algorithms.

1
Mustansiriyah university
Computer Graphics Education college
2018-2019
Third stage
Computer science department

1- Direct method:
In this method, we learn how to draw a line between two points by
drawing a group of pixels using the command plot (x , y , color), with
substituting in straight line equation:

Y=m×X+b

Where (m) is the slope and (b) is a constant which represents


the clipping from y-axis (y-intercept).

𝒚𝒆𝒏𝒅−𝒚𝒔𝒕𝒂𝒓𝒕
𝒎= , b=ystart – m * xstart
𝒙𝒆𝒏𝒅−𝒙𝒔𝒕𝒂𝒓𝒕

Note: start may be 1, and end may be 2.

Direct method for drawing lines can be shown in algorithm (4).

Algorithm (4):
Input: Xstart,Ystart, Xend, Yend.
Output: Arbitrary line.
{
dx=Xend-Xstart
dy=Yend-Ystart
m=dy/dx

b= Ystart – m * Xstart ;
for x= Xstart to Xend step sign(dx)
{
y=m *x + b;
plot (x , y , color);
}
}

Direct method is clarified in algorithm (4), assuming that:


1- Sign is a function returns (-1,0,+1) as it's argument is (<0,=0,>0).

H.W.: Write complete program in order to draw arbitrary line


using direct method?

2
Mustansiriyah university
Computer Graphics Education college
2018-2019
Third stage
Computer science department

Example: trace the line where the endpoints are (1, 5), (7, 2) by Direct
method , and draw the line in screen coordinate.
dx=7-1=6; dy=2-5= -3; m= -3/6= -1/2 =-0.5; b=5- (-0.5)*1=5.5

X Y Point (x, y) Plot in screen


1 1*-0.5+5.5 (1,5) (1,5)
2 2*-0.5+5.5 (2,4.5) (2,5)
3 3*-0.5+5.5 (3,4) (3,4)
4 4*-0.5+5.5 (4,3.5) (4,4)
5 5*-0.5+5.5 (5,3) (5,3)
6 6*-0.5+5.5 (6,2.5) (6,3)
7 7*-0.5+5.5 (7,2) (7,2)

H.W/ 1. Tracing same example but endpoints (7, 2) (1, 5).

3
Mustansiriyah university
Computer Graphics Education college
2018-2019
Third stage
Computer science department

2. Simple DDA (Digital Differential Analysis)


One technique for obtaining a straight line is to solve the
differential equation for straight line.

DDA Algorithm(5):

Consider one point of the line as (X1,Y1) and the second point of the
line as (X2,Y2).

1. calculate dx , dy
dx = ( X2 – X1);
dy = ( Y2 – Y1);

2. Depending upon absolute value of dx & dy


choose number of (length) to put pixel as
length = abs(dx) if abs(dx) > abs(dy)
Else
length = abs(dy) if abs(dy) > abs(dx)

3. calculate increment in x & y for each steps


Xinc = dx / length;
Yinc = dy / length;

4. Put pixel for each step


X = X1;
Y = Y1;
for (int i = 0; i <= length; i++)
{
plot (X,Y,’b’);
X += Xinc;
Y += Yinc;
}

H.W.: Write complete program in order to draw arbitrary line


using DDA algorithm?

4
Mustansiriyah university
Computer Graphics Education college
2018-2019
Third stage
Computer science department

Algorithm (5):
Input: x1,x2,y1,y2.
i=0;
Output: Arbitrary line
{ If (abs(x2-x1)  abs(y2-y1))

length= abs(x2-x1);

else

length= abs(y2-y1);

dx=(x2-x1)/length;
dy=(y2-y1)/length;
x=x1;

y=y1;

while (i  length)

{
plot(round(x),round(y),color);

x=x+ d x ;

y=y+ d y ;

i=i+1;

5
Mustansiriyah university
Computer Graphics Education college
2018-2019
Third stage
Computer science department

A simple algorithm for DDA is clarified in algorithm (5), assuming that:

1- Round is a function approximates float (real) numbers to nearest larger


number.

Ex/: trace the line where the end points between (23,33), (29,40) by DDA
method.
Sol/: dx=29-23=6
dy=40-33=7 i X Y Plot Plot
(X) (y)
Length=7
Xinc=6/7 =0.857 0 23 33 23 33
Yinc=7/7=1.
1 23.857 34 24 34
2 24.714 35 25 35
3 25.571 36 26 36
4 26.429 37 26 37
5 27.286 38 27 38
6 28.143 39 28 39
7 29 40 29 40

NOTE:
The DDA algorithm is faster than the direct use of the line equation
since it calculates points on the line without any floating point
multiplication. However, a floating point addition is still needed in
determining each successive point. Furthermore, cumulative error
due to limited precision in the floating point representation may
cause calculated points to drift away from their true position when
the line relatively long.

6
Mustansiriyah university
Computer Graphics Education college
2018-2019
Third stage
Computer science department

3.Bresenham’s algorithm
- Developed by Bresenham J.E.
- Designed so that each iteration changes one of the coordinates
values by ± 1.
- The other coordinate may or may not change depending on the
value of an error term maintained by the algorithm.
- The error term records the distance measured perpendicular to the
axis of greatest movement between the exact path of the line and the
actual dots generated.

Bresenham's line algorithm is shown in algorithm (6).

Algorithm (6):
Input: x1,x2,y1,y2.
Output: Arbitrary line
{ x=x1 ;y=y1; dx=x2-x1; dy=y2-y1;
e=dy/dx-0.5 ;
for i=0 to abs(dx)
{ plot (x,y,color);
While (e>=0)
{
if y1>y2
{ y=y-1;e=e-1;
else
y=y+1; e=e-1;
}
}
If x1>x2
{ x=x-1;
else
x=x+1;
}
e=e+dy/dx
}
}

7
Mustansiriyah university
Computer Graphics Education college
2018-2019
Third stage
Computer science department

H.W.: Write complete program in order to draw arbitrary line


using Bresenham's algorithm?
Ex. / trace the line where the end points (50, 65), (59, 68) by Bresenham’s
algorithm.
i X Y e
Sol. / dx= 59-50= 9 0 50 65 -0.167 +m
dy= 68-65= 3 1 51 65 0.166 -1+m
m= dy/dx =3/9 = 0.333
2 52 66 -0.501 +m
e=0.333 – 0.5 = -0.167
3 53 66 -0.168 +m
4 54 66 0.165 -1+m

5 55 67 -0.502 +m

6 56 67 -0.169 +m

7 57 67 0.164 -1+m

8 58 68 -0.503 +m
9 59 68 -0.17

8
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

Circle Drawing
The circle is a special kind of curves. The circle is a closed curve
with same starting and ending point. Circles are probably the most
used curves in elementary graphics.

xc,yc

 A circle is specified by the coordinates of its center (xc,yc) and


its radius (r) .
 The circle equation is : (x−xc)2 + (y −yc)2 = r 2 ……. (1)
 If the center of the circle is at the origin (0,0) then the equation
is :
x 2+y2=r 2 …… (2)
Solving equation (1) for y :

y= yc ±√r 2−(x−xc)2

Note: To draw a circle increment the x values by one unit from –r to


+r and use the above equation to solve for the two y values at each
step.

1
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

1. Direct (implicit) algorithm

In this method the first pixel of circle is at left side as equation


x=xc-r
y=yc
to draw the circle we can increment x from –r to +r or from 0 to 2r by
one unit at each step and solving for y

y= yc ±√r 2−(x−xc)2
x=x+1

This method of drawing a circle is inefficient because:


1. We are not taking advantages of the symmetry of the circle.

2. The amount of processing time required to perform the


squaring and square root operations repeatedly.

3. X values are equally spaced (they differ by one unit ) the y


values are not. The circle is denes and flat near the y-axis and
has large gaps and is steep near the x-axis.

2
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

Direct Algorithm

Input : xc ,yc , r.
Output : Circle
{ x=xc-r;
for i= 0 to 2*r
{ y=yc+
plot (x, integer (y) ,color)
y=yc-
plot (x, integer (y),color)
x=x+1;
}
}

H\W: Design implicit algorithm to draw circle if the first point is at


right side.

H\W: design implicit algorithm to draw circle if the first point is


x=xc, y=yc - r

H\W: Find the point of a circle where xc=20, yc = 10 and r=8 ?

3
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

Example :Find the point of a circle where xc=10, yc= 10 and r=5
using direct algorithm?

xc=10
yc=10
x=xc-r x=10-5=5 3
For i=0:2*r
y=yc+sqrt((r^2)-(x-xc)^2)
Plot(x,round(y),'y')
y=yc-sqrt((r^2)-(x-xc)^2)
Plot(x,round(y),'y')
x=x+1
End

X Y Round(y) Y Round(y) Plot(X,Y)


5 10 10 10 10 (5,10),(5,10)
6 13 13 7 7 (6,13),(6,7)
7 14 14 6 6 (7,14),(7,6)
8 14.5 15 5.4 5 (8,15),(8,5)
9 14.8 15 5.1 5 (9,15),(9,5)
10 15 15 5 5 (10,15),(10,5)
11 14.8 15 5.1 5 (11,15),(11,5)
12 14.5 15 5.4 5 (12,15),(12,5)
13 14 14 6 6 (13,14),(13,6)
14 13 13 7 7 (14,13),(14,7)

4
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

2. parametric (polar) algorithm

One method of eliminating the problem of plotting points evenly


spaced around the circle is to use polar representation of a circle:

x= xc+ r cos ,

y = yc + r sin .

Where:   is measured in radians from 0 to 2 

arc length= r   , r=radius (constant)

in this method we depend on angles to draw the circle, since it


propose the first angle th=0, and end angle is two_pi (360).
The change in angle (dth) must be small value dth=1/r.

5
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

Polar algorithm

Input : xc ,yc , r.
Output : Circle
{
th=0; dth=1/r;
while (th<=2*pi)
{
x = xc + r *cos (th)
y = yc + r sin(th)
plot (integer(x),integer(y),color)
th = th + dth;
}
}

Note: the algorithm use cos & sin operation and do not take
the advantage of symmetric in circle

H\W: write Matlab program to draw circle using polar


algorithm?

6
Computer Graphics Mustansiriyah university
Third stage 2018-2019 Education college
Computer science department

Example :Find the point of a circle where xc=10, yc= 10 and r=5
using polar algorithm ?

th=0
dth=1/r=1/5
While th <=2*pi
x=xc+r*cos(th)
y=yc+r*sin(th)
Plot(round(x),round(y),'.k')
th=th+dth
End

x Round(x) y Round(y) th Plot (x,y)


15 15 10 10 0.2 (15,10)
14.9 15 10.9 11 0.4 (15,11)
14.6 15 11.9 12 0.6 (15,12)
14.1 14 12.8 13 0.8 (14,13)
13.4 13 13.5 14 1 (13,14)
12.7 13 14.2 14 1.2 (13,14)
11.8 12 14.6 15 1.4 (12,15)
: : : : : :
: : : : : :
14.9 15 9.5 10 6.4 (15,10)

7
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Chapter Five
5.1 Symmetric (incremental) algorithm

• Computation can be reduced by considering the symmetry of circle.


• The shape of the circle is similar in each quadrant
• We can generate the circle section in the second quadrant by noting
that the two circle sections are symmetric with respect to the y axis And
circle sections in the third and fourth quadrants can be obtained from
sections in the first and second quadrants by considering symmetry
about the x axis.

Circle sections in adjacent octants within one quadrant are symmetric


with respect to the 45' line dividing the two octants.

1
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Consider a circle center at the origin(0,0),if the point (x,y ) is on the


circle then we can trivially compute seven other points on the circle.
• This method proposed the center of circle at origin point(0,0),so the
first pixel in the circle is (r,0).
• The other pixels are computed depend on polar equation as follow:-

x = xc + r cos   
y = yc + r sin 
x =r cos 
y =r sin 
Dx=-rsind
Dy=rcos d
Dx =-y d
Dy = x d
as we know
x = x + Dx x = x – y* dø
y = y + Dy y = y + x* dø
symmetry equation

if we add center (xc,yc) we obtain the values of 8 pixels of the circle as


figure bellow.

2
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Incremental (Symmetric) algorithm

Input : xc,yc,r.
Output : circle
{
th=0 , pi=3.141593 , dth=1/r , x=r , y=0;
while th<=pi/4
{
plot (integer (xc+x) , integer (yc+y) ,color )
plot (integer (xc+x) , integer (yc-y) ,color)
plot (integer (xc-x) , integer (yc+y) ,color )
plot (integer (xc-x) , integer (yc-y) ,color)
plot (integer (xc+y) , integer (yc+x) ,color )
plot (integer (xc+y) , integer (yc-x) ,color )
plot (integer (xc-y) , integer (yc+x) ,color )
plot (integer (xc-y) , integer (yc-x) ,color )
th = th + dth ;
x = x - y * dth ;
y = y + x * dth ;
}
}

3
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

5.2 Bresenham circle algorithm:

The values of a circle centered at the origin are computed in a 45 sector


from x=0 to x=y the remaining seven sectors are obtained from the eight
point symmetry of the circle.
They values for this sector decrease as the x values increase if (0, r) is the
starting point of the algorithm, then as x increase by one unit the y value
either remains the same or is decrease by one unit. If(x, y) is a pixel on
the circle, the next pixel is either A or B.
A :( x+1, y); to the right of previous point
B: (x+1, y-1); down and to the right of the previous point.

The algorithm proceeds to choose Pixel A or B by finding and comparing


the distance from each pixel to the point on the circle that has x value of
(x+1) these distances measure how far from the circle each pixel is the
pixel with the smallest distances the best approximation on the circle.
The square of the distance of pixel A from the center of the circle is:
(x+1) ^2 + y^2.
The difference between this squared distance and the squared distance
to the closest point on the circle is: d(A)= (x+1)^2 +y^2 -r^2
for pixel B the distance is : d(B)= (x+1)^2 + (y-1)^2 – r^2.

< 0 𝑖𝑓 (𝑥, 𝑦) 𝑖𝑠 𝑖𝑛𝑠𝑖𝑑𝑒 𝑡ℎ𝑒 𝑐𝑖𝑟𝑐𝑙𝑒 𝑏𝑜𝑢𝑛𝑑𝑎𝑟𝑦


𝑑 { = 0 𝑖𝑓 (𝑥, 𝑦) 𝑖𝑠 𝑜𝑛 𝑡ℎ𝑒 𝑐𝑖𝑟𝑐𝑙𝑒 𝑏𝑜𝑢𝑛𝑑𝑎𝑟𝑦
> 0 𝑖𝑓 (𝑥, 𝑦)𝑖𝑠 𝑜𝑢𝑡𝑠𝑖𝑑𝑒 𝑡ℎ𝑒 𝑐𝑖𝑟𝑐𝑙𝑒 𝑏𝑜𝑢𝑛𝑑𝑎𝑟𝑦

A point lies on the circle if its d value equals 0, in order to determine


which pixel A or B has the smallest d value we examine the sum

S=d(A)+d(B);

- There are three cases to be consider:


1. If the circle goes between A and B then
d(A) > 0 and d(B) <0
If abs (d(A)) > abs (d(B)) → choose B , such that S> 0
If abs (d(A)) <= abs(d(B)) → choose A , such that S<= 0
2. If the circle goes through or above A then
d(A)<=0 and d(B)<0 → choose A, such that S<0

4
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

3. If the circle goes through B or below then

d(A) > 0 and d(B) >=0 → choose B , such that S>0

Rule: if S>0 we choose pixel B otherwise we choose pixel A.

P(x,y
A(x+1,y)
)

B(x+1,y-
1)
r

5
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Bresenham circle algorithm:

Input : xc,yc,r.
Output : circle

{
x=0; y=r;
While(x<=y)
{
plot (integer (xc+x) , integer (yc+y) ,color)
plot (integer (xc+x) , integer (yc-y) ,color)
plot (integer (xc-x) , integer (yc+y) ,color )
plot (integer (xc-x) , integer (yc-y) ,color)
plot (integer (xc+y) , integer (yc+x) ,color )
plot (integer (xc+y) , integer (yc-x) ,color )
plot (integer (xc-y) , integer (yc+x) ,color )
plot (integer (xc-y) , integer (yc-x) ,color )

da= (x+1)^2+(y)^2-(r)^2
db= (x+1)^2+ (y-1)^2 -(r)^2
s= da + db
if (s>0)
{ y=y-1 ; }

x=x+1;
}
}

6
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Ex. /trace the Bresenham algorithm to generate six points of the circle
centered at (300,150) with a radius equal to 9 unit.
Sol. / da = (x+1) ^2 + y^2 – R^2 , db= (x+1) ^2 + (y-1) ^2 – R^2,
R^2=81

x y x+xc y+yc d(A) d(B) s


0 9 300 159 1 -16 -15
1 9 301 159 4 -13 -9
2 9 302 159 9 -8 1
3 8 303 158 -1 -16 -17
4 8 304 158 8 -7 1
5 7 305 157 4 -9 -5

7
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

5.3 Midpoint circle algorithm:

• Bresenham's line algorithm for raster displays is adapted to circle


generation by setting up decision parameters (P) for finding the closest
pixel to the circumference at each step.
• For a given radius r and screen center position ( xc,yc), we can first set
up our algorithm to calculate pixel positions around a circle path
centered at the coordinate origin (0,0). So first pixel is (0,r)
• each calculated position (x,y) is moved to its proper screen position by
adding xc to x and yc to y.
• we compute the first octant pixels from x=0 to x=y.
• Positions for the other seven octants are then obtained by symmetry
•Similarly to the case with lines, there is an incremental algorithm for
drawing circles –the mid-point circle algorithm
•In the mid-point circle algorithm we use eight-way symmetry so only
ever calculate the points for the top right eighth of a circle, and then use
symmetry to get the rest of the points .
• we can take unit steps in the positive x direction over octant and use a
decision parameter to determine which of the two possible y positions is
closer to the circle path at each step.

• Any point ( x , y) on the boundary of the circle with radius r satisfies


the equation p = 0.
• If the point is in the interior of the circle, P is negative value.
• if the point is outside the circle, P is positive.

8
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

• We need to determine whether the pixel at position (x+ 1, y) or the


one at position (x+1,y-1) is closer to the circle. So If p<0,the point is
inside the circle and the pixel (x+1,y) is closer to the circle boundary.
Otherwise, the point is outside or on the circle boundary and we
select the pixel (x+1,y-1 ).

9
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

midpoint circle algorithm

Input : xc,yc,r.
Output : circle
{
x = 0 ; y = r ; p = 2*(1 – r)
While x <= y
{
plot (integer (xc+x) , integer (yc+y) ,color)
plot (integer (xc+x) , integer (yc-y) ,color)
plot (integer (xc-x) , integer (yc+y) ,color )
plot (integer (xc-x) , integer (yc-y) ,color)
plot (integer (xc+y) , integer (yc+x) ,color )
plot (integer (xc+y) , integer (yc-x) ,color )
plot (integer (xc-y) , integer (yc+x) ,color )
plot (integer (xc-y) , integer (yc-x) ,color )
x = x + 1;
If p < 0 Then
p = p + 2 * x + 1;
Else
y=y-1;
p = p + 2 * (x - y) + 1;
End If
}
}

10
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Ex. /trace the Midpoint algorithm to generate six points of the circle
centered at (120,130) with a radius equal to 7 unit.
Sol. /p=2(1-r)=2(1-7)=-12

If p<0 p=p+2*x+1

If p>=0 p=p+2(x-y)+1

x y P x+xc y+yc
0 7 -12 120 137
1 7 -9 121 137
2 7 -4 122 137
3 7 3 123 137
4 6 0 124 136
5 5 1 125 135

11
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

5.4 Ellipses Drawing


- It is a variation of a circle.
- Stretching a circle in one direction produce an ellipse.
- Following algorithm for ellipse drawing which stretched in
the x or y direction.

The polar equation for this type of ellipse centered at ( 𝑥𝑐 ,


𝑦𝑐) are:
x= 𝑥𝑐+xr*cos(𝜃)
…….. (1)
y=𝑦𝑐 +yr*sin(𝜃)
𝜃 values between 0 and 2π radius

yr
ø
(xc,yc) xr

The values of (xr) and (yr) effect the shape of the ellipse.
- If yr > xr the ellipse is longer in the y direction
- If yr < xr the ellipse is longer in the x direction.
- If xr = yr the equation produces a circle.
- The ellipse can be drawn using four-points symmetry.
If (x,y) lies on the ellipse so do the points (-x,y), (x,-y) and
(-x,-y).

(-x,y) (x,y)
y
r

(-x,-y) (x,-y)

12
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

A)The polar representation of an ellipse:


The polar equation for an ellipse centered at (xc, yc) and xr is the radius
on the x-axis and yr is the radius on the y-axis.
Then algorithm is:

Input : xc,yc,xr,yr.
Output: Ellipse .
{
dth=1/ ((xr +yr)/2) ;
th=0 ;
pi= 3.1416;
While (th<=2*pi)
{
x= xr*cos(th) ;
y= yr*sin(th) ;
plot (integer (xc+x), integer(yc +y),color);
plot (integer (xc-x), integer (yc+y), color);
plot (integer (xc+x), integer (yc-y), color);
plot (integer (xc-x), integer (yc-y), color) ;
th=th+dth ;
}
}

EX /trace the algorithm that use the polar representation to generate six
points of the ellipse centered at (300, 150) with xr=10, yr=5.
Sol:\ dth=1/((xr+yr)/2) ; 2*pi=6.2832
=1/((10+5)/2)
= 0.133

x=xr*cos(th) Plot(x) y=yr*sin(th) Plot(y) x+xc y+yc th


10 10 0 0 310 150 0
9.9 10 0.6 1 310 151 0.1333
9.6 10 1.3 1 310 151 0.2667
9.2 9 1.9 2 309 152 0.4000
8.6 9 2.5 3 309 153 0.5333
7.8 8 3.0 3 308 153 0.6667

13
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

B)Incremental method to drawing of ellipse: similar of discuss in


circle, but differ for equations :
- The following incremental equations for an ellipse are derived from
equations (1)

Xn+1 =Xn cos (Δ Ө) – (Xr/Yr) * Yn sin (Δ Ө)….. (2)


Yn+1 =Yn cos (Δ Ө) + (Yr/Xr) *Xn sin (Δ Ө)….. (3)

This algorithm is modify the polar representation (leave that of students


to write this algorithm)

14
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Arc: is part of circle but is needed start angle & end angle and distance of
center of arc to surrounding is same as.

Sector: is part of ellipse but contains two line; line1 from center to start
angle & other line from center to end angle & distance between center of
sector to surrounding is different as.

r a
r
Arc Sector b
r

Scan-converting ARCs and Sectors


 ARC can be generated by:
1- Polynomial method

Value of x is varied from 𝑥1 to 𝑥2 and the values of y are


found be evaluating the expression √𝑟2+√𝑥2 .
An arc would be appear to be nothing more than portions of
circles.
2- Trigonometric method

The starting value is set equal to 𝜃1 and the ending value is set
equal to 𝜃2. The rest of the steps are similar to those used
when scan-converting a circle except that symmetry is not
used.

Sectors
A sector is scan-converting an arc and then scan-converting
two-lines from the center of the Arc to the endpoints of the
Arc.
Ex./ Assume that a sector whose center is at point (h,k) is to
be scan-converted.
- First scan-convert an Arc from 𝜃1 to 𝜃2 .
- Next a line would be scan-converted from (h,k) to (r
cos(𝜃1)+h, r sin(𝜃1)+k).
- A second line would be scan-converted from (h,k) to (r
cos(𝜃2)+h, r sin(𝜃2)+k).

15
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Scan-converting a rectangle
Rectangle sides are parallel to the coordinates axes may be
constructed if the locations of two vertices are known. The
remaining corner points are then derived. If we have the
following rectangle.

y y (x2,y2)
(x1,y1)

(x1,y1)
(x2,y2)

x x

The lines would be drawn as follows:


plot ([𝑥1 𝑦1] , [𝑥1 𝑦2] ,color)
plot ([𝑥1 𝑦2] , [𝑥2 𝑦2] ,color)
plot ([𝑥2 𝑦2] , [𝑥2 𝑦1] ,color)
Plot ([𝑥2 𝑦1] , [𝑥1 𝑦1] ,color)

16
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Chapter Six
6.1 Two Dimension(2D)- Transformations
One of the most common and important tasks in computer graphics is to
transform the coordinates ( position, orientation, size and shape ) of either
objects within the graphical scene or the camera that is viewing the scene. It is
also frequently necessary to transform coordinates from one coordinate system
to another, (e.g. world coordinates to viewpoint coordinates to screen
coordinates). All of these transformations can be efficiently and sufficiently
handled using some simple matrix representations, which we will see can be
particularly useful for combining multiple transformations into a single
composite transform matrix.

6.2 Fundamental Transformation


Three basic types of transformations that can perform in two dimensions::
A. Translation(shift OR move).
B. Scaling.
C. Rotation

These basic transformations can also be combined to obtain more complex


transformations.

Representation of Points/Objects
A point p in 2D is represented as a pair of numbers: p= (x, y) where x is the x-
coordinate of the point p and y is the y-coordinate of p . 2D objects are often
represented as a set of points (vertices), {𝑝1, 𝑝2,..., 𝑝𝑛} . We can also write points
in vector/matrix as : p=(xy)

6.2.A) Translation: A translation moves an object to a different position on


the screen. You can translate a point in 2D by adding translation coordinate (𝑡𝑥,
𝑡𝑦) to the original coordinate (X, Y) to get the new coordinate (X’, Y’).

1
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Consider a point P(x, y). we can translated it means shift it to new position p`(x`,
y`) by adding tx and ty in y where Tx and Ty are translating factor.

Mathematically this can be represented as:


x`= x + Tx
y`= y + Ty
Note1:Using coordinate system the translating factor are
If Tx>0 then point moves to the right.
If Tx<0 then point moves to the left.
If Ty>0 then point moves to the up.
If Ty<0 then point moves to the down.

Note2: using coordinate system in screen then translating factor are


If TX>0 then point moves to the right.
If TX <0 then point moves to the left.
If Ty >0 then point moves to the down.
If Ty <0 then point moves to the up.

We can represent the translation transformation using column vectors as


follows:
𝑥 𝑡𝑥 𝑥′ 𝑥 𝑡𝑥
P’ = P+T, 𝑃 =[𝑦] , 𝑇 = [𝑡𝑦] , 𝑃′ =[ ′ ] = [𝑦] + [𝑡𝑦]
𝑦
x + t𝑥
P’ = [ y + t𝑦 ]
Example : Consider a triangle defined by its three vertices (20,0), (60,0),
(40,100) been moved 100 units to the right and 10 units up. Find the new
coordinates of this triangle after translation.
So, the new coordinated of the triangle are :

100 20 60 40 20 + 𝑡𝑥 60 + 𝑡𝑥 40 + 𝑡𝑥
T=[ ] , P=[ ] , P’=[ ]
10 0 0 100 0 + 𝑡𝑦 0 + 𝑡𝑦 100 + 𝑡𝑦

120 160 140


So, the new coordinated of the triangle are : 𝑃′ =[ ]
10 10 110
40
[ ] 140
100 [ ]
100 110
110

20 120
[ ] 10 [ ] 160
0 60 10 [ ]
[ ] 10
0
100 120 140 160
0 10 20 30 40 50 60 70 80 90 100

2
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

6.2.B) Scaling : To change the size of an object, scaling transformation is


used. 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 by the scaling factor ( S ) to get the desired result.
Notes:
 If the scaling factor (S < 1 ) ; then we can reduce the size of the object.
 If the scaling factor (S > 1 ) ; then we can increase the size of the object.
 If the scaling factor (S = 1 ) ; then no change.

Let us assume that the original coordinates are (x, y), the scaling factors are (Sx ,
Sy ), and the produced coordinates are (x’, y’). This can be mathematically
represented as shown below :

x' = x . Sx and y' = y . Sy

The scaling factor Sx, Sy scales the object in x and y direction respectively. The
above equations can also be represented in matrix form as below.

𝑋′ 𝑆𝑥 0 𝑥
𝑃′ = 𝑆 . 𝑃 𝑂𝑅 ( ′) = [ 0 ] .
𝑆𝑦 𝑦) (
𝑌

Where S is the scaling matrix.

If 𝑆𝑥 = 𝑆𝑦 → No change in the shape the object (𝑢𝑛𝑖𝑓𝑜𝑟𝑚 𝑠𝑐𝑎𝑙𝑖𝑛𝑔)

If 𝑆𝑥 ≠ 𝑆𝑦 change in the shape the object (distortion 𝑖𝑛 𝑡ℎ𝑒 𝑜𝑟𝑖𝑔𝑖𝑛𝑎𝑙 𝑜𝑏𝑗𝑒𝑐𝑡)

The scaling process is shown in the following figure.

3
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

Whenever the scaling process is performed there is one point still no change (same
position), this point is called fixed point of the scaling transformation. If the fixed
point at the origin, then the point (x,y) can be scaled by a scale factor Sx , Sy in the
x-axis and y-axis direction respectively to the new point (𝑥 ′ , 𝑦 ′ ).

x' = x . Sx ; y' = y . Sy ; (apply in fixed point in origin point)

6.2.B.1)Scaling about Arbitory Point

The scaling is performed with respect to any point (xf, yf) as fixed point according to
the three steps:

1. We must first translate the object so that the fixed point is coincide with the
origin as follows: every object point (x, y) is moved to the new position (𝑥 ′ , 𝑦 ′ )
such as:
𝑥 ′ = 𝑥 − 𝑥𝑓 , 𝑦 ′ = 𝑦 − 𝑦𝑓
2. We then scaled these translated points by scale factors 𝑆𝑥 and 𝑆𝑦 ,so that :
𝑥 ′′ = 𝑥 ′ . 𝑆 𝑥 , 𝑦 ′′ = 𝑦 ′ . 𝑆𝑦
3. Then perform the inverse of the original translation to move the fixed point back
to its original position.

𝑥 ′′′ = 𝑥 ′ + 𝑥 𝑓 , 𝑦 ′′′ = 𝑦 ′′ + 𝑦𝑓

These three steps can be combined in the following equation that scales a point (xf,
yf). 𝑖. 𝑒 𝑡ℎ𝑒 𝑓𝑖𝑛𝑎𝑙 𝑒𝑞𝑢𝑎𝑡𝑖𝑜𝑛𝑠 𝑜𝑓 𝑠𝑐𝑎𝑙𝑖𝑛𝑔 𝑜𝑏𝑗𝑒𝑐𝑡 𝑎𝑏𝑜𝑢𝑡 𝑎𝑟𝑏𝑖𝑡𝑜𝑟𝑦 𝑝𝑜𝑖𝑛𝑡 𝑎𝑟𝑒:

𝒙′′′ = (𝒙 − 𝒙 𝒇 ) . 𝑺 𝒙 + 𝒙 𝒇 , 𝒚′′′ = (𝒚 − 𝒚 𝒇 ) . 𝑺 𝒚 + 𝒚𝒇

4
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

6.2.C) Rotations
 You can rotate an object about the origin or about a pivot point.
 It is possible to rotate one or more objects or the entire image about any
point in world space in either negative oriented a (clockwise) where angle is
negative oriented or positive oriented (Anti-clockwise) where angle is positive.
6.2.C.1) Rotate about the origin :
- Any point (x,y) can be represented by its radial distance (r) from
the origin and its angle ∅ of the x-axis.
x= r*cos(∅)
y=r*sin(∅) …….. (1)
- If (x,y) is rotated an angle 𝜃 in the Anti-clockwise direction. The
transformed point (𝑥̅ , 𝑦̅) is represented as:
𝑥̅ = 𝑟 ∗ cos(∅ + 𝜃)
𝑦̅ = 𝑟 ∗ sin(∅ + 𝜃)……. (2)

Using the laws of sines and cosines we get:


̅ = 𝒙 𝐜𝐨𝐬(𝜽) - y sin(𝜽)
𝒙
̅ = 𝐱 𝐬𝐢𝐧(𝜽) + 𝒚 𝐜𝐨𝐬(𝜽) ……. (3)
𝒚

Equation (3) are the transformation that rotate a point an angle (𝜃)
about the origin in the Anti-clockwise direction.
To rotate an object each point defining that object must be
transformed using equation (3). The object is then drawn using the list
of transformed points.

5
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

To rotate in clockwise change the angle 𝜃 to - 𝜃 where:


cos (-𝜃) = cos (𝜃)
sin(-𝜃) = -sin (𝜃)
So to rotate a point (x,y) through a clockwise angle 𝜃 about the origin
of the coordinate system we write:

̅ = 𝒙 cos(𝜽)+y sin(𝜽)
𝒙
̅ = −𝒙 sin(𝜽) + y cos (𝜽) …………(4)
𝒚

Equation (4) are the transformation that rotate a point an angle (𝜃)
about the origin in the clockwise direction.

6.2.C.2) Rotate about a pivot point:


After an object is rotated about a specified pivot point, it is still the same
distance away from the pivot point but its orientation has been changed:

Pivot Point Inside object Pivot Point Outside object

To rotate an object an angle (𝜃) about a pivot point three steps


are required:
1. Translate the pivot point (𝑥𝑝 , 𝑦𝑝 ) to the origin. Every point (x,y)
defining the object is translated to a new point (𝑥̅ , 𝑦̅) where:
𝑥̅ = 𝑥 − 𝑥𝑝
𝑦̅ = 𝑦 − 𝑦𝑝

6
Al-Mustansiriyah university
Computer Graphics 2018-2019 Education college
Third stage Computer science department

2. Rotate these translated points (𝑥̅ , 𝑦̅) 𝜃 degree about the origin to
obtain the new point (𝑥, ̿ 𝑦̿)
𝑥̿ = 𝑥̅ ∗ cos(𝜃 ) − 𝑦̅ ∗ sin(𝜃 )
𝑦̿ = 𝑦̅ ∗ cos(𝜃 ) + 𝑥̅ ∗ sin(𝜃 )
Substituting for 𝑥̅ and 𝑦̅
𝑥̿ = (𝑥 − 𝑥𝑝 ) ∗ cos(𝜃 ) − (𝑦 − 𝑦𝑝 ) ∗ sin(𝜃 )
𝑦̿ = (𝑦 − 𝑦𝑝 ) ∗ cos(𝜃 ) + (𝑥 − 𝑥𝑝 ) ∗ sin(𝜃 )
3. Translate the center of rotation back to the pivot point (𝑥𝑝 , 𝑦𝑝 )
𝑥̅̿ = 𝑥̿ + 𝑥𝑝
𝑦̿̅ = 𝑦̿ + 𝑦𝑝

𝑖. 𝑒
𝑥̅̿ =(𝑥 − 𝑥𝑝 ) ∗ cos(𝜃 ) − (𝑦 − 𝑦𝑝 ) ∗ sin(𝜃 ) + 𝑥𝑝
𝑦̿̅ =(𝑦 − 𝑦𝑝 ) ∗ cos(𝜃 ) + (𝑥 − 𝑥𝑝 ) ∗ sin(𝜃 )+ 𝑦𝑝

You might also like