GVC-432 Ref: Donald Hearn & M. Pauline Baker ,: Lecture - 4

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 36

GVC-432

Lecture - 4
Ref: Donald Hearn & M. Pauline Baker ,
Computer Graphics
Foley, van Dam, Feiner & Hughes,
Computer Graphics Principles & Practice

Dr Pavan Chakraborty
IIIT-Allahabad

Indian Institute of Information Technology - Allahabad


Coordinate Systems
• Pixel Coordinate System • Array[row][column]
- rows and columns

• Rectilinear • Row major used in C and


C+ ( last index moves
fastest in memory)
• Usually for graphics, we
start at top left corner and
work our way across and • Not all languages do it this
down way - eg Fortran uses
column major (first index
• Same as raster orientation moves fastest)

Indian Institute of Information Technology - Allahabad


y

x Cartesian
y
Coordinates
• Often we use the • This works if we know the
Cartesian coordinate max min values.
convention ie x,y
coordinates (or x,y,z in • Common values are eg
3D) and map this to
640 columns x 320 rows
our display
• Or 1024x768 or better
• Usually column
corresponds to x, and
-row corresponds to y • Aspect ratio is the ratio of
these eg 4:3 - Chosen to suit
the common display devices
eg TV screens or monitors
Indian Institute of Information Technology - Allahabad
Drawing Space or Canvas
• Coordinate Systems • We do not want to write our
application programs
worrying about pixel
• Drawing Primitives resolutions

• Library of utilities • We may have libraries that


– eg drawDot( int x, int y);
allow us to do so, but often
– Or drawLine( x1, y1, x2, y2 );
they will support more
general coordinates
• Usually we have “Primitive”
Models for coordinate spaces•
Eg real space “normalised”
and colours
coordinates [0.0,1.0]

Indian Institute of Information Technology - Allahabad


Colours in Brief
• Red Green Blue is not the • These will conveniently pack
Only colour model although into a computer word of 4
still most common bytes, one byte for each
• We specify separate RGB entity
values for each pixel
• 1 Byte gives us 256 values
• They map to intensities - hence numbers of colours
• All colours can be • Need not use this
expressed as combination resolution
of these
• Can also use Look-up
• Sometimes also an “alpha” tables to save memory
or transparency value

Indian Institute of Information Technology - Allahabad


Graphics Libraries and
packages
• What is a graphics • Java Development Kit and
system? Java 2D and Java 3D
libraries
• A package or Library that
links to a Language or • GL and OpenGL
environment and lets us (Graphics Library), VOGL
write programs that are
independent of the • X11, DirectX, PHIGS,…
graphics hardware and and lots of others
devices

Indian Institute of Information Technology - Allahabad


Summary
• Graphics has a varied • Note devices and memory
history model
• Very technology driven • Colour models and drawing
spaces are important ideas
• Good advances in recent for our programs
years with adequate
memory and processing
power

• Primitives and library layers


approach is very common

Indian Institute of Information Technology - Allahabad


Towards the Ideal Line
• We can only do a discrete approximation

• Illuminate pixels as close to the true path


as possible, consider bi-level display only
– Pixels are either lit or not lit
What is an ideal line
• Must appear straight and continuous
– Only possible axis-aligned and 45o lines

• Must interpolate both defining end points

• Must have uniform density and intensity


– Consistent within a line and over all lines
– What about antialiasing?

• Must be efficient, drawn quickly


– Lots of them are required!!!

Indian Institute of Information Technology - Allahabad


Simple Line
Based on slope-intercept
algorithm from algebra:
y = mx + b
Simple approach:
increment x, solve for y
Floating point arithmetic
required

Indian Institute of Information Technology - Allahabad


LINE-DRAWING ALGORITHMS

Indian Institute of Information Technology - Allahabad


Does it Work?
It seems to work okay for
lines with a slope of 1 or
less,
but doesn’t work well for
lines with slope greater than
1 – lines become more
discontinuous in appearance
and we must add more than
1 pixel per column to make it
work.
Solution? - use symmetry.
Indian Institute of Information Technology - Allahabad
Modify algorithm per
octant

