lecture 4 graphic primitives, line. features of a simple graphic program

18
Lecture 4 Graphic Primitives, Line

Upload: savannah-goldwyn

Post on 01-Apr-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Lecture 4Graphic Primitives, Line

Page 2: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Features of a simple graphic program

Page 3: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Introduction• How does that program relate to the concepts of

pixels and memory mapping that were introduced in Chapter 2?

• How were the lines drawn?• What exactly did the line:

g.drawLine(50,50,50,150) do;

Page 4: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Drawing straight lines• there are two ways of doing it: • the brute force approach and the elegant way.

We’ll tackle the brute force way first to introduce some ideas and then looks at how it can be refined to produce an efficient (i.e. quick) solution known as Bresenham’s algorithm.

Page 5: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Brute Force• The equation of a straight line:

Page 6: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Brute Force

Page 7: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Brute Force

Page 8: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Brute Force Implementation

Page 9: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Brute Force - Disadvantages• Gaps start to appear in the line. Why has this

happened? Looking at some values for x and y soon reveals that when m>1, the values for y increase by an amount greater than the increments of x that we are using (i.e. >1).

• It requires floating point arithmetic which is slow when compared with using integer only arithmetic.

• An approach which used integers would result in a much quicker algorithm.

Page 10: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Brute Force - Disadvantages

Page 11: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Brute Force - Disadvantages• How to solve the Gap Problem. • use the most rapidly changing variable (x or y)

as the index to the loop. i.e. When the gradient (m) >1 - use y as the control variable in the loop and make x the subject of the equation:

X= (y-c) / m

Page 12: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Bresenham’s algorithm

Page 13: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Bresenham’s algorithm

Page 14: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Bresenham’s algorithm

Page 15: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Bresenham’s algorithm

Page 16: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Bresenham’s algorithm- Implementation

Page 17: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Bresenham’s algorithm- Implementation

Page 18: Lecture 4 Graphic Primitives, Line. Features of a simple graphic program

Exercises