CPE 514
Geometric Modeling
MODULE 2
Unit 1- Basic Line drawing
11/14/2024 1
Introduction
The most fundamental graphics primitive you can draw, other than a
point (dot), is a line.
As you will see in this unit, lines are very versatile and form the basis
of a number of other graphics primitives such as polylines and simple
geometrical shapes.
11/14/2024 2
Objectives
By the end of this unit, you should be able to:
1. Know the basic representation of line and line segments.
2. Learn end understand different line drawing algorithms.
3. Learn the mathematics of circle and its use in graphic designs
11/14/2024 3
Representing Straight Lines
A line can be defined by its slope, m,
and its y intercept b
Points on the line satisfy the equation
y = mx + b
A line can be defined by two points
• e.g., (x0, y0), (x1, y1)
11/14/2024 4
Representing Straight Lines
• To convert to the previous representation, substitute the two
points into y = mx + b and determine m and b
(1): y0 = mx0 + b
(2): y1 = mx1 + b
(2) – (1): y1 – y0 = m(x1 – x0)
m = (y1 – y0) / (x1 – x0) if x1 ≠ x0
b = y0 – (y1 – y0) x0 / (x1 – x0)
= (x1y0 – x0y0 – x0y1 + x0y0) / (x1 – x0)
= (x1y0 – x0y1) / (x1 – x0)
11/14/2024 5
Representing Straight Lines
A line can be defined by a point and a
vector.
e.g., (x, y) = (x0, y0) + kv is on the line,
where k is a scalar value
If v is the vector from (x0, y0) to (x1, y1) and k
∈ [0, 1], this is a line segment from (x0, y0) to
(x1, y1)
If the vector v has unit length and k ∈ [0, L],
this is a line segment of length L in the
direction of v
11/14/2024 6
Explicit, Implicit and Parametric
Forms.
Explicit form:
In an explicit form, one variable is expressed as a function
of the other variable(s)
In 2D, y is expressed as an explicit function of x
e.g., line: y = mx + b.
e.g., circle: y = √(R^2 – x^2 )
11/14/2024 7
Explicit, Implicit and Parametric
Forms.
Implicit form:
In an implicit form, points on the shape satisfy an implicit
function
In 2D, a point (x, y) is on the shape if f(x, y) = 0.
e.g., line: f(x,y) = mx + b – y
e.g., circle: f(x,y) = R^2 – x^2 – y^2
11/14/2024 8
Explicit, Implicit and Parametric
Forms.
Parametric form:
In a parametric form, points on the shape are expressed in
terms of a separate parameter (not x or y).
Line segment:
x = (1-t) x0 + t x1 t ∈ [0, 1],
y = (1-t) y0 + t y1
Points on the line are a linear combination of the endpoint
positions (x0, y0) and (x1, y1)
11/14/2024 9
Explicit, Implicit and Parametric
Forms.
Parametric form:
circle:
x = R cosΘ Θ ∈ [0, 2π]
y = R sinΘ
Points on the circle are swept out as Θ ranges from 0 to 2π
11/14/2024 10
Drawing Straight Lines
Parametric form:
A line segment is continuous
All points between the two endpoints belong to the line; a
digital image is discrete
We can only set pixels at discrete locations
Pixels have a finite size
How do we know what pixels between the two endpoints
should be set?
11/14/2024 11
Drawing Straight Lines
Parametric form:
Drawing vertical and horizontal
lines is straightforward
Set pixels nearest the line
endpoints and all the pixels in
between
11/14/2024 12
Line Drawing Algorithms:
Digital Differential Analyzer
(DDA)
Draw the thinnest possible line
with no gaps
Sample the line at equal intervals
Determine the optimal intervals
from the slope of the line
11/14/2024 13
Line Drawing Algorithms:
Digital Differential Analyzer (DDA)
Guarantees the thinnest possible line with no gaps
For lines that are more horizontal than vertical (P0P1):
sample once each time for each column of pixels
between the endpoints
For lines that are more horizontal than vertical (P0P1):
sample once each time for each column of pixels
between the endpoints
11/14/2024 14
Line Drawing Algorithms:
Digital Differential
Analyzer (DDA)
There are 4 cases to
consider
We will consider the first
case (the others are
treated similarly):
Positive slope, m ∈ [0, 1]
11/14/2024 15
Line Drawing Algorithms:
Digital Differential Analyzer (DDA)
For now, consider integer endpoints (x0, y0) and (x1, y1).
For convenience, order the endpoints from left to right
m ∈ [0,1] → These lines are more horizontal than vertical –
Choose the interval between sample points
to sample once for each column between the endpoints→
Produces the thinnest possible line without gaps.
11/14/2024 16
Line Drawing Algorithms:
If the endpoints are at
integer grid locations,
the number of pixels to
be set along the line
(including the
endpoints) is
N + 1, where N = x1 – x0
11/14/2024 17
Line Drawing Algorithms: DDA
Recall the parametric form of the line
x = (1-t) x0 + t x1 t ∈ [0, 1]
y = (1-t) y0 + t y1
Sample the line at t = 0, 1/N, 2/N, … 1
x(i) = x0 + i y(i) = (1 – t) y0 + t y1
Basic algorithm:
N = (x1 – x0)
for(i = 0; i <= N; i++) {
t=i/N
setPixel (x0 + i, round((1 – t) y0 + t y1))
}
Each new pixel requires 1 divide, 3 additions, 2 multiplications and a round
11/14/2024 18
Line Drawing Algorithms:More efficient algorithm
Increment y between sample points
yi = (1 – ti) y0 + ti y1
yi+1 = (1 – ti+1) y0 + ti+1y1
yi+1 – yi = (-ti+1 + ti) y0 + (ti+1 – ti) y1
= (-(i + 1)/N + i/N) y0 + (((i + 1)/N - i/N) y1
= (y1 – y0) / N
= (y1 – y0) / (x1 – x0)
=m
11/14/2024 19
Line Drawing Algorithms:More efficient algorithm
For each sample, x increases by 1 and y increases by m
More efficient algorithm
i = x0
y = y0
m = (y1 – y0) / (x1 – x0)
(while i <= x1) {
setPixel (i, round(y))
i=i+1
y=y+m
}
Each new pixel requires 2 additions and a round
11/14/2024 20
Line Drawing Algorithms:More efficient algorithm
To adapt the algorithm for floating point endpoints, simply
change the initial conditions
The main loop stays the same
i = round(x0)
m = (y1 – y0) / (x1 – x0)
y = y0 + m * (i – x0)
(while i <= x1) {
setPixel (i, round(y))
i=i+1
y=y+m
}
11/14/2024 21
Line Drawing Algorithms:More efficient algorithm
Advantages
Eliminates the multiplications and division required for each
pixel
Limitations
Time consuming round() is still required y and m are both
floats – this is problematic for fixed point processors (e.g.,
modern PDAs and cell phones).
11/14/2024 22
Line Drawing Algorithms:Midpoint Line Algorithm
Extends the DDA to avoid floating point
calculation
Each new pixel requires one or two integer
adds and a sign test
Consider the case where
m ∈ [0,1]
Order the endpoints from left to right
These lines are more horizontal than vertical
m = Δy/Δx ∈ [0,1]
so Δy < Δx between each sample Point
m ∈ [0,1] → the line goes up and to the
right x increases by 1 for each sample along the
line11/14/2024 23
Line Drawing Algorithms:Midpoint Line Algorithm
The next pixel will be in the next
column y increases along the line, but
more slowly than x
For each unit change in x, y will change
by less than one
• Sometimes the next pixel will be on
the same row (to the right)
• Sometimes it will be on the row above
(to the right and up)
11/14/2024 24
Line Drawing Algorithms: Approach
Sample once along the line at every column
At each sample point, determine if the next pixel
is to the right or to the right and up from the
current pixel.
Basic algorithm:
i = x0
j = y0
while (i <= x1) {
setPixel (i, j)
i=i+1
if (Condition) j = j+1
11/14/2024 } 25
Line Drawing Algorithms: Approach
Condition() determines if the next pixel is
to the right or to the right and up.
The key is to determine an efficient
Condition()
Consider a line with slope m ∈ [0, 1]
11/14/2024 26
Line Drawing Algorithms: Approach
Assume that for column i, the pixel (i, j)
was set.
We need to determine
if the next pixel is pixel (i+1, j) or (i+1, j+1)
Consider the midpoint between these
two pixel centers: the point (i+1, j+½)
If the midpoint is above the line, the pixel
(i+1, j) is closer to the line
If the midpoint is below the line, the pixel
(i+1, j+1) is closer to the line.
11/14/2024 27
Line Drawing Algorithms: Approach
• Advance to the next sample point
Recall the basic algorithm
i = x0
j = y0
while (i <= x1) {
setPixel (i, j)
i=i+1
if (Condition) j = j+1
}
Condition() tests whether the midpoint, (i+1, j+½), is above or below
the line
11/14/2024 28
Line Drawing Algorithms: Approach
• We need a mathematical expression for
Condition()
Use the implicit representation for the
line
F(x,y) = mx + b – y
Verify that for positive m, at a given x,
If y is on the line, then F(x, y) = 0
If y is above the line, then F(x,y) < 0
If y is below the line, then F(x,y) > 0
11/14/2024 29
Line Drawing Algorithms: Approach
To determine if the next pixel is (i+1, j) or (i+1, j+1):
Evaluate F(i+1, j + ½)
F(i+1, j + ½) = m(i + 1) + b – (j + ½)
The midpoint test, i.e., the desired Condition (i, j) is
F(i+1, j + ½) > 0
11/14/2024 30
Line Drawing Algorithms: Bresenham’s Algorithm
An efficient form of the midpoint algorithm uses integer
math that is more amenable for fixed point processors
assumes both endpoints are at integer pixel locations uses
incremental arithmetic for each new pixel.
Midpoint test
F(i, j+ ½) > 0
m*i + b – (j+ ½) > 0
• Recall
m = (y1 – y0) / (x1 – x0)
b = (x1y0 – x0y1) / (x1 – x0)
• Multiply through by 2(x1 – x0) to get rid of all Fractions
11/14/2024 31
Line Drawing Algorithms: Bresenham’s Algorithm
1. Use integer math
Multiply both sides of the midpoint test by 2(x1 – x0) to
get rid of all fractions
• As long as x1 > x0, the modified midpoint test is equivalent
to the
midpoint test
Modified midpoint test
2(y1 – y0)i + 2(x1y0 – x0y1) - 2(x1 – x0)j - (x1 – x0) > 0
11/14/2024 32
Line Drawing Algorithms: Bresenham’s Algorithm
2. Use incremental arithmetic
F(i, j) = 2(y1 – y0)i + 2(x1y0 – x0y1) - 2(x1 – x0)j - (x1 – x0)
if F <= 0, the next pixel is (i+1, j)
F(i+1, j) = 2(y1 – y0)(i+1) + 2(x1y0 – x0y1) - 2(x1 – x0)j - (x1 –
x0)
= F(i,j) + 2(y1 – y0)
else (if F > 0), the next pixel is (i+1, j+1)
F(i+1, j+1) = 2(y1 – y0)(i+1) + 2(x1y0 – x0y1) - 2(x1 – x0)(j+1) -
(x1
– x0)
= F(i,j) + 2(y1 – y0) - 2(x1 – x0)
11/14/2024 33
Drawing Circles
A circle is the set of all points that are a distance R from a center point
(cx, cy)
Explicit equation
y = cy + √(R2 - (x – cx)2)
Implicit equations
F(x, y) = R2 - (x – cx)2 - (y – cy)2
Parametric equation
x = cx + R cos Θ Θ ∈ [0, 2π]
y = cy + R sin Θ
11/14/2024 34
11/14/2024 35
The Digital Image
Suppose we wish to ‘capture’ an image and represent it on a
computer e.g. with a scanner or camera (the machine equivalent of
an eye).
Since we do not have infinite storage (bits), it follows that we must
convert that analogue signal into a more limited digital form. We call
this conversion process sampling.
Sampling theory is an important part of Computer Graphics,
underpinning the theory behind both image capture and
manipulation.
11/14/2024 36
Raster Image Representation
The Computer Graphics solution to the problem of image
representation is to break the image (picture) up into a regular grid that
we call a ‘raster’.
Each grid cell is a ‘picture cell’, a term often contracted to pixel.
11/14/2024 37
Raster Image Representation
Rasters are used to represent digital images.
Modern displays use a rectangular raster, comprised of W × H pixels.
The raster illustrated here contains a greyscale image; its contents are
represented in memory by a greyscale frame buffer.
The values stored in the frame buffer record the intensities of the pixels
on a discrete scale (0=black, 255=white).
The pixel is the atomic unit of the image; it is coloured uniformly, its
single colour represents a discrete sample of light e.g. from a captured
image.
11/14/2024 38
Raster Image Representation
In most implementations, rasters take the form of a rectilinear grid often
containing many thousands of pixels.
The raster provides an orthogonal two-dimensional basis with which to
specify pixel coordinates.
By convention, pixels coordinates are zero-indexed and so the origin is
located at the top-left of the image.
Therefore pixel (W − 1,H − 1) is located at the bottom-right corner of a
raster of width W pixels and height H pixels. As a note, some Graphics
applications make use of hexagonal pixels instead 1, however we will not
consider these on the course.
11/14/2024 39
Raster Image Representation
The number of pixels in an image is referred to as the image’s resolution.
Modern desktop displays are capable of visualising images with
resolutions around 1024 × 768 pixels (i.e. a million pixels or one mega-
pixel). Even inexpensive modern cameras and scanners are now capable
of capturing images at resolutions of several mega-pixels.
In general, the greater the resolution, the greater the level of spatial
detail an image can represent.
11/14/2024 40
Hardware Frame Buffers
An image is represented by storing values of the colour of each pixel in a
structured way.
Since the earliest computer Visual Display Units (VDUs) of the 1960s, it
has become common practice to reserve a large, contiguous block of
memory specifically to manipulate the image currently shown on the
computer’s display. This piece of memory is referred to as a frame
buffer.
By reading or writing to this region of memory, we can read or write the
colour values of pixels at particular positions on the display.
11/14/2024 41
Hardware Frame Buffers
Note that the term ‘frame buffer’ as originally defined, strictly refers to
the area of memory reserved for direct manipulation of the currently
displayed image.
In the early days of Graphics, special hardware was needed to store
enough data to represent just that single image. However we may now
manipulate hundreds of images in memory simultaneously and the term
‘frame buffer’ has fallen into informal use to describe any piece of
storage that represents an image.
11/14/2024 42
Hardware Frame Buffers
There are a number of popular formats (i.e. ways of encoding pixels)
within a frame buffer. This is partly because each format has its own
advantages, and partly for reasons of backward compatibility with older
systems (especially on the PC).
Often video hardware can be switched between different video modes,
each of which encodes the frame buffer in a different way.
Three common frame buffer formats are discussed in the subsequent
sections; the greyscale, pseudo-colour, and true-colour formats. These
formats are frequently encountered by people involved in programming
of Graphics, Vision or mainstream Windows GUI programming
11/14/2024 43
Greyscale Frame Buffer
Arguably the simplest form of frame buffer is the greyscale frame buffer;
often mistakenly called ‘black and white’ or ‘monochrome’ frame
buffers. Greyscale buffers encodes pixels using various shades of grey.
In common implementations, pixels are encoded as an unsigned integer
using 8 bits (1 byte) and so can represent 2^8 = 256 different shades of
grey. Usually black is represented by value 0, and white by value 255. A
mid-intensity grey pixel has value 128.
Consequently an image of width W pixels and height H pixels requires W
×H bytes of memory for its frame buffer.
11/14/2024 44
Greyscale Frame Buffer
The frame buffer is arranged so that the first byte of memory
corresponds to the pixel at coordinates (0, 0).
Recall that this is the top-left corner of the image. Addressing then
proceeds in a left-right, then top-down manner (see Figure). So, the
value (grey level) of pixel (1, 0) is stored in the second byte of memory,
pixel (0, 1) is stored in the (W + 1)th byte, and so on. Pixel (x, y) would
be stored at buffer offset A
where: A = x +Wy (2.1) i.e. A bytes from the start of the frame buffer.
Sometimes, scan line is referred to as a full row of pixels.
A scan-line is therefore W pixels wide.
11/14/2024 45
Greyscale Frame Buffer
Old machines, such as the ZX Spectrum, required more CPU time to
iterate through each location in the frame buffer than it took for the
video hardware to refresh the screen.
In an animation, this would cause undesirable flicker due to partially
drawn frames. To compensate, byte range [0, (W − 1)] in the buffer
wrote to the first scan-line, as usual. However bytes [2W, (3W −1)] wrote
to a scan-line one third of the way down the display, and [3W, (4W −1)]
to a scan-line two thirds down. This interleaving did complicate
Graphics programming, but prevented visual artifacts that would
otherwise occur due to slow memory access speeds.
11/14/2024 46
Pseudo-colour Frame Buffer
The pseudo-colour frame buffer allows representation of colour images.
The storage scheme is identical to the greyscale frame buffer.
However the pixel values do not represent shades of grey. Instead each
possible value (0 − 255) represents a particular colour; more specifically,
an index into a list of 256 different colours maintained by the video
hardware.
The colours themselves are stored in a “Colour Lookup Table” (CLUT)
which is essentially a map < colourindex, colour > i.e. a table indexed
with an integer key (0−255) storing a value that represents colour.
In alternative terminology the CLUT is sometimes called a palette.
11/14/2024 47
Pseudo-colour Frame Buffer
Many common colours can be produced by adding together (mixing)
varying quantities of Red, Green and Blue light.
For example, Red and Green light mix to produce Yellow light. Therefore
the value stored in the CLUT for each colour is a triple (R, G,B) denoting
the quantity (intensity) of Red, Green and Blue light in the mix. Each
element of the triple is 8 bit i.e. has range (0 − 255) in common
implementations.
The earliest colour displays employed pseudo-colour frame buffers. This
is because memory was expensive and colour images could be
represented at identical cost to grayscale images (plus a small storage
overhead for the CLUT).
11/14/2024 48
Pseudo-colour Frame Buffer
The obvious disadvantage of a pseudocolour frame buffer is that only a
limited number of colours may be displayed at any one time (i.e. 256
colours). However the colour range (we say gamut) of the display is 2^8
× 2^8 × 2^8 = 2^24 = 16, 777, 216 colours.
Pseudo-colour frame buffers can still be found in many common
platforms e.g. both MS and X Windows (for convenience, backward
compatibility etc.) and in resource constrained computing domains (e.g.
low-spec games consoles, mobiles).
11/14/2024 49
Pseudo-colour Frame Buffer
Consider an image filled with an expanses of colour index 1 (we might set
CLUT < 1,Blue >, to create a blue ocean). We could sprinkle consecutive
runs of pixels with index ‘2,3,4,5’ sporadically throughout the image. The
CLUT could be set to increasing, lighter shades of Blue at those indices. This
might give the appearance of waves.
The colour values in the CLUT at indices 2,3,4,5 could be rotated
successively, so changing the displayed colours and causing the waves to
animate/ripple (but without the CPU overhead of writing to multiple
locations in the frame buffer).
Effects like this were regularly used in many ’80s and early ’90s computer
games, where computational expense prohibited updating the frame buffer
directly for incidental animations.
11/14/2024 50
True-Colour Frame Buffer
The true-colour frame-buffer also represents colour images, but does
not use a CLUT.
The RGB colour value for each pixel is stored directly within the frame
buffer. So, if we use 8 bits to represent each Red, Green and Blue
component, it requires 24 bits (3 bytes) of storage per pixel. Compare
with the other types of frame buffer, pixels are stored in left-right, then
top-bottom order.
So in our 24 bit colour example, pixel (0, 0) would be stored at buffer
locations 0, 1 and 2. Pixel (1, 0) at 3, 4, and 5; and so on.
Pixel (x, y) would be stored at offset A
11/14/2024 51
True-Colour Frame Buffer
where: S = 3W
A = 3x + Sy
where S is sometimes referred to as the stride of the display.
The advantages of the true-colour buffer complement the disadvantages of
the pseudo-colour buffer We can represent all 16 million colours at once in
an image (given a large enough image!), but true-colour frame image takes 3
times as much storage than the pseudo-colour buffer.
The image would also take longer to update (3 times as many memory
writes) which should be taken under consideration on resource constrained
platforms (e.g. if writing a video codec on a mobile phone).
11/14/2024 52
Alternative forms of true-colour
buffer
The true colour buffer, as described, uses 24 bits to represent RGB
colour. The usual convention is to write the R, G, and B values in order
for each pixel.
Sometime image formats (e.g. Windows Bitmap) write colours in order
B, G, R. This is primarily due to the little-endian hardware architecture of
PCs, which run Windows.
These formats are sometimes referred to as RGB888 or BGR888
respectively.
11/14/2024 53