Unit 1 of 1 · B.Sc IT Sem 6

Unit 1: Graphics algorithm implementation

Software Lab-XI (Computer Graphics) notes · PTU syllabus (BSIT605/BSBC605)

7 min read12 topics10 exam questions
On this page
  1. Unit summary
  2. Setting up graphics.h
  3. Basic graphics functions
  4. Primitive functions for animation
  5. Line drawing: direct method
  6. DDA line algorithm
  7. Bresenham's line algorithm (all slopes)
  8. Circle drawing: polynomial and trigonometric methods
  9. Bresenham's (midpoint) circle algorithm
  10. Bresenham's (midpoint) ellipse algorithm
  11. Moving a character along a circle
  12. 2D clipping and windowing
  13. Practical list
  14. Key terms
  15. Quick revision
  16. Important questions

Unit summary

This lab implements graphics in C++: basic graphics functions — circle, putpixel, rectangle, arc, ellipse, floodfill and setcolor — simple animation, line drawing with the direct method, DDA and Bresenham, circles with polynomial, trigonometric and Bresenham methods, ellipses with Bresenham's algorithm, moving a character along a circle, and 2D clipping and windowing.

After this unit you can

  • Use the graphics.h library functions
  • Implement line drawing algorithms
  • Implement circle and ellipse drawing algorithms
  • Animate objects and implement clipping and windowing

PTU syllabus topics

  • Basic C++ graphics functions (circle, putpixel, rectangle, arc, ellipse, floodfill, setcolor)
  • primitive functions for animation
  • direct method/DDA/Bresenham's line drawing algorithms
  • circle drawing via polynomial/trigonometric/Bresenham's methods
  • ellipse drawing via Bresenham's algorithm
  • moving a character along a circle
  • 2D clipping and windowing
ProcessBresenham's line drawing (slope < 1)
  1. 1Plot the start point (x0, y0)
  2. 2Compute dx, dy and p = 2dy − dx
  3. 3If p < 0

    Next is (x + 1, y); p += 2dy

  4. 4Else

    Next is (x + 1, y + 1); p += 2dy − 2dx

  5. 5Repeat until x reaches x1
1

Topic 1

Setting up graphics.h

  • graphics.h is the Borland BGI library used in Turbo C++. On modern systems use WinBGIm with Code::Blocks or Dev-C++ (link libbgi, libgdi32, libcomdlg32, libuuid, libole32, liboleaut32), or a DOS emulator running Turbo C++. Every program starts with initgraph and ends with closegraph.
cpp#include <graphics.h>
#include <conio.h>
int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, (char*)"");     // "C:\\TURBOC3\\BGI" in Turbo C++
    // drawing code
    getch();
    closegraph();
    return 0;
}
2

Topic 2

Basic graphics functions

Key termsgraphics.h functions
putpixel(x, y, color)
Plot one pixel
line(x1, y1, x2, y2)
Draw a line
rectangle(left, top, right, bottom)
Draw a rectangle
circle(x, y, r)
Draw a circle
arc(x, y, start, end, r)
Draw an arc between angles
ellipse(x, y, start, end, xr, yr)
Draw an ellipse or elliptical arc
setcolor(c), setfillstyle(pattern, c)
Set drawing and fill colours
floodfill(x, y, border)
Fill an area bounded by the border colour
outtextxy(x, y, text)
Write text
cleardevice(), delay(ms)
Clear screen; pause
cppsetcolor(YELLOW);
circle(150, 150, 60);
setfillstyle(SOLID_FILL, BLUE);
floodfill(150, 150, YELLOW);            // fill inside the yellow circle
setcolor(WHITE);
rectangle(250, 100, 400, 200);
arc(500, 150, 0, 180, 50);
ellipse(320, 320, 0, 360, 100, 50);
putpixel(320, 320, RED);
outtextxy(260, 400, (char*)"Basic shapes");
3

Topic 3

Primitive functions for animation

cpp// Moving car: draw, wait, erase, shift
for (int x = 0; x < getmaxx() - 120; x += 5) {
    setcolor(WHITE);
    rectangle(x, 300, x + 120, 340);          // body
    rectangle(x + 25, 270, x + 95, 300);      // roof
    circle(x + 25, 350, 12);                  // wheels
    circle(x + 95, 350, 12);
    delay(30);
    cleardevice();
}
  • Double buffering with setactivepage and setvisualpage removes flicker in WinBGIm.
4

Topic 4

Line drawing: direct method

