You are on page 1of 9

CS 430 Outline

Computer Graphics
• Math refresher
Line Drawing • Line drawing
Week 1, Lecture 2 • Digital differential analyzer
David Breen, William Regli and Maxim Peysakhov • Bresenham’s algorithm
Department of Computer Science • XPM file format
Drexel University

1 2

Geometric Preliminaries Affine Geometry


• Affine Geometry • Affine Operations:
– Scalars + Points + Vectors and their ops
• Euclidian Geometry
– Affine Geometry lacks angles, distance
– New op: Inner/Dot product, which gives • Affine Combinations: a 1v1 + a 2v 2 + … + a nv n
• Length, distance, normalization where v 1,v 2, …,vn are vectors and
• Angle, Orthogonality, Orthogonal projection Example:
• Projective Geometry

3 4

Linear Combinations &


Mathematical Preliminaries
Dot Products
• Vector: an n-tuple of real numbers • A linear combination of the vectors
• Vector Operations v1 , v2 , … vn
– Vector addition: u + v = w is any vector of the form
• Commutative,
associative,
a1 v1 + a2 v2 + … + an vn
identity element (0) where ai is a real number (i.e. a scalar)
– Scalar multiplication: cv
• Dot Product:
• Note: Vectors and Points are different
– Can not add points
– Can find the vector between two points

a real value u1v 1 + u2v 2 + … + unv n written as u•v


5 6

1
Fun with Dot Products Projections & Angles
• Euclidian Distance from (x,y) to (0,0) • Angle between vectors,
  
u ⋅ v = u v cos(θ )
in general: 2 2 2
x2 + y2 x1 + x2 + ... + xn
which is just: x • x
• Projection of vectors €
• This is also the length of vector v:
||v|| or |v|
• Normalization
€ of a vector:
• Orthogonal vectors:

7 8
Pi c s /Ma th co urtes y o f Da ve Mo u nt @UMD-CP

Matrix Multiplication
Matrices and Matrix Operators
• [C] = [A][B]
• A n-dimensional vector: • Sum over rows & columns
• Recall: matrix multiplication
• Matrix Operations:
– Addition/Subtraction
is not commutative
– Identity
– Multiplication
• Identity Matrix:


Scalar
Matrix Multiplication
1s on diagonal
• Implementation issue: 0s everywhere else
Where does the index start?
(0 or 1, it’s up to you…)

9 10

Matrix Determinants Cross Product


• A single real number n • Given two non-parallel vectors, A and B
• Computed recursively det(A) = ∑ Ai, j (−1) M i, j
i+ j
• A x B calculates third vector C that is
• Example: 'a c $ j=1
det % " = ad − bc
orthogonal to A and B
&b d #
• Uses: • A x B = (a y b z - a z b y, a zb x - a x b z, a xby - a y bx )
– Find vector ortho to two other vectors
€   
– Determine the plane of a polygon x y z
A × B = ax ay az
bx by bz
11 12

2
Matrix Transpose & Inverse
• Matrix Transpose:
Swap rows and cols:
• Facts about Line Drawing
the transpose:

• Matrix Inverse: Given A, find B such that


AB = BA = I BèA-1
(only defined for square matrices)
13 14

Scan-Conversion Algorithms Drawing a Line


• Scan-Conversion:
• y = mx + B
Computing pixel • m = ∆y / ∆x
coordinates for • Start at leftmost x and increment by 1
ideal line on
2D raster grid ➙ ∆x = 1
• Pixels best • yi = Round(mxi + B)
visualized as • This is expensive and inefficient
circles/dots • Since ∆x = 1, yi+1 = yi + ∆y = yi + m
– Why? Monitor hardware
– No more multiplication!
• This leads to an incremental algorithm
15 16
1994 Foley/ VanDam / Fi ner / H uges/ Ph il li ps I CG

Digital Differential Analyzer


Generalizing DDA
(DDA)
• If |slope| is less then 1
§ ∆x = 1
• If |slope| is less than or equal to 1
§ else ∆y = 1 – Ending point should be right of starting
• Check for vertical line point
§ m= ∞ yk+1
∆y • If |slope| is greater than 1
• Compute corresponding yk
∆y (∆x) = m (1/m) – Ending point should be above starting
• x k+1 = x k + ∆x point
• y k+1 = y k + ∆y ∆x
xk xk+1 • Vertical line is a special case
• Round (x,y) for pixel location
• Issue: Would like to avoid ∆x = 0
floating point operations 17 18
1994 Foley/ VanDam / Fi ner / H uges/ Ph il li ps I CG

3
Bresenham’s Algorithm The Algorithm
• 1965 @ IBM
• Basic Idea:
– Only integer
arithmetic
– Incremental

• Consider the implicit


equation for a line: Assumptions:
0 ≤ slope ≤ 1
19 20
1994 Foley/ VanDam / Fi ner / H uges/ Ph il li ps I CG Pi c s /Ma th co urtes y o f Da ve Mo u nt @UMD-CP

