Docsity

Computer Graphics: Primitives and Line Drawing Algorithms - Prof. Zewde, Summaries of Computer Graphics

Partial preview of the text

Download Computer Graphics: Primitives and Line Drawing Algorithms - Prof. Zewde and more Summaries Computer Graphics in PDF only on Docsity!

COMPUTER GRAPHICS

Chapter 2

Graphics Primitives

Bezawit L.

Outline

  • Primitives
  • Point and Line primitives
  • (^) Fill Area primitives
  • OPENGL primitives function: Point, Lines Polygon  Characters

OpenGL Point Drawing Primitives

  • To draw point, we use the pair of functions glBegin … glEnd , using the symbolic constant GL_POINTS to specify how the vertices in between should be interpreted.
  • glBegin(GL_POINTS); glVertex2f(100.0, 200.0); glVertex2f(150.0, 250.0);
  • glEnd();

Line Drawing Algorithms

Lines are a very common primitive and supported by almost all graphics packages. lines form the basis of more complex primitives such as polylines (a connected sequence of straight-line segments) or polygons. There are two line-drawing algorithms DDA algorithm and Bresenham’s algorithm.

DDA Line-Drawing Algorithm

  • Step 3 : Identify the number of steps to put pixel. If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate.
  • if |(dx)| > |(dy)| No_steps = |dx|;
  • else No_steps = |dy|;

DDA Line-Drawing Algorithm

  • Step 4 : Calculate the increment in x coordinate and y coordinate. Xinc = dx / (float) No_steps; Yinc = dy / (float) No_steps;
  • Step 5 : Put the pixel by successfully incrementing x and y coordinates accordingly and complete the drawing of the line. x = x + Xinc; y = y + Yinc; putpixel(Round(x), Round(y));

Exercise

  • Step3: identify number of steps dx>dy= 5> No_steps=5;
  • Step 4: calculate the increment Xinc=dx/No_steps= 5/5= Yinc=dy/No_steps=3/5=0.

Exercise

  • Step 5: Using the values, we can now start to plot line points: S tart with (x0,y0) = (10,10) – colour this pixel Next, (x1,y1) = (10+1,10+0.6) = (11,10.6) – so we colour pixel (11,11) Next, (x2,y2) = (11+1,10.6+0.6) = (12,11.2) – so we colour pixel (12,11) Next, (x3,y3) = (12+1,11.2+0.6) = (13,11.8) – so we colour pixel (13,12) Next, (x4,y4) = (13+1,11.8+0.6) = (14,12.4) – so we colour pixel (14,12) Next, (x5,y5) = (14+1,12.4+0.6) = (15,13) – so we colour pixel (15,13)
  • We have now reached the end-point (xend,yend), so the algorithm terminates.

DDA Advantage and Disadvantage

The DDA algorithm is simple and easy to implement, but it does involve floating point operations to calculate each new point. Floating point operations are time-consuming when compared to integer operations. Line drawing is a very common operation in computer graphics, it would be nice if we could devise a faster algorithm which uses integer operations only.

Bresenham’s Line-Drawing Algorithm

Bresenham’s line-drawing algorithm provides significant improvements in efficiency over the DDA algorithm. The improvements arise from the observation that for any given line, if we know the previous pixel location, we only have a choice of 2 locations for the next pixel.

  • (^) we do not need to compute the actual 5 floating-point location of the ‘true’ line point; we need only make a decision between pixels A and B.

OpenGL Line Functions

  • (^) To draw Line, we use the pair of functions glBegin … glEnd , using the symbolic constant GL_LINES to specify how the vertices in between should be interpreted.
  • glBegin(GL_LINES); glVertex2f(100.0, 200.0); glVertex2f(150.0, 200.0);
  • glEnd()
  • Two other symbolic constants to draw slightly different types of straight-line primitive are: GL_LINE_STRIP and GL_LINE_LOOP.  (^) It draw line segments from (100,200) to (150,200).

OpenGL Line Functions

  • GL_LINES treats the vertices as pairs of end-points. A start-point with no end-point are ignored.
  • glBegin(GL_LINES); glVertex2i(200,100); glVertex2i(50,0); glVertex2i(100,200); glVertex2i(150,0); glVertex2i(0,100); glEnd();

OpenGL Line Functions

  • GL_LINE_LOOP is the same as GL_LINE_STRIP except that the last point is joined to the first one to create a loop.
  • glBegin(GL_LINE_LOOP); glVertex2i(200,100); glVertex2i(50,0); glVertex2i(100,200); glVertex2i(150,0); glVertex2i(0,100);
  • glEnd();

Fill-Area Primitives

The most common type of primitive in 3-D computer graphics is the fill-area primitive. The term fill-area primitive refers to any enclosed boundary that can be filled with a solid colour or pattern. However, fill-area primitives are normally polygons , as they can be filled more efficiently by graphics packages. Polygons are 2-D shapes whose boundary is formed by any number of connected straight-line segments.