OR, increment along x-axis if dy<dx


else increment along y-axis
DDA algorithm
• DDA = Digital Differential Analyser
– finite differences
• Treat line as parametric equation in t :

Start point - ( x1 , y1 )
End point - ( x2 , y 2 ) x(t )  x1  t ( x2  x1 )
y (t )  y1  t ( y2  y1 )

Indian Institute of Information Technology - Allahabad


DDA Algorithm x(t )  x1  t ( x2  x1 )
y (t )  y1  t ( y2  y1 )

• Start at t = 0
dx
• At each step, increment t by dt xnew  xold 
dt
dy
• Choose appropriate value for dt ynew  yold 
dt
• Ensure no pixels are missed:
– Implies: dx and dy
1 1
dt dt

• Set dt to maximum of dx and dy

Indian Institute of Information Technology - Allahabad


DDA Algorithm
The digital differential analyzer (DDA)
0.0  m  1.0  x  1  yk 1  yk  m
1
1.0  m  y  1  xk 1  xk 
m
Equations are based on the assumption that lines are to be processed from
the left endpoint to the right endpoint.

0.0  m  1.0  x  1  yk 1  yk  m
1
 1.0  m  y  1  xk 1  xk 
m
Indian Institute of Information Technology - Allahabad
DDA Algorithm
#include 'device. h"
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 (ROUNDlxl, ROUND(y) ) :


for (k=O; k<steps; k++) {
x += xIncrment;
y += yIncrement;
setpixel (ROUND(x), ROUND(y));
}}
1 Indian Institute of Information Technology - Allahabad
DDA algorithm
line(int x1, int y1, int x2, int y2)

{ n - range of t.
float x,y;
int dx = x2-x1, dy = y2-y1;
int n = max(abs(dx),abs(dy));
float dt = n, dxdt = dx/dt, dydt = dy/dt;
x = x1;
y = y1;
while( n-- ) {
point(round(x),round(y));
x += dxdt;
y += dydt;
}
}

Indian Institute of Information Technology - Allahabad


DDA algorithm
• Still need a lot of floating point arithmetic.
– 2 ‘round’s and 2 adds per pixel.

• Is there a simpler way ?


• Can we use only integer arithmetic ?
– Easier to implement in hardware.

Indian Institute of Information Technology - Allahabad


Observation on lines.
while( n-- )
{
draw(x,y);
move right;
if( below line )
move up;
}

Indian Institute of Information Technology - Allahabad


Testing for the side of a
line.
• Need a test to determine which side of a line
a pixel lies.
• Write the line in implicit form:

F ( x, y )  ax  by  c  0
• Easy to prove F<0 for points above the
line, F>0 for points below.

Indian Institute of Information Technology - Allahabad


Testing for the side of a
line.
F ( x, y )  ax  by  c  0
• Need to find coefficients a,b,c.
• Recall explicit, slope-intercept form :

• So:

dy
y  mx  b and so y  xb
dx

F ( x, y )  dy.x  dx. y  c  0

Indian Institute of Information Technology - Allahabad


Decision variable.
1
Evaluate F at point M d  F ( x p  1, y p  )
2
Referred to as decision variable

NE

M
E

Previous Choices for Choices for


Pixel Current pixel Next pixel
(xp,yp)

Indian Institute of Information Technology - Allahabad


Decision variable.
Evaluate d for next pixel, Depends on whether E or NE Is chosen :

If E chosen :

1 1
d new  F ( x p  2, y p  )  a ( x p  2)  b( y p  )  c
2 2
But recall :
1
d old  F ( x p  1, y p  )
NE 2
1
M  a ( x p  1)  b( y p  )  c
2
E
Previous Choices for
So :
d new  d old  a
Pixel Choices for Next pixel
(xp,yp) Current pixel  d old  dy
Indian Institute of Information Technology - Allahabad
Decision variable.
If NE was chosen :
3 3
d new  F ( x p  2, y p  )  a ( x p  2)  b( y p  )  c
2 2