Bresenham’s Algorithm
Bresenham’s Algorithm
Given:
implicit line equation: • Suppose we just
Let: finished
where r and q are points on the line and – (assume 0 ≤ slope ≤ 1) Q
M
other cases symmetric
dx ,dy are positive
• Which pixel next?
Then: – E or NE
Observe that all of these are integers
and: for points above the line
for points below the line
Now….. 21 22
Pi c s /Ma th co urtes y o f Da ve Mo u nt @UMD-CP
Pi c s /Ma th co urtes y o f Da ve Mo u nt @UMD-CP

Bresenham’s Algorithm Bresenham’s Algorithm


Assume: • Create “modified” implicit function (2x)
• Q = exact y value at
• y midway between E and NE: M • Create a decision variable D to select,
where D is the value of f at the midpoint:
Observe:
If Q < M, then pick E Q
M
Else pick NE
If Q = M, M

it doesn’t matter 23 24
Pi c s /Ma th co urtes y o f Da ve Mo u nt @UMD-CP Pi c s /Ma th co urtes y o f Da ve Mo u nt @UMD-CP

4
Bresenham’s Algorithm Bresenham’s Algorithm
• If then M is below the line • If then M is below the line
– NE is the closest pixel – NE is the closest pixel
• If then M is above the line • If then M is above the line
– E is the closest pixel – E is the closest pixel

• Note: because we multiplied by 2x, D is


M now an integer---which is very good news
• How do we make this incremental??
25 26

Case I: When E is next Case II: When NE is next


• What increment for computing a new D? • What increment for computing a new D?
• Next midpoint is: • Next midpoint is:

M M

• Hence, increment by: • Hence, increment by:


27 28
Pi c s /Ma th co urtes y o f Da ve Mo u nt @UMD-CP Pi c s /Ma th co urtes y o f Da ve Mo u nt @UMD-CP

How to get an initial value for D? The Algorithm


• Suppose we start at:
• Initial midpoint is:
Then:

Assumptions:
0 ≤ slope ≤ 1
29 Pre-computed: 30
Pi c s /Ma th co urtes y o f Da ve Mo u nt @UMD-CP Pi c s /Ma th co urtes y o f Da ve Mo u nt @UMD-CP

5
Generalize Algorithm Generalize Algorithm
• If qx > rx, swap points • Reflect line into first case
• If slope > 1, always increment y, • Calculate pixels
conditionally increment x • Reflect pixels back into original
• If -1 <= slope < 0, always increment x, orientation
conditionally decrement y
• If slope < -1, always decrement y,
conditionally increment x
• Rework D increments
31 32

Bresenham’s Algorithm: Bresenham’s Algorithm:


Example Example

33 34

Bresenham’s Algorithm: Bresenham’s Algorithm:


Example Example

35 36

6
Bresenham’s Algorithm: Bresenham’s Algorithm:
Example Example

37 38

Bresenham’s Algorithm: Bresenham’s Algorithm:


Example Example

39 40

Bresenham’s Algorithm: Some issues with


Example Bresenham’s Algorithms
• Pixel ‘density’ varies
based on slope
– straight lines look
darker, more pixels per
unit length
– Endpoint order
– Line from P1 to P2
should match P2 to P1
– Always choose E when
hitting M, regardless of
direction
41 42
1994 Foley/ VanDam / Fi ner / H uges/ Ph il li ps I CG

7
XPM Format XPM Basics
• Encoded pixels • X PixelMap (XPM)
• C code • Native file format in X Windows
• Color cursor and icon bitmaps
• ASCII Text file • Files are actually C source code
• Viewable on Unix • Read by compiler instead of viewer
w/ display • Successor of X BitMap (XBM) B-W format
• On Windows with
IrfanVIew
• Translate w/
convert 44 45

XPM Supports Color XPM: Defining Grayscales


and Colors
• Each pixel specified by an ASCII char
• key describes the context this color should be
used within. You can always use “c” for
“color”.
• Colors can be specified:
– color name
– “#” followed by the RGB code in hexadecimal
• RGB – 24 bits (2 characters ‘0’ - ‘f’) for each
color.
46 47

XPM: Specifying Color XPM Example


• Array of C strings
Color Name RGB Color
• The XPM format assumes the
black # 00 00 00 origin (0,0) is in the upper left-
hand corner.
white # ff ff ff • First string is “width height
ncolors cpp”
# 80 80 80 • Then you have "ncolors" strings
associating characters with
red # ff 00 00 colors.

green # 00 ff 00 • And last you have "height"


strings of "width" *
"chars_per_pixel" characters
blue # 00 00 ff
48 49

8
Plain PBM Image files Programming assignment 1
• Input PostScript-like file
• Output B/W XPM
• Primary I/O formats for the course
• Create data structure to hold points and lines
in memory (the world model)
• Implement 2D translation, rotation and
scaling of the world model
• Implement line drawing and clipping
• Due October 8th
• Get started now!
50 52

Questions?

Go to Assignment 1

53

You might also like