cppvoid directLine(int x1, int y1, int x2, int y2) {
    float m = (float)(y2 - y1) / (x2 - x1);   // assumes x1 < x2 and abs(m) <= 1
    float c = y1 - m * x1;
    for (int x = x1; x <= x2; x++) putpixel(x, (int)(m * x + c + 0.5), WHITE);
}
5

Topic 5

DDA line algorithm

cpp#include <cmath>
void ddaLine(int x1, int y1, int x2, int y2) {
    int dx = x2 - x1, dy = y2 - y1;
    int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
    float xinc = dx / (float)steps, yinc = dy / (float)steps;
    float x = x1, y = y1;
    for (int i = 0; i <= steps; i++) {
        putpixel((int)round(x), (int)round(y), GREEN);
        x += xinc; y += yinc;
    }
}
6

Topic 6

Bresenham's line algorithm (all slopes)

cppvoid bresLine(int x1, int y1, int x2, int y2) {
    int dx = abs(x2 - x1), dy = abs(y2 - y1);
    int sx = x1 < x2 ? 1 : -1, sy = y1 < y2 ? 1 : -1;
    int err = dx - dy;
    while (true) {
        putpixel(x1, y1, CYAN);
        if (x1 == x2 && y1 == y2) break;
        int e2 = 2 * err;
        if (e2 > -dy) { err -= dy; x1 += sx; }
        if (e2 <  dx) { err += dx; y1 += sy; }
    }
}
7

Topic 7

Circle drawing: polynomial and trigonometric methods

cppvoid plot8(int xc, int yc, int x, int y, int c) {      // eight-way symmetry
    putpixel(xc + x, yc + y, c); putpixel(xc - x, yc + y, c);
    putpixel(xc + x, yc - y, c); putpixel(xc - x, yc - y, c);
    putpixel(xc + y, yc + x, c); putpixel(xc - y, yc + x, c);
    putpixel(xc + y, yc - x, c); putpixel(xc - y, yc - x, c);
}
void polyCircle(int xc, int yc, int r) {
    for (int x = 0; x <= r / sqrt(2.0); x++)
        plot8(xc, yc, x, (int)round(sqrt((double)r * r - x * x)), WHITE);
}
void trigCircle(int xc, int yc, int r) {
    for (double t = 0; t <= M_PI / 4; t += 1.0 / r)
        plot8(xc, yc, (int)round(r * cos(t)), (int)round(r * sin(t)), YELLOW);
}
8

Topic 8

Bresenham's (midpoint) circle algorithm

cppvoid bresCircle(int xc, int yc, int r) {
    int x = 0, y = r, d = 3 - 2 * r;
    while (x <= y) {
        plot8(xc, yc, x, y, MAGENTA);
        if (d < 0) d += 4 * x + 6;
        else { d += 4 * (x - y) + 10; y--; }
        x++;
    }
}
9

Topic 9

Bresenham's (midpoint) ellipse algorithm

cppvoid plot4(int xc, int yc, int x, int y, int c) {
    putpixel(xc + x, yc + y, c); putpixel(xc - x, yc + y, c);
    putpixel(xc + x, yc - y, c); putpixel(xc - x, yc - y, c);
}
void midEllipse(int xc, int yc, long rx, long ry) {
    long x = 0, y = ry;
    double p1 = ry * ry - rx * rx * ry + 0.25 * rx * rx;
    while (2 * ry * ry * x < 2 * rx * rx * y) {          // region 1
        plot4(xc, yc, x, y, LIGHTGREEN);
        x++;
        if (p1 < 0) p1 += 2 * ry * ry * x + ry * ry;
        else { y--; p1 += 2 * ry * ry * x - 2 * rx * rx * y + ry * ry; }
    }
    double p2 = ry * ry * (x + 0.5) * (x + 0.5) + rx * rx * (y - 1) * (y - 1) - rx * rx * ry * ry;
    while (y >= 0) {                                      // region 2
        plot4(xc, yc, x, y, LIGHTGREEN);
        y--;
        if (p2 > 0) p2 += rx * rx - 2 * rx * rx * y;
        else { x++; p2 += 2 * ry * ry * x - 2 * rx * rx * y + rx * rx; }
    }
}
10

Topic 10

Moving a character along a circle

