Você está na página 1de 27

Computer Graphics

Engr. Muhammad Umer Haroon


LECTURE 4
Pixel

 The smallest dot illuminated that can be seen on screen.


 A pixel is a point sample.
 An image (in the sense of image-based graphics) is a regular grid of point
samples,
 A pixel is not a little square.
Picture

 Composition of pixels makes picture that forms on whole screen .


 An image (in the sense of image-based graphics) is a regular grid of point
samples,
Resolution

 We know that Graphics images on the screen are built up


from tiny dots called picture elements or pixels.
 The display resolution is defined by the number of rows
from top to
bottom, and number of pixels from left to right on each
line.
 In general, higher the resolution, more pleasing is the picture.
 Higher resolution means a sharper, clearer picture, with less pronounced
‘staircase’ effect on lines drawn diagonally and better looking text
characters.
 On the other hand, higher resolution also means more
memory requirement for the display.
Line

 A line, or straight line, is, roughly speaking, an (infinitely) thin, (infinitely)


long, straight geometrical object, i.e. a curve that is long and straight.
Euclidean geometry, the study of plane and solid figures
on the basis of axioms and theorems employed by the
Greek mathematician Euclid
Mathematically…

 Given two points, in Euclidean geometry, one can always find exactly one line
that passes through the two points; this line provides the shortest connection
between the points and is called a straight line.
A line may have three forms with respect to slope:
 slope = 1
 slope > 1
 slope < 1
Line Cont…
 When slope is 1 , line appears at 45 degree angle
 When slope is less than 1 , line appears at an angle less than 45 degree.
 When slope is greater than 1 , line appears at an angle greater than 45
degree.
 There are three techniques to be discussed to draw a line involving different
time complexities.
Techniques

 Incremental line algorithm


 DDA line algorithm
 Bresenham line algorithm
Rasterization

 Rasterization: Process of determining which pixels provide the best


approximation to a desired line on the screen.
Incremental line algorithm
This algorithm uses simple line equation

y=mx+b

where m = dy / dx

and b = y – m x
 Calculations from board….
Terms as used in algorithm
dx = p2.x – p1. x
dy = p2.y – p1. y
m = dy / dx
x = p1.x
y = p1.y
b=y–m*x
Pseudo Code
if |m| < 1
for counter = p1.x to p2.x
drawPixel (x, y)
x=x+1
y=m*x+b
else
for counter = p1.y to p2.y
drawPixel (x, y)
y=y+1
x=(y–b)/m
Algorithm pros & cons:

Incremental line algorithm is quite simple and easy.


But firstly it involves lot of mathematical
calculations that is for calculating coordinate using
equation each time.
Secondly it works only in incremental direction.
Digital Differential Analyzer (DDA) Algorithm:
 Find difference dx and dy between x coordinates and y coordinates
respectively ending points of a line.

 If |dx| is greater than |dy|, than |dx| will be step and otherwise |dy|
will be step .
Find difference, dx and dy as:
dy = y2 – y1
dx = x2 – x1
if |dx| > |dy| then
step = |dx|
else
step = |dy|

/// step is a variable


///step is the total number of pixel required for a line.
 Now we divide dx and dy by step to get xIncrement and yIncrement
that is the increment required in each step to find next pixel value.
xIncrement = dx/step
yIncrement = dy/step

 // xIncrement is a variable whose value changes along x axis


 // yIncrement is a variable whose value changes along y axis

 Next a loop is required that will run step times. In the loop drawPixel
and add xIncrement
in x1 by and yIncrement in y1.
Complete pseudo code
dx = p2.x – p1. x
dy = p2.y – p1. y
x1=p1.x
y1=p1.y
if |dx|>|dy| then
step = |dx|
else
step = |dy|
xIncrement = dx/step
yIncrement = dy/step
for counter = 1 to step
drawPixel (x1, y1)
x1 = x1 + xIncrement
y1 = y1 + yIncrement
 Board work
 To use floating point calculation, which requires more space as well as they
have more computational cost.

 Line wont be smooth

Você também pode gostar