Professional Documents
Culture Documents
Line Drawing: Outline
Line Drawing: Outline
Line Drawing: Outline
Computer Graphics I
• Math refresher
Line Drawing • Line drawing
Week 1, Lecture 2 • Digital differential analyzer
David Breen, William Regli and Maxim Peysakhov • Bresenham s algorithm
Geometric and Intelligent Computing Laboratory • XPM file format
Department of Computer Science
Drexel University
http://gicl.cs.drexel.edu
1 2
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
Pics/Math courtesy of Dave Mount @ 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
2
Matrix Transpose & Inverse
• Matrix Transpose:
Swap rows and cols:
• Facts about Line Drawing
the transpose:
3
Bresenham s Algorithm The Algorithm
• 1965 @ IBM
• Basic Idea:
– Only integer
arithmetic
– Incremental
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
dx ,dy are positive other cases symmetric
• Which pixel next?
Then: – E or NE
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
M M
Assumptions:
0 ≤ slope ≤ 1
29 Pre-computed: 30
Pics/Math courtesy of Dave Mount @ UMD-CP Pics/Math courtesy of Dave Mount @ 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
33 34
35 36
6
Bresenham s Algorithm: Bresenham s Algorithm:
Example Example
37 38
39 40
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
8
Programming assignment 1
• Input PostScript-like file
• Output B/W XPM
• Primary I/O formats for the course Questions?
• Create data structure to hold points and lines
in memory (the world model)
• Implement 2D translation, rotation and scaling Go to Assignment 1
of the world model
• Implement line drawing and clipping
• January 20th
• Get started now!
51 52