gvc04 line draw
TRANSCRIPT
-
8/7/2019 GVC04 Line Draw
1/36
GVC-432
Lecture - 4
Ref: Donald Hearn &M. Pauline Baker ,
Computer GraphicsFoley, van Dam, Feiner & Hughes,
Computer Graphics Principles & Practice
Dr Pavan ChakrabortyIIIT-Allahabad
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
2/36
Coordinate Systems
Pixel Coordinate System- rows and columns
Rectilinear
Usually for graphics, westart at top left corner andwork our way across anddown
Same as raster orientation
Array[row][column]
Row major used in Cand
C+ ( last index moves
fastest inmemory)
Not all languages do it this
way - eg Fortran uses
columnmajor (first indexmoves fastest)
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
3/36
Cartesian
Coordinates Often we use theCartesian coordinateconvention ie x,y
coordinates (or x,y,z in3D) and map this toour display
Usually columncorresponds to x, and-row corresponds to y
This works if we know themaxmin values.
Common values are eg
640 columns x 320 rows
Or1024x768 or better
Aspect ratio is the ratio oftheseeg 4:3 - Chosen to suitthe common display deviceseg TV screens ormonitors
x
y
Indian Institute of Information Technology - Allahabad
y
-
8/7/2019 GVC04 Line Draw
4/36
Drawing Space or Canvas
Coordinate Systems
Drawing Primitives
Library of utilities eg drawDot( int x, int y);
Or drawLine( x1, y1, x2, y2 );
Usually we have PrimitiveModels for coordinatespaces and colours
We do not want to write ourapplication programsworrying about pixelresolutions
Wemay have libraries thatallow us to do so, but oftenthey will support moregeneral coordinates
Eg real space normalisedcoordinates [0.0,1.0]
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
5/36
Colours in Brief
Red Green Blue is not theOnly colourmodel althoughstill most common
We specify separate RGBvalues foreach pixel
They map to intensities
All colours can beexpressed as combination
of these
Sometimes also an alphaor transparency value
These will conveniently packinto a computer word of 4
bytes, one byte foreach
entity
1 Byte gives us 256 values- hencenumbers of colours
Need not use this
resolution
Can also use Look-up
tables to save memory
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
6/36
Graphics Libraries and
packages What is a graphics
system?
A package orLibrary that
links to a Language or
environment and lets us
write programs that are
independent of thegraphics hardware and
devices
Java Development Kit and
Java 2D and Java 3D
libraries
GL and OpenGL
(Graphics Library), VOGL
X11, DirectX, PHIGS,
and lots of others
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
7/36
Summary Graphics has a varied
history
Very technology driven
Good advances in recentyears with adequate
memory and processing
power
Primitives and library
layers approach is very
common
Note devices and memorymodel
Colourmodels and drawingspaces are important ideas
for our programs
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
8/36
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 areeither lit ornot lit
-
8/7/2019 GVC04 Line Draw
9/36
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 beefficient, drawnquickly Lots of them are required!!!
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
10/36
Simple Line
Based onslope-intercept
algorithm from algebra:
y = mx + b
Simple approach:
increment x, solve for y
Floating point arithmetic
required
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
11/36
Indian Institute of Information Technology - Allahabad
LINE-DRAWING ALGORITHMS
-
8/7/2019 GVC04 Line Draw
12/36
Does it Work?It seems to work okay for
lines with a slope of 1 or
less,
but doesnt work well for
lines with slope greater than1 lines becomemore
discontinuous in appearance
and wemust add more than
1 pixel per column to makeitwork.
Solution? - usesymmetry.
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
13/36
Modify algorithm per
octant
OR, increment along x-axis if dy
-
8/7/2019 GVC04 Line Draw
14/36
DDA algorithm
DDA = Digital Differential Analyser
finite differences
Treat line as parametric equationin t :
)()(
)()(
121
121
yytyty
xxtxtx
!
!),(
),(
22
11
yx
yxStart point -
End point -
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
15/36
DDA Algorithm Startat t= 0
At each step, incrementtbydt
Choose appropriate value fordt
Ensure no pixels are missed:
Implies: and
Setdtto maximum ofdx and dy
)()(
)()(
121
121
yytyty
xxtxtx
!
!
dt
dyyy
dt
dxxx
oldnew
oldnew
!
!
1dt
dx1
dt
dy
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
16/36
Indian Institute of Information Technology - Allahabad
DDA AlgorithmThe digitaldifferentialanalyzer(DDA)
myyxmkk!!(e
110.10.0 myyxm kk !!(e 110.10.0
m
xxymkk
110.1 1 !!(
mxxymkk
110.1 1 !!("
myyxm kk !!(u" 110.10.0
Equations are based on the assumption that lines are to be processed
from the left endpoint to the right endpoint.
-
8/7/2019 GVC04 Line Draw
17/36
Indian Institute of Information Technology - Allahabad
#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
-
8/7/2019 GVC04 Line Draw
18/36
DDA algorithmline(int x1, int y1, int x2, int y2)
{
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;
}
}
n - range oft.
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
19/36
DDA algorithm Still need a lot of floating point arithmetic.
2 rounds 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
-
8/7/2019 GVC04 Line Draw
20/36
Observation on lines.
while( n-- )
{draw(x,y);
move right;
if( below line )
move up;
}
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
21/36
Testing for the side of a
line. Need a test to determine which side of a line
a pixel lies.
Write the lineinimplicit form:
0),( !! cbyaxyxF
Easy to prove F0 for points below.
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
22/36
Testing for the side of a
line.
Need to find coefficients a,b,c.
Recall explicit, slope-intercept form :
So:
0),( !! cbyaxyxF
bxdx
dyybmxy !! soand
0..),( !! cydxxdyyx
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
23/36
-
8/7/2019 GVC04 Line Draw
24/36
Decision variable.
Evaluate d fornext pixel, Depends on whetherE orNE Is chosen :
IfE chosen :
cybxayxFd ppppnew !! )21()2()
21,2(
Butrecall :
cybxa
yxd
pp
ppold
!
!
)21()1(
)2
1,1(
So :
dyd
add
old
oldne
!
!
M
E
NE
Previous
Pixel
(xp,yp)
Choices for
Current pixel
Choices for
Next pixel
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
25/36
Decision variable.IfNE was chosen :
cybxayxd ppppne !! )
2
3()2()
2
3,2(
So :
dxdyd
badd
old
oldnew
!
!
M
E
NE
Previous
Pixel
(xp,yp)
Choices for
Current pixel
Choices for
Next pixel
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
26/36
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 ford
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
27/36
Initial value of d.
2
)2
1()1()
2
1,1(
11
1111
bacbyax
cybxayxFdstart
!
!!
But(x1,y1) is a point on the line, so F(x1,y1)=0
2/dxdydstart !
Conventionalto multiply by 2 to remove fraction doesnt effect sign.
2),( 11
bayx !
Start point is (x1,y1)
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
28/36
Bresenham algorithmvoid MidpointLine(int
x1,y1,x2,y2)
{
int dx=x2-x1;int dy=y2-y1;
int d=2*dy-dx;
int increE=2*dy;
int incrNE=2*(dy-dx);
x=x1;y=y1;
WritePixel(x,y);
while (x < x2) {
if (d
-
8/7/2019 GVC04 Line Draw
29/36
Bresenham was not the end!
2-step algorithm by XiaolinWu:
(see Graphics Gems 1, by BrianWyvill)
Treat line drawing as an automaton , orfinite statemachine, ie. looking at next two
pixels of a line, easy to see that only a finite
set of possibilities exist.
The2-step algorithmexploits symmetry bysimultaneously drawing from both ends
towards themidpoint.
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
30/36
Two-step Algorithm
Possible positions ofnext two pixels dependent on slope
current pixel in blue:
Slope between 0 and
Slope between and 1
Slope between1 and 2
Slope greater than2
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
31/36
Circle drawing. Can also use Bresenham to draw circles.
U
se 8-fol
d symmetr
y
Choices for
Next pixel
M
E
SE
Previous
PixelChoices for
Current pixel
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
32/36
Circle drawing. Implicit form foracircle is:
)32(chosenisIf
)522(chosenisSIf
!
!
poldne
ppoldne
xdd
yxdd
Functions are linearequations in terms of(xp,yp)
Termed point of evaluation
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
33/36
Problems with Bresenham
algorithm
Pixels are drawn as a single lineunequal lineintensity with changein angle.
Pixel density= n pixels/mm
Pixel density=2.n pixels/mm
Can draw lines in darkercoloursaccording to line direction.
- Better solution : antialiasing !
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
34/36
Summary of line drawing
so far. Explicit form of line
Inefficient, difficult to control.
Parametric form of line. Express linein terms of parametert
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
-
8/7/2019 GVC04 Line Draw
35/36
Indian Institute of Information Technology - Allahabad
-
8/7/2019 GVC04 Line Draw
36/36
Sampling theorytells us aliasing is caused by frequencies being presentabovethe Nyquistlimit.
Ideal solution : band-pass filterto remove high frequencies.
Fouriertransform tells us the transform ofa band-pass filter is a sinc function.
Convolution theorytells us we can convolve witha sinc function in the spatialdomain instead.
A sinc function is an impractical filter.
Summary of aliasing.
Indian Institute of Information Technology - Allahabad