Professional Documents
Culture Documents
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
3 4
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
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
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
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
M M
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
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
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