Docsity

Computer Graphics I: Line Drawing - Lecture Notes from CS 430/536 at Drexel University - P, Lab Reports of Computer Graphics

Toggle sidebar
bg1
1
1
CS 430/536
Computer Graphics I
Line Drawing
Week 1, Lecture 2
David Breen, William Regli and Maxim Peysakhov
Geometric and Intelligent Computing Laboratory
Department of Computer Science
Drexel University
http://gicl.cs.drexel.edu
2
Outline
• Math refresher
• Line drawing
• Digital differential analyzer
• Bresenham’s algorithm
• XPM file format
3
Geometric Preliminaries
• Affine Geometry
– Scalars + Points + Vectors and their ops
• Euclidian Geometry
– Affine Geometry lacks angles, distance
– New op: Inner/Dot product, which gives
• Length, distance, normalization
• Angle, Orthogonality, Orthogonal projection
• Projective Geometry
4
Affine Geometry
• Affine Operations:
• Affine Combinations: α1v1 + α2v2 + … + αnvn
where v1,v2, …,vn are vectors and
Example:
5
Mathematical Preliminaries
• Vector: an n-tuple of real numbers
• Vector Operations
– Vector addition: u + v = w
• Commutative,
associative,
identity element (0)
– Scalar multiplication: cv
• Note: Vectors and Points are different
– Can not add points
– Can find the vector between two points
6
Linear Combinations &
Dot Products
• A linear combination of the vectors
v1, v2, … vn
is any vector of the form
α
1v1 +
α
2v2 + … +
α
nvn
where
α
i is a real number (i.e. a scalar)
• Dot Product:
a real value u1v1 + u2v2 + … + unvn written as
vu •
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Computer Graphics I: Line Drawing - Lecture Notes from CS 430/536 at Drexel University - P and more Lab Reports Computer Graphics in PDF only on Docsity!

1

CS 430/

Computer Graphics I

Line Drawing

Week 1, Lecture 2 David Breen, William Regli and Maxim Peysakhov Geometric and Intelligent Computing Laboratory Department of Computer Science Drexel University http://gicl.cs.drexel.edu 2

Outline

  • Math refresher
  • Line drawing
  • Digital differential analyzer
  • Bresenham’s algorithm
  • XPM file format 3

Geometric Preliminaries

  • Affine Geometry
    • Scalars + Points + Vectors and their ops
  • Euclidian Geometry
    • Affine Geometry lacks angles, distance
    • New op: Inner/Dot product, which gives
      • Length, distance, normalization
      • Angle, Orthogonality, Orthogonal projection
  • Projective Geometry 4

Affine Geometry

  • Affine Operations :
  • Affine Combinations : α 1 v 1 + α 2 v 2 + … + α nvn where v 1 , v 2 , …, vn are vectors and Example: 5

Mathematical Preliminaries

  • Vector: an n -tuple of real numbers
  • Vector Operations
    • Vector addition: u + v = w
      • Commutative, associative, identity element (0)
    • Scalar multiplication: c v
  • Note: Vectors and Points are different
    • Can not add points
    • Can find the vector between two points 6

Linear Combinations &

Dot Products

  • A linear combination of the vectors v 1 , v 2 , … vn is any vector of the form α 1 v 1 + α 2 v 2 + … + α nvn where α i is a real number (i.e. a scalar)
  • Dot Product : a real value u 1 v 1 + u 2 v 2 + … + unvn written as u^ • v

7

Fun with Dot Products

  • Euclidian Distance from (x,y) to (0,0) in general: which is just:
  • This is also the length of vector v: ||v|| or |v|
  • Normalization of a vector:
  • Orthogonal vectors:

x^2 + y^2 x 1^^2 + x 22 +...^ + xn^2

x •

x

8

Projections & Angles

  • Angle between vectors,
  • Projection of vectors Pics/Math courtesy of Dave Mount @ UMD-CP

u^  ⋅ v  = uv  cos( θ) 9

Matrices and Matrix Operators

  • A n -dimensional vector:
  • Matrix Operations:
    • Addition/Subtraction
    • Identity
    • Multiplication • Scalar
      • Matrix Multiplication
  • Implementation issue: Where does the index start? (0 or 1, it’s up to you…) 10

Matrix Multiplication

• [C] = [A][B]

  • Sum over rows & columns
  • Recall: matrix multiplication is not commutative
  • Identity Matrix : 1s on diagonal 0s everywhere else 11

Matrix Determinants

  • A single real number
  • Computed recursively
  • Example:
  • Uses:
    • Find vector ortho to two other vectors
    • Determine the plane of a polygon ba^ d^ c = ad −^ bc     det^ 

det( A ) = Ai , j (− 1 ) i +^ j^ Mi , j