cpp// A small ball (or the letter A) travels around a circular track
int xc = 320, yc = 240, R = 150;
for (int k = 0; k < 3; k++)                               // three laps
    for (double t = 0; t < 2 * M_PI; t += 0.05) {
        cleardevice();
        setcolor(WHITE); circle(xc, yc, R);              // track
        int x = xc + (int)(R * cos(t)), y = yc + (int)(R * sin(t));
        setcolor(RED); circle(x, y, 10);
        setfillstyle(SOLID_FILL, RED); floodfill(x, y, RED);
        outtextxy(x - 3, y - 25, (char*)"A");
        delay(20);
    }
11

Topic 11

2D clipping and windowing

cpp// Cohen–Sutherland line clipping
const int INSIDE = 0, LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8;
int xmin = 150, ymin = 150, xmax = 450, ymax = 350;      // clipping window

int code(int x, int y) {
    int c = INSIDE;
    if (x < xmin) c |= LEFT;   else if (x > xmax) c |= RIGHT;
    if (y < ymin) c |= BOTTOM; else if (y > ymax) c |= TOP;
    return c;
}
void cohenSutherland(int x1, int y1, int x2, int y2) {
    int c1 = code(x1, y1), c2 = code(x2, y2);
    bool accept = false;
    while (true) {
        if (!(c1 | c2)) { accept = true; break; }        // both inside
        if (c1 & c2) break;                               // both outside same side
        int out = c1 ? c1 : c2; double x, y;
        if (out & TOP)         { x = x1 + (x2 - x1) * (ymax - y1) / (double)(y2 - y1); y = ymax; }
        else if (out & BOTTOM) { x = x1 + (x2 - x1) * (ymin - y1) / (double)(y2 - y1); y = ymin; }
        else if (out & RIGHT)  { y = y1 + (y2 - y1) * (xmax - x1) / (double)(x2 - x1); x = xmax; }
        else                   { y = y1 + (y2 - y1) * (xmin - x1) / (double)(x2 - x1); x = xmin; }
        if (out == c1) { x1 = (int)x; y1 = (int)y; c1 = code(x1, y1); }
        else           { x2 = (int)x; y2 = (int)y; c2 = code(x2, y2); }
    }
    rectangle(xmin, ymin, xmax, ymax);
    if (accept) { setcolor(YELLOW); line(x1, y1, x2, y2); }
}
  • Windowing: map a world-coordinate window to a screen viewport with xv = xvmin + (xw − xwmin) × sx and yv = yvmin + (yw − ywmin) × sy; draw the original and the mapped figure side by side to check the result.
12

Topic 12

Practical list

  • Draw basic shapes with circle, putpixel, rectangle, arc, ellipse, floodfill and setcolor.
  • Simple animations: moving car, bouncing ball, rotating fan blades.
  • Lines with the direct method, DDA and Bresenham's algorithm; compare output for steep and shallow lines.
  • Circles with polynomial, trigonometric and Bresenham's methods.
  • Ellipse with Bresenham's (midpoint) algorithm.
  • Move a character or ball along a circular path.
  • Cohen–Sutherland line clipping; window-to-viewport mapping.

Key terms

BGI
Borland Graphics Interface used by graphics.h
initgraph
Function that initialises graphics mode
floodfill
Function filling an area up to a border colour
Eight-way symmetry
Plotting eight circle points from one computed point
Region code
Bits locating a point relative to the clipping window

Quick revision

  • initgraph, closegraph; WinBGIm on modern compilers.
  • putpixel, line, rectangle, circle, arc, ellipse, setcolor, floodfill, outtextxy.
  • Animation: draw, delay, clear, move.
  • Direct, DDA, Bresenham lines; polynomial, trig, Bresenham circles; midpoint ellipse.
  • Motion along a circle with cos and sin; Cohen–Sutherland; windowing.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.What does initgraph() do?
  2. Q2.Which function fills a closed area?
  3. Q3.Why is Bresenham's algorithm faster than DDA?
  4. Q4.What is the initial decision parameter in Bresenham's circle algorithm?
  5. Q5.How do you move an object along a circle?
  6. Q6.What does a region code of 0000 mean?

Long-answer questions

  1. Q1.Write programs to draw a line using DDA and Bresenham's algorithms.
  2. Q2.Write a program to draw a circle using Bresenham's algorithm.
  3. Q3.Write a program to draw an ellipse using the midpoint algorithm.
  4. Q4.Write a program for Cohen–Sutherland line clipping.

Stuck on this unit?

Message SBS on WhatsApp for help with Software Lab-XI (Computer Graphics), or to ask about studying B.Sc IT at Synetic.

WhatsApp us