BASIC RASTER GRAPHICS
ALGORITHMS FOR 2D
DRAWING
2020/2020 - 1 Materi 02 – Komputer Grafik-
Raster Display
1
Primitive element: pixel
Raster: A rectangular array of points or dots
Pixel: One dot or picture element of the raster
Scan line: A row of pixels
Great asset: total control of the image
Biggest problems: you work at a particular RESOLUTION
Plotting Pixels
2
In it’s most basic form, each pixel can be set to
black or white (i.e. turned on or off), allowing
patterns of dots to be created on the screen.
Classic problem… “stair casing”.
Memory Mapping
3
Drawing on the computer screen is achieved by setting the right
pixels either on or off. Each pixel on the screen corresponds to an
address in the computers memory - this is known as memory
mapping and the display is said to be a “memory mapped
display.”
Memory Mapping
4
Cartesian Coordinate System
5
In practice, the Cartesian coordinate system is used to define the location of
pixels, leaving the computer to convert the coordinates to specific memory
locations.
The modern Cartesian coordinate system in two dimensions (also called a
rectangular coordinate system) is defined by two axes, at right angles to
each other, forming a plane (an xy-plane). The horizontal axis is labeled x,
and the vertical axis is labeled y.
The coordinates in a three dimensional system are of the form (x,y,z).
Cartesian Coordinate System
6
p(x, y)
O x
Line Drawing
7
A (straight) line can be mathematically defined by its end points. To
describe a line we need simply to state the coordinates of the two ends.
e.g. (3, 8) to (12, 20)
p(12, 20)
p(3, 8)
x
Line Drawing
8
Drawing lines on a raster grid implicitly involves approximation. The general
process is called rasterization or scan-conversion.
Optimal Line Drawing
9
What is the best way to draw a line from the pixel
(x1,y1) to (x2,y2)? Such a line should ideally have the
following properties:
straight
pass through endpoints
smooth
independent of endpoint order
uniform brightness
brightness independent of
slope
efficient
Line Characterisations
y
10
(xi , yi) Explicit: y = mx + B
Implicit: F(x,y) = ax + by + c = 0
Constant slope: ∆y
O x =m
∆x
The simplest strategy is:
1) Compute m;
(xi , yi)
2) Increment x by 1 starting
with the leftmost point;
3) Calculate yi = mxi + B;
(xi , Round(yi)) 4) Intensify the pixel at (xi ,
Round(yi)), where
Round(yi) = Floor(0.5+yi)
The above brute-force strategy is inefficient, however, because
11
each iteration requires a floating-point (or binary fraction) multiply,
Addition, and invocation of Floor. We can eliminate the multiplication
by noting that:
yi +1 = mxi +1 + B = m( xi + ∆x) + B = yi + m∆x
And if ∆x =1, then yi+1 = yi + m. Thus, a unit of change in x changes
y by m, which is the slope of the line.
Simple Incremental
Algorithm
(xi , yi)
An incremental algorithm can
be defined: At each step, we
make incremental calculations
(xi+1 , yi+1) (xi , Round(yi))
based on the preceding step,
starting from one of the
endpoints.
(xi +1, yi + m)
Round= Returns the integral value
that is nearest to x, with halfway
cases rounded away from zero
Slope Problem
12
Note that in order to implement the above algorithm, the slope m has to be
between 0 and 1; then we are able to step along x axis; otherwise, we
must reverse the roles of x and y and assigning a unit step to y and
incrementing x by ∆x = ∆y/m = 1/m.
m >1, cannot step along m <1, can step along x.
x. To handle this, swap x
and y.
Midpoint Line (Bresenham) Algorithm
13
What is wrong with the incremental algorithm?
– It requires floating-point operations (Round)
– The time-consuming floating-point operations are
unnecessary because both endpoints are integers.
– Therefore, instead of incrementing y and then rounding it at each
step, we just go to the right, or to the right and up using only
integer quantities.
Increment Decision
Assume the slope of the line, m, is
between 0 and 1.Consider the line
in the right figure, where the NE
previously selected pixel is P (xp ,
yp). Now, we must choose between
the pixel one increment to the right M Q
(called the east pixel, E) or the
pixel one increment to the right E
and one increment up (called the P = (xp , yp)
northeast pixel, NE). Let Q be the
intersection point of the line being
scan-converted with the grid line x The problem becomes
= xp+1. Let M be the midpoint
between E and NE. It is easy to to decide on which side
see that , if M lies above the line,
pixel E is closer to the line; if M is of the line the midpoint
below the line, pixel NE is closer to
the line. lies?
14
Solution
15
P (x1,y1)
Let’s consider the explicit form of the
dy=y1-y0
line: y = mx + B.
We have P (x0,y0)
F(x,y) = dy × x – dx × y + B × dx = 0. dx=x1 – x0
Compare with the implicit form
dy y1 − y0
F(x,y) = ax + by + c = 0 m=
We have a = dy, b = -dx, and c = B × dx x1 − x0
dx.
Given a point (x , y), investigate the sign of F:
+ Point below the line
F ( x, y ) = 0 Point on the line
−
Point above the line
Decision Variable d
16
Since we’re trying to decide the
relationship between M and the line,
we need only to compute NE (xp+1, yp+1)
1
F ( M ) = F ( x p + 1, y p + ) Q 1
2
M ( x + 1, y + )
and to test its sign. p p
2
(xp , yp) (xp +1, yp)
E
Define a decision variable d:
1 1
d = F ( x p + 1, y p + ) = a ( x p + 1) + b( y p + ) + c
2 2
+ Choose pixel NE
= 0 Choose pixel E
−
Choose pixel E
How will d be used?
17
(xp+1, yp+1)
NE (xp+2, yp+1)
1 Q 1
d old = F ( x p + 1, y p + ) d new = F ( x p + 2, y p + )
2 M 2
(xp , yp)
E
(xp +1, yp) (xp +2, yp)
Assume E is chosen, M is incremented by one step in the x direction. Then
1 1
d new = F ( x p + 2, y p + ) = a ( x p + 2) + b( y p + ) + c
2 2
1 1
= a ( x p + 1) + a + b( y p + ) + c = F ( x p + 1, y p + ) + a
2 2
= d old + a = d old + dy
Incremental update
18
We call the increment to add after E is chosen ∆E : ∆E = a = dy. In other words,
we can derive the value of the decision variable at the next step incrementally
from the value at the current step without having to computing F(M) directly, by
merely adding ∆E .
On the other hand, if NE is chosen:
3 3
d new = F ( x p + 2, y p + ) = a ( x p + 2) + b( y p + ) + c
2 2
1 1
= a ( x p + 1) + a + b( y p + ) + b + c = F ( x p + 1, y p + ) + a + b
2 2
= d old + a + b = d old + dy − dx
Therefore, ∆NE = a + b= dy – dx.
1 1
Now the initial condition: F ( x0 + 1, y0 + ) = a ( x0 + 1) + b( y0 + ) + c
2 2
= F ( x0 , y0 ) + a + b / 2
Hence, dstart = a +b/2 = dy – dx/2.
Summary to the midpoint technique
19
Let’s summarise the incremental midpoint algorithm. At each step,
the algorithm chooses between two pixels based on the sign of
the decision variable calculated in the previous iteration; then it
updates the decision variable by adding either ∆E or ∆NE to the
old value, depending on the choice of the pixel. The algorithm
starts from the first endpoint, and the first decision variable is
given by (a+b)/2. Using dstart, we choose the second pixel, and
so on.
To eliminate the fraction in dstart, we redefine our original F by
multiplying it by 2; F(x,y) = 2 (ax+by+c). This multiplies each
constant the decision variable (and the increments ∆E and ∆NE) by
2 but does not affect the sign of the decision variable, which is
all that matters for the midpoint test.
Summary to the midpoint technique
20
(cont.)
Initialisation: dstart = 2 × a +b = 2×dy – dx
where dy = y1 – y0 and dx = x1 – x0 .
Incremental update: 1) if E was chosen, ∆E = 2× dy
dnew = dold + ∆E
2) if NE was chosen, ∆NE = 2× (dy – dx)
dnew = dold + ∆NE
Advantage: The arithmetic needed to evaluate dnew for any step is a
simple integer addition. No time-consuming multiplication is involved.
Further, the incremental update is quite simple, therefore, it is an
efficient algorithm.
Note: the given algorithm only works for those line with slope (0, 1).
Lines: Arbitrary Directions
21
Step through y, Step through y,
decrement x increment x
Step through x- Step through x,
3 2
,increment y increment y
4 1
5 8
Step through x-, Step through x,
increment y 6 7
decrement y
Step through y-, Step through y-,
decrement x increment x
Example
22
• Line end points: 13
(x0 , y0) = (5, 8)
12
(x1 , y1) = (9, 11)
11
Solution:
10
dx = 4
dy = 3 9
dstart=2*dy-dx=2 > 0
NE is chosen 8
∆NE = 2*(dy-dx) = -2 <0 7
d = d + ∆NE = 0
E is chosen 6
4 5 6 7 8 9 10 11
…
Lines: Some Remarks
23
Weaker Intensity of Diagonal Lines.
Consider the two scan-converted lines in
the below figure. The diagonal line, B, has a
slope of 1 and hence is √2 times as long as
A, the horizontal line. Yet the same number
of pixels (9) is drawn to represent each line.
If the intensity of each pixel is I, then the
intensity per unit length of line A is I,
whereas for line B it is only I/√2 .
Digital Differential Analyzer (DDA)
24
Finite
Solution:
25
Example of DDA algorithm
a single DDA for the first quadrant:
26 line from (0,0 ) to (5,5)
Example of DDA algorithm
a single DDA for the first quadrant:
27
line from (0,0) to (−8,−4)
Bresenham Algorithm
28
Basic idea:
29
Example of Bresenham algorithm
30
line from (0,0) to (5,5)
Flowchart of Bresenham
31
32
Conditions for general Bresenham’s
33
algorithm
34
Example of Bresenham
algorithm
35 line from (0,0) to (−8,−4)