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

## Presentation on theme: "Lecture 4 Graphic Primitives, Line. Features of a simple graphic program."— Presentation transcript:

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;

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.

Brute Force The equation of a straight line:

Brute Force

Brute Force Implementation

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.

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

Bresenham’s algorithm

Bresenham’s algorithm- Implementation

Exercises

Download ppt "Lecture 4 Graphic Primitives, Line. Features of a simple graphic program."

Similar presentations