Unit 1: Graphics algorithm implementation
Software Lab-XI (Computer Graphics) notes · PTU syllabus (BSIT605/BSBC605)
On this page
- Unit summary
- Setting up graphics.h
- Basic graphics functions
- Primitive functions for animation
- Line drawing: direct method
- DDA line algorithm
- Bresenham's line algorithm (all slopes)
- Circle drawing: polynomial and trigonometric methods
- Bresenham's (midpoint) circle algorithm
- Bresenham's (midpoint) ellipse algorithm
- Moving a character along a circle
- 2D clipping and windowing
- Practical list
- Key terms
- Quick revision
- 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
- 1Plot the start point (x0, y0)
- 2Compute dx, dy and p = 2dy − dx
- 3If p < 0
Next is (x + 1, y); p += 2dy
- 4Else
Next is (x + 1, y + 1); p += 2dy − 2dx
- 5Repeat until x reaches x1
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;
}Topic 2
Basic graphics 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");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.
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);
}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;
}
}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; }
}
}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);
}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++;
}
}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; }
}
}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);
}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.
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
- Q1.What does initgraph() do?
- Q2.Which function fills a closed area?
- Q3.Why is Bresenham's algorithm faster than DDA?
- Q4.What is the initial decision parameter in Bresenham's circle algorithm?
- Q5.How do you move an object along a circle?
- Q6.What does a region code of 0000 mean?
Long-answer questions
- Q1.Write programs to draw a line using DDA and Bresenham's algorithms.
- Q2.Write a program to draw a circle using Bresenham's algorithm.
- Q3.Write a program to draw an ellipse using the midpoint algorithm.
- 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.
