0% found this document useful (0 votes)
6 views

Computer Graphics BCA4001 Programs

The document contains practical file programs for computer graphics and animation, specifically focusing on line and shape drawing algorithms. It includes implementations of DDA line generation, Bresenham's line and circle drawing algorithms, and a midpoint ellipse drawing algorithm using C programming language. Each program initializes a graphics window, performs the drawing operations, and then closes the graphics window after user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Computer Graphics BCA4001 Programs

The document contains practical file programs for computer graphics and animation, specifically focusing on line and shape drawing algorithms. It includes implementations of DDA line generation, Bresenham's line and circle drawing algorithms, and a midpoint ellipse drawing algorithm using C programming language. Each program initializes a graphics window, performs the drawing operations, and then closes the graphics window after user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Graphics and Animation (BCA4001)

Practical File Programs


Program 1: DDA Line Generation
/* Program for DDA Line Generation */
#include <graphics.h>
#include <stdio.h>
#include <math.h>
void main() {
int gd = DETECT, gm;
int x1, y1, x2, y2;
float dx, dy, steps, x, y, k;
initgraph(&gd, &gm, "");
x1 = 100; y1 = 100; x2 = 200; y2 = 200;
dx = x2 - x1;
dy = y2 - y1;
steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
dx = dx / steps;
dy = dy / steps;
x = x1;
y = y1;
for (k = 1; k <= steps; k++) {
putpixel(x, y, WHITE);
x += dx;
y += dy;
}
getch();
closegraph();
}
Program 2: Bresenham Line Drawing Algorithm
/* Bresenham's Line Drawing Algorithm */
#include <graphics.h>
#include <stdio.h>
#include <math.h>
void main() {
int gd = DETECT, gm;
int x1 = 100, y1 = 100, x2 = 200, y2 = 200;
int dx = abs(x2 - x1), dy = abs(y2 - y1);
int p = 2 * dy - dx;
int x = x1, y = y1;
initgraph(&gd, &gm, "");
while (x <= x2) {
putpixel(x, y, WHITE);
x++;
if (p < 0) {
p += 2 * dy;
} else {
y++;
p += 2 * (dy - dx);
}
}
getch();
closegraph();
}
Program 3: Bresenham Circle Drawing Algorithm
/* Bresenham's Circle Drawing Algorithm */
#include <graphics.h>
#include <stdio.h>
#include <math.h>
void drawCircle(int xc, int yc, int x, int y) {
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
putpixel(xc+y, yc+x, WHITE);
putpixel(xc-y, yc+x, WHITE);
putpixel(xc+y, yc-x, WHITE);
putpixel(xc-y, yc-x, WHITE);
}
void main() {
int gd = DETECT, gm;
int x = 0, y = 50;
int d = 3 - 2 * y;
initgraph(&gd, &gm, "");
while (x <= y) {
drawCircle(200, 200, x, y);
x++;
if (d < 0)
d += 4 * x + 6;
else {
y--;
d += 4 * (x - y) + 10;
}
}
getch();
closegraph();
}
Program 4: Midpoint Ellipse Drawing
/* Midpoint Ellipse Drawing */
#include <graphics.h>
#include <math.h>
void drawEllipse(int xc, int yc, int a, int b) {
float dx, dy, d1, d2, x, y;
x = 0;
y = b;
d1 = (b * b) - (a * a * b) + (0.25 * a * a);
dx = 2 * b * b * x;
dy = 2 * a * a * y;
while (dx < dy) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
if (d1 < 0) {
x++;
dx = dx + (2 * b * b);
d1 = d1 + dx + (b * b);
} else {
x++;
y--;
dx = dx + (2 * b * b);
dy = dy - (2 * a * a);
d1 = d1 + dx - dy + (b * b);
}
}
d2 = ((b * b) * ((x + 0.5) * (x + 0.5))) + ((a * a) * ((y - 1) * (y - 1))) - (a * a * b * b);
while (y >= 0) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
if (d2 > 0) {
y--;
dy = dy - (2 * a * a);
d2 = d2 + (a * a) - dy;
} else {
y--;
x++;
dx = dx + (2 * b * b);
dy = dy - (2 * a * a);
d2 = d2 + dx - dy + (a * a);
}
}
}
void main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawEllipse(250, 250, 100, 50);
getch();
closegraph();
}

You might also like