M So :
NE
d new  d old  a  b
E
 d old  dy  dx
Previous Choices for
Pixel Choices for Next pixel
(xp,yp) Current pixel

Indian Institute of Information Technology - Allahabad


Summary of mid-point
algorithm
• Choose between 2 pixels at each step based
upon sign of decision variable.
• Update decision variable based upon which
pixel is chosen.
• Start point is simply first endpoint (x1,y1).
• Need to calculate initial value for d

Indian Institute of Information Technology - Allahabad


Initial value of d.
Start point is (x1,y1)
1 1
d start  F ( x1  1, y1  )  a( x1  1)  b( y1  )  c
2 2
b
 ax1  by1  c  a 
2
b
 F ( x1 , y1 )  a 
2

But (x1,y1) is a point on the line, so F(x1,y1) =0

d start  dy  dx / 2
Conventional to multiply by 2 to remove fraction  doesn’t effect sign.

Indian Institute of Information Technology - Allahabad


Bresenham algorithm
void MidpointLine(int
x1,y1,x2,y2) while (x < x2) {
{ if (d<= 0) {
int dx=x2-x1; d+=incrE;
int dy=y2-y1; x++
int d=2*dy-dx; } else {
d+=incrNE;
int increE=2*dy; x++;
int incrNE=2*(dy-dx); y++;
}
x=x1;
WritePixel(x,y);
y=y1; }
WritePixel(x,y); }

Indian Institute of Information Technology - Allahabad


Bresenham was not the end!
2-step algorithm by Xiaolin Wu:
(see Graphics Gems 1, by Brian Wyvill)
Treat line drawing as an automaton , or
finite state machine, ie. looking at next two
pixels of a line, easy to see that only a finite
set of possibilities exist.
The 2-step algorithm exploits symmetry by
simultaneously drawing from both ends
towards the midpoint.

Indian Institute of Information Technology - Allahabad


Two-step Algorithm
Possible positions of next two pixels dependent on slope
– current pixel in blue:
Slope between 0 and ½

Slope between ½ and 1

Slope between 1 and 2

Slope greater than 2

Indian Institute of Information Technology - Allahabad


Circle drawing.
• Can also use Bresenham to draw circles.

• Use 8-fold symmetry

M
SE

Previous Choices for Choices for


Pixel Current pixel Next pixel

Indian Institute of Information Technology - Allahabad


Circle drawing.
• Implicit form for a circle is:
f ( x, y )  ( x  xc ) 2  ( y  yc ) 2  r 2

If SE is chosen d new  d old  (2 x p  2 y p  5)


If E is chosen d new  d old  (2 x p  3)

• Functions are linear equations in terms of (xp,yp)


–Termed point of evaluation

Indian Institute of Information Technology - Allahabad


Problems with Bresenham
algorithm
• Pixels are drawn as a single line 
unequal line intensity with change in angle.

Pixel density = 2.n pixels/mm


Can draw lines in darker colours
according to line direction.
- Better solution : antialiasing !

Pixel density = n pixels/mm

Indian Institute of Information Technology - Allahabad


Summary of line drawing
so far.
• Explicit form of line
– Inefficient, difficult to control.
• Parametric form of line.
– Express line in terms of parameter t
– DDA algorithm
• Implicit form of line
– Only need to test for ‘side’ of line.
– Bresenham algorithm.
– Can also draw circles.

Indian Institute of Information Technology - Allahabad


Indian Institute of Information Technology - Allahabad
Summary of aliasing.
• Sampling theory tells us aliasing is caused by frequencies being present
above the Nyquist limit.
• Ideal solution : band-pass filter to remove high frequencies.
• Fourier transform tells us the transform of a band-pass filter is a sinc function.

• Convolution theory tells us we can convolve with a sinc function in the


spatial domain instead.
• A sinc function is an impractical filter.

Indian Institute of Information Technology - Allahabad

You might also like