Line Drawing: Outline

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

CS 430/536 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

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: α1v1 + α2v2 + … + αnvn
•  Length, distance, normalization where v1,v2, …,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, α1v1 + α2v2 + … + αnvn
identity element (0) where αi 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 u1v1 + u2v2 + … + unvn 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
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

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 = (aybz - azby, azbx - axbz, axby - aybx)
–  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 circles/ •  This is expensive and inefficient
dots
•  Since ∆x = 1, yi+1 = yi + ∆y = yi + m
–  Why? Monitor hardware
–  No more multiplication!
15
•  This leads to an incremental algorithm 16
1994 Foley/VanDam/Finer/Huges/Phillips ICG

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
yk+1
§  m = ∞
∆y •  If |slope| is greater than 1
•  Compute corresponding yk
∆y (∆x) = m (1/m) –  Ending point should be above starting
•  xk+1 = xk + ∆x point
∆x
•  yk+1 = yk + ∆y
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/Finer/Huges/Phillips ICG

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 Pre-computed: 20
Pics/Math courtesy of Dave Mount @ UMD-CP
1994 Foley/VanDam/Finer/Huges/Phillips ICG

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

Observe that all of these are integers


and: for points above the line
for points below the line
Now….. 21 22
Pics/Math courtesy of Dave Mount @ UMD-CP
Pics/Math courtesy of Dave Mount @ 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,
Observe: where D is the value of f at the midpoint:
If Q < M, then pick E Q

M
Else pick NE
M
If Q = M,
it doesn t matter 23 24
Pics/Math courtesy of Dave Mount @ UMD-CP Pics/Math courtesy of Dave Mount @ 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
Pics/Math courtesy of Dave Mount @ UMD-CP Pics/Math courtesy of Dave Mount @ 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
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

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
41 direction 42
1994 Foley/VanDam/Finer/Huges/Phillips ICG

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
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

You might also like