You are on page 1of 31

Graphics

Primi,ves

IT 520 - Computer Graphics


Hakim Usoof
hau@ucsc.cmb.ac.lk
Topics
•  Points
•  Lines
•  Circles

18-Jun-17 Hakim Usoof 2


Coordinate system

18-Jun-17 Hakim Usoof 3


Points
•  PloDng is straight forward opera,on
on a screen
•  Given the x and y coordinates a
corresponding pixel will be illuminated
on a raster display

18-Jun-17 Hakim Usoof 4


Lines
•  Not a problem in vector displays or ploKers as
coordinates are con,nuous
•  On digital displays however, coordinates are
non-con,nuous
–  E.g. (10.45,20.89) will be rounded to (10,21)
–  Rounding causes lines to be jaggies (step like)
–  Highly no,ceable on low resolu,on displays
–  Can be improved by varying the intensity on
adjacent pixels (an,-aliasing)
–  Colour a pixel putpixel(x,y,intensity) go read a
current intensity of a pixel getpixel(x,y)

18-Jun-17 Hakim Usoof 5


Lines drawing
•  A line is a set of points lying between
two points (x1,y1) and (x2,y2) on the
digital (raster) system.
–  Using Cartesian slope-intercept equa,on
y = m.x + c
y −y
m= 2 1
x2 − x1
c = y1 − m.x1
Δy = m.Δx
Δy
Δx =
18-Jun-17 Hakim Usoof m 6
Assignment 1
•  Plot the line between (3,5) and (10,12)
•  Plot the line between (3,3) and (10,15)

18-Jun-17 Hakim Usoof 7


Line
•  When |m|< 1, Δx can be set propor,onal
to a small horizontal deflec,on voltage
and the corresponding ver,cal is then set
propor,onal to Δy
•  When |m|> 1, Δy can be set propor,onal
to a small ver,cal deflec,on voltage and
the corresponding horizontal is then set
propor,onal to Δx
•  m=1, Δx=Δy which mean the ver,cal and
horizontal deflec,on voltages are equal
18-Jun-17 Hakim Usoof 8
Lines on Raster systems
•  PloKed on pixels
•  Step size in horizontal and ver,cal
systems are constrained by pixel
separa,on
•  Sampled a line by discrete posi,ons
and determine the nearest pixel to the
line at each sampled posi,on

18-Jun-17 Hakim Usoof 9


Digital Differen,al Analyzer
(DDA) Algorithm
Consider first a line with posi,ve slope.
–  If m ≤ 1, we sample at unit x intervals (Δx =
1) and compute each successive y value as
yk+1 = yk + m
–  Subscript k takes integer values star,ng
from 1, for the first point, and increases by 1
un,l the final endpoint is reached. Since m
can be any real number between 0 and 1,
the calculated y values must be rounded to
the nearest integer.

18-Jun-17 Hakim Usoof 10


DDA Algorithm
Consider first a line with posi,ve slope.
–  If m ≥ 1, we sample at unit y intervals (Δy
= 1) and compute each successive x value
as
xk+1 = xk + 1/m
–  Basically roes of x and y are revered

18-Jun-17 Hakim Usoof 11


DDA algorithm
Consider lines with a nega,ve slope
–  We use Δx = -1 when |m| ≤ 1
yk+1 = yk - m
–  We use Δy = -1 when |m| ≥ 1
xk+1 = xk - 1/m

18-Jun-17 Hakim Usoof 12


Review of DDA
•  Eliminated mul,plica,on
•  In long line calculated segments
posi,ons of the pixel drils from the
true path
•  Rounding and Floa,ng point
opera,ons s,ll have a considerable
cost

18-Jun-17 Hakim Usoof 13


lineDDA.c
#define ROUND(a) ((int)(a+0.5))

void lineDDA(int xa, int ya, int xb, int yb) {
int dx=xb-xa, dy=yb-ya, steps, k;
float xIncrement, yIncrement, x=xa, y=ya;
if(abs(dx)>abs(dy)) steps=abs(dx);
else
steps=abs(dy);
xIncrement = dx/(float) steps; yIncrement = dy/(float) steps;
setpixel(ROUND(x),ROUND(y));
for(k=0;k<steps;k++){
x += xIncrement;
y += yIncrement;
setpixel(ROUND(x),ROUND(y));
}
getche();
}

void setpixel(x,y) {
int x,y;
putpixel(x,480-y,RED);
}

18-Jun-17 Hakim Usoof 14


Assignment 2
Plot the line between (3,3) and (10,15)
using the DDA algorithm

18-Jun-17 Hakim Usoof 15


Bresenham’s Algorithm
Assume that the line equa,on
is
y=mx+c
and (xk,yk) has been already
Line ploKed.
Yk+2
Then the next pixel on the line
Yk+1 d1 can be:

Yk d2 (xk+1,yk) or (xk+1,yk+1)
depending on d1>d2 or d1<d2
The exact posi,on on the line
Xk Xk+1 Xk+2 Xk+3
is
y=m(xk+1)+c

18-Jun-17 Hakim Usoof 16
Bresenham’s Algorithm
Let Δx= xn-x1, Δy= yn-y1; m = Δy/ Δx
d1 = yk+1-y
d2= y-yk

d1-d2 =2yk-2y+1

= 2yk - 2 (m(xk+1)+c)+1
= 2yk – 2.(Δy/ Δx)xk-2(Δy/ Δx)-2c+1

Define Pk= Δx (d1-d2)

18-Jun-17 Hakim Usoof 17


Bresenham’s Algorithm
Pk = Δx (2yk –2. (Δy/ Δx)xk-2 Δy/ Δx-2c+1)
= 2 Δx yk- 2 xi –2 Δy -2c. Δx + Δx
= 2 Δx yk- 2 Δy xk + A
where A= Δx - 2c. Δx -2 Δy

If Pk>0 Then plot (xk+1,yk)
else Plot (xk+1,yk+1)

Consider
Pk+1 = 2 Δx yk+1- 2 Δy xk+1 +A
Pk +1 - Pk = 2 Δx (yk+1- yk) -2 Δy(xk+1 – xk)
8-Jun-17
1 Hakim Usoof 18
Bresenham’s Algorithm
Pk +1 = Pk +2 Δx (yk+1- yk) -2 Δy(xk+1 – xk)
Since unit steps are taken in x direc,on,
xk+1 – xk=1
Pk +1 = Pk +2 Δx (yk+1- yk) -2 Δy
IF Pk>0 then yk+1 = yk else yk+1 = yk +1
IF Pk>0 then
Pk +1 = Pk-2 Δy
else
Pk +1 = Pk +2 Δx-2 Δy
P1 = 2Δy - Δx

18-Jun-17 Hakim Usoof 19
Bresenham’s Algorithm
Bresenham’s algorithm for |m| < 1
① Input the two line endpoints (x1, y1) and
(xn, yn).
② Plot (x1,y1)
③ Compute Δx , Δy , 2Δy and 2Δy − 2Δx
④ Compute p1 = 2Δy − Δx
⑤ For each xk (star,ng at k = 1, perform
decision test and plot the next pixel.
⑥ Repeat steps 5 for Δx ,mes.

18-Jun-17 Hakim Usoof 20


Bresenham’s Algorithm
•  For lines with m > 1 interchange the roles
of x and y direc,ons.
•  If the line is ploKed from right to lel
both x and y is decreases as we step from
right to lel.
•  For m < 0 the procedure is the same as
for posi,ve slopes but one coordinate
increases and the other decreases.
•  Cases such as Δx=0,Δy=0 and Δx=Δy can
be directly ploKed without processing via
the algorithm.
18-Jun-17 Hakim Usoof 21
Assignment 2
Plot the line between (3,3) and (13,11)
using the Bresenham’s algorithm

18-Jun-17 Hakim Usoof 22


Circles
Circle equa,on
(x-xc)2+(y-yc)2 = r2
For a given x value y values can be
calculated as (x,y)

y=yc ± √r2-(x-xc)2 r

(xc,yc)

18-Jun-17 Hakim Usoof 23


Circles
Using Polar coordinates (x,y)

x= xc+r cos (θ) θ

y = yc+r sin (θ)



Could set step size of θ as 1/r degrees to
get uniform spacing. But this is s,ll
computa,onally expensive due to
trigonometric func,on evalua,ons.

18-Jun-17 Hakim Usoof 24
Circle drawing Algorithm
•  If (x,y) is on the circle, then seven other points
on the circle can be iden,fied due to symmetry.

(-y,x) (y,x)

(-x,y) (x,y)
45o
(-x,-y) (x,-y)

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

18-Jun-17 Hakim Usoof 25


Bresenham’s Circle Drawing
Algorithm
xk xk+1 xk+2 xk+3 xk+4 Assume that (xi,yi) has
yk d1 been already ploKed.
Then the next pixel
yk-1 d2
on the circle path is
either
yk-2
(xk+1,yk) or (xk+1,yk-1)
yk-3

yk-4 The point on the
circle path is
y2=r2- (xk+1)2

18-Jun-17 Hakim Usoof 26


Bresenham’s Circle Drawing
Algorithm
Define d1= yk2 –y 2
d2= y2- (yk-1)2
Let Pk= d1-d2
= yk2-2y2+(yk-1)2
= y 2 –2(r2- (x +1)2)+y 2-2y +1
k k k k
= 2yk2+2xk2+4xk-2yk-2r2+3
Pk+1 = 2yk+12 +2(xk+1)2+4(xk+1)-2yk+1-2r2+3
Pk+1- Pk = 2 (yk+12 -yk2) –2 (yk+1-yk)+4xk+6
(note xk+1=xk+1)

18-Jun-17 Hakim Usoof 27
Bresenham’s Circle Drawing
Algorithm
yk+1=yk –1 or yk+1=yk

If Pk>0 Then
Pk+1= Pk+4 (xk-yk)+4xk+10
else
Pk+1=Pk+4xk+6

P1=3-2r
18-Jun-17 Hakim Usoof 28
Midpoint Circle Algorithm
xk xk+1 xk+2 xk+3 xk+4 Another approach to
yk decide which pixel is
yk-1
closer to the circle
p a t h i s t o t e s t
yk-2 midpoint whether the midpoint
of the two pixels is
yk-3 inside or outside the
yk-4 circle.
Let f(x,y)= x2+y2-r2

18-Jun-17 Hakim Usoof 29


Midpoint Circle Algorithm
Define Pk= f(midpoint) =f(xk+1,yk-1/2)
If Pk>0 then select (xk+1,yk-1) else select
(xk+1,yk).
Pk=(xk+1)2+(yk –1/2)2 – r2

18-Jun-17 Hakim Usoof 30


Assignment 3
•  Draw the 1st ad 2nd octant of the circle
with r = 10 and center (0,0) using the
Midpoint algorithm

18-Jun-17 Hakim Usoof 31

You might also like