j = 1 n

12

Cross Product

  • Given two non-parallel vectors, A and B
  • A x B calculates third vector C that is

orthogonal to A and B

  • A x B = (aybz - azby, azbx - axbz, axby - aybx)

A × B =

x

y

z

ax ay az

bx by bz

19

Bresenham’s Algorithm

• 1965 @ IBM

  • Basic Idea:
    • Only integer arithmetic
    • Incremental
  • Consider the implicit equation for a line: 1994 Foley/VanDam/Finer/Huges/Phillips ICG 20

Bresenham’s Algorithm

Given: implicit line equation: Let: where r and q are points on the line and dx ,dy are positive Then: Observe that all of these are integers and: for points above the line for points below the line Now….. (^) Pics/Math courtesy of Dave Mount @ UMD-CP 21

Bresenham’s Algorithm

  • Suppose we just finished - (assume 0 ≤ slope ≤ 1) other cases symmetric
  • Which pixel next?
    • E or NE Pics/Math courtesy of Dave Mount @ UMD-CP M Q 22

Assume:

  • Q = exact y value at
  • y midway between E and NE:

Observe:

If , then pick E

Else pick NE

If ,

it doesn’t matter

Bresenham’s Algorithm

Pics/Math courtesy of Dave Mount @ UMD-CP

M

Q < M

Q = M

M Q 23

  • Create “modified” implicit function (2x)
  • Create a decision variable D to select,

where D is the value of f at the

midpoint:

Bresenham’s Algorithm

Pics/Math courtesy of Dave Mount @ UMD-CP^24

Bresenham’s Algorithm

  • If then M is below the line
    • NE is the closest pixel
  • If then M is above the line
    • E is the closest pixel
  • Note: because we multiplied by 2x, D is

now an integer---which is very good news

  • How do we make this incremental??

25

Case I: When E is next

  • What increment for computing a new D?
  • Next midpoint is:
  • Hence, increment by: Pics/Math courtesy of Dave Mount @ UMD-CP^26

Case II: When NE is next

  • What increment for computing a new D?
  • Next midpoint is:
  • Hence, increment by: Pics/Math courtesy of Dave Mount @ UMD-CP 27

How to get an initial value for D?

  • Suppose we start at:
  • Initial midpoint is:

Then:

Pics/Math courtesy of Dave Mount @ UMD-CP^28

The Algorithm

Assumptions: 0 ≤ slope ≤ 1 Pre-computed: (^) Pics/Math courtesy of Dave Mount @ UMD-CP 29

Generalize Algorithm

  • If qx > rx , swap points
  • If slope > 1, always increment y,

conditionally increment x

  • If -1 < slope < 0, always increment x,

conditionally decrement y

  • If slope < -1, always decrement y,

conditionally increment x

30

Bresenham’s Algorithm:

Example

37

Bresenham’s Algorithm:

Example

38

Bresenham’s Algorithm:

Example

39

Some issues with

Bresenham’s Algorithms

  • Pixel ‘density’ varies based on slope
    • straight lines look darker, more pixels per unit length
    • Endpoint order
    • Line from P1 to P should match P2 to P
    • Always choose E when hitting M , regardless of direction 1994 Foley/VanDam/Finer/Huges/Phillips ICG 40

Some issues with

Bresenham’s Algorithms

  • How to handle the line when it hits the clip window?
  • Vertical intersections
    • Could change line slope
    • Need to change init cond.
  • Horizontal intersections
    • Again, changes in the boundary conditions
    • Can’t just intersect the line w/ the box 1994 Foley/VanDam/Finer/Huges/Phillips ICG 41

XPM Format

  • Encoded pixels
  • C code
    • ASCII Text file
    • Viewable on Unix w/ display
    • On Windows with IrfanVIew
    • Translate w/ convert^42

XPM Basics

  • X P ixel M ap (XPM)
  • Native file format in X Windows
  • Color cursor and icon bitmaps
  • Files are actually C source code
  • Read by compiler instead of viewer
  • Successor of X BitM ap (XBM) B-W format

43

XPM Supports Color

44

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

XPM: Specifying Color

Color Name RGB Color black # 00 00 00 white **# ff ff ff

80 80 80**

red # ff 00 00 green # 00 ff 00 blue # 00 00 ff 46

XPM Example

  • Array of C strings
  • The XPM format assumes the origin (0,0) is in the upper left- hand corner.
  • First string is “width height ncolors cpp”
  • Then you have "ncolors" strings associating characters with colors.
  • And last you have "height" strings of "width" * "chars_per_pixel" characters 47

Arrangements for

Presentations

  • Grad students pick dates
  • E-mail me your preference
  • Priority - first come, first served 48

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!