C481 B581 Computer Graphics
Dana Vrajitoru

Introduction to 2D Graphics


Coordinate Systems

Basis

Frame

Change of Coordinate Systems

Projection


Scan conversion

Def. The operation of converting an object from the continuous mathematical space into the discrete space of pixels.

[0,a]x[0,b] -> {0,1, ..., n}x{0, 1, ..., m}

Point scanning: (x,y) -> (round(x), round(y)),

round(w) = int(w + 0.5), closest integer to the real number
Continuous image
Discrete image

Line scanning

General equation:  y = mx + b, m is the slope of the line.
Horizontal lines:    y=b    (m = 0)
Vertical lines:        x = c    (m = oo)
Diagonal:

x = y + b    (m = 1)
x = y + b    (m = -1)
Line segment: defined by 2 points: (x1, y1) to (x2, y2).
m = (y2 - y1) / (x2 - x1)
b = y1 - m x1
Line Properties

Direct Conversion Pick the coordinate with the longest projection of the segment

if m < 1 : horizontal
if m > 1 : vertical
Horizontal case: if x1 < x2
compute m = (y2 - y1) / (x2 - x1)
for each x, x1 <= x <= x2
compute y = round (m x + b)
draw pixel (x,y)
Vertical case: switch x and y.
x1=1, y1 = 2, x2 = 9, y2 = 3
m =3

Bresenham's algorithm

For the case x1 < x2, 0 < m < 1.

x = x1; y = y1;
dx = x2 - x1; dy = y2 - y1;
dT = 2 (dy - dx); dS = 2dy;
d = 2dy - dx;
setPixel(x, y);
while (x<x2) {
  x++;
  if (d < 0)
    d = d + dS;
  else {
    y++;
    d = d + dT;
  }
  setPixel(x, y);
}


Surface filling

Region: an area enclosed by a boundary in which all of the pixels are connected.
Connectivity modes: 4-connected or 8-connected.

An algorithm for a 4-connected area.

FloodFill (x0, y0, fill_color, original_color)
{
  int color, x, y;
  x = x0; y = y0;
  getPixel(x, y, color);
  if (color == original_color) {
    while (color == original_color) {
      setPixel(x, y, fill_color);
      FloodFill(x, y+1, fill_color, original_color);
      FloodFill(x, y-1, fill_color, original_color);
      x = x - 1;
      getPixel(x, y, color);
    }
    x = x0 + 1; y = y0;
    getPixel(x, y, color);
    while (color == original_color) {
      setPixel(x, y, fill_color);
      FloodFill(x, y+1, fill_color, original_color);
      FloodFill(x, y-1, fill_color, original_color);
      x = x + 1;
      getPixel(x, y, color);
    }
  }
}


Anti-aliasing

The setPixel function:
 
     Original color c0

     New color    c1

     0.67*c0+0.33*c1

     0.67*c1+0.33*c0

Flood-fill with anti-aliasing:

  • replace the check for
  • with do not use anti-aliasing for setPixel while flood-filling a region.