0% found this document useful (0 votes)
17 views22 pages

Lab 2

The document contains a series of programming exercises focused on graphics algorithms using C. It includes source code examples for drawing lines, circles, ellipses, and implementing various filling algorithms. Each section provides the source code along with a brief description of the algorithm being implemented.
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)
17 views22 pages

Lab 2

The document contains a series of programming exercises focused on graphics algorithms using C. It includes source code examples for drawing lines, circles, ellipses, and implementing various filling algorithms. Each section provides the source code along with a brief description of the algorithm being implemented.
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/ 22

Prepared by SHREE KRISHNA KAFLE/ www.unibytes.

xyz

LAB 1

1. Write a program to draw a line by using build in function line()


Source code:
#include <stdio.h>

#include <graphics.h>

#include <conio.h>

#include <dos.h>

int main()

int gd = DELECT, gm ;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI"); line(100, 100, 200, 200);

getch();

Output:

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

2. Write a program to implement DDA algorithm for drawing a line segment between
two given end points A(x1, x2) and B(x2, y2).
Source code:
#include <stdio.h>
#include <graphics.h>
#include <math.h>
float round(float a);
int main(){
int gd = DETECT, gm;
int x1, y1, x2, y2, steps, k;
float xincr, yincr, x, y, dx, dy;
printf("enter x1,y1");
scanf("%d%d", &x1, &y1);
printf("enter x2,y2");
scanf("%d%d", &x2, &y2);
initgraph(&gd, &gm, "c:\\turboc3\\BGI");
dx = x2 - x1; dy = y2 - y1;
if (abs(dx) > bs(dy))steps =abs(dx);
else{
steps = abs(dy);
xincr = dx / float)steps;
yincr = dy /(float)steps; x = x1;
y = y1;
}
for (k = 1; k <= steps; k++)
{
delay(100); y += yincr;
putpixel(round(x), round(y), WHITE);
}
outtextxy(200, 20, "DDA"); outtextxy(x1 + 5, y1 - 5, (x1,y1)");
outtextxy(x2 + 5, y2 + 5, "(x2,y2)");
getch();
closegraph();
}

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

float round(float a)
{
int b = (int)(a + 0.5); return b;
}

Output:

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

3. To implement Bresenham's line drawing algorithm for drawing a line segment


between two given endpoints A (x1, y2) and B (x2, y2).
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.>
int main() {
int x, y, x1, y1, x2, y2, p, dx, dy;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
printf("\nEnter the x-coordinate of the first point::");
scanf("%d", &x1);
printf("\nEnter the y-coordinate of the first point::");
scanf("%d", &y1);
printf("\nEnter the x-coordinate of the second point::");
scanf("%d", &x2);
printf("\nEnter the y-coordinate of the second point::");
scanf("%d", &y2);
x = x1; y = y1;
dx = x2 - x1; dy = y2 - y1;
putpixel(x, y, 2);
p = (2 * dy - dx);
while (x <= x2)
{
if (p < 0)
{
x = x + 1;
p = p + 2 * dy;
} else
{
x = x + 1; y = y + 1;
p = p + (2 * dy) - (2 * dx);
}
putpixel(x, y, 7);

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

}
getch();
closegraph();
return 0;
}

Output:

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

4. To implement midpoint circle generation algorithm or bresenham's circle algorithm


for drawing a circle of given center (x, y) and radius r.
Source Code:
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.>
void draw_circle(int, int, int);
void symmetry(int, int, int, int);
int main() {
int xc, yc, R;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
printf("Enter the center of the circle:\n");
printf("Xc =");
scanf("%d", &xc);
printf("Yc =");
scanf("%d", &yc);
printf("Enter the radius of the circle:");
scanf("%d", &R);
draw_circle(xc, yc, R);
getch();
closegraph();
return 0; }
void draw_circle(int xc, int yc, int rad) {
int x = 0;
int y = rad;
int p = 1 - rad;
symmetry(x, y, xc,yc);
for(x=0;y>x;x++) {
if (p < 0)
p += 2 * x + 3;
else {
p += 2 * (x - y) + 5;

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

y--; }
symmetry(x, y, xc, yc);
delay(50); }
}
void symmetry(int x, int y, int xc, int yc){
putpixel(xc + x, yc - y, WHITE);
delay(50);
putpixel(xc + y, yc - x, WHITE);
delay(50);
putpixel(xc + y, yc + x, WHITE);
delay(50);
putpixel(xc + x, yc + y, WHITE);
delay(50);
putpixel(xc - x, yc + y, WHITE);
delay(50);
putpixel(xc - y, yc + x, WHITE);
delay(50);
putpixel(xc - y, yc - x, WHITE);
delay(50);
putpixel(xc - x, yc - y, WHITE);
delay(50);
}

Output:

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

5. To implement the Ellipse Generation Algorithm for drawing an ellipse of given


center (x,y) and radius rx and ry.
Source Code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void disp();
float x, y;
int xc, yc;
int main() {
int gd = DETECT, gm;
int rx, ry;
float p1, p2;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
printf("Enter the center point:");
scanf("%d%d", &xc, &yc);
printf("Enter the value for Rx and Ry:");
scanf("%d%d", &rx, &ry);
x = 0;
y = ry;
disp();
p1 = (ry * ry) - (rx * rx * ry) + (rx * rx) / 4;
while ((2.0 * ry * ry * x) <= (2.0 * rx * rx * )) {
x++;
if (p1 <= 0)
p1 = p1 + (2.0 * ry * ry * x) + (ry * ry);
else {
y--;
p1 = p1 + (2.0 * ry * ry * x) - (2.0 * rx * rx * y) + (ry * ry);
}
disp(); }
x = -x; disp();
x = -x; x = rx;

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

y = 0;
disp();
p2 = (rx * rx) + 2.0 * (ry * ry * rx) + (rx * ry) / 4;
while ((2.0 * ry * ry * x) > (2.0 * rx * rx * y)) {
y++;
if (p2 > 0)
p2 = p2 + (rx * rx) - (2.0 * rx * rx * y);
else {
x--;
p2 = p2 + (2.0 * ry * ry * x) - (2.0 * rx * rx * y) + (rx * rx);
}
disp(); y = -y; disp(); y = -y;
}
getch(); closegraph();
return 0;
}
void disp() {
delay(50);
putpixel(xc + x, yc + y, 7);
putpixel(xc - x, yc + y, 7);
putpixel(xc + x, yc - y, 7);
putpixel(xc - x, yc - y, 7);
}

Output:

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

6. Program for scan line polygon fill algorithm.


Source Code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
int main() {
int n, i, j, k, gd = DETECT, gm, dy, dx; int x, y, temp;
int a[20][2], xi[20];
float slope[20];
printf("\n\n\t Enter the no. of edges of polygon: ");
scanf("%d", &n);
printf("\n\n\t Enter the co-ordinates of polygon:\n\n\n");
for (i = 0; i < n; i++) {
printf("\t X%d Y%d : ", i, i);
scanf("%d %d", &a[i][0], &a[i][1]);
}
a[n][0] = a[0][0];
a[n][1] = a[0][1];
detectgraph(&gd, &gm);
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
for (i = 0; i < n; i++) {
line(a[i][0], a[i][1], a[i + 1][0], a[i + 1][1]);
}
getch();
for (i = 0; i < n; i++) {
dy = a[i + 1][1] - a[i][1];
dx = a[i + 1][0] - a[i][0];
if (dy == 0) slope[i] = 1.0;
if (dx == 0) slope[i] = 0.0;
if ((dy != 0) && (dx != 0));
{
slope[i] = (float)dx / dy; }
}
for (y = 0; y < 480; y++) {

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

k = 0;
for (i = 0; i < n; i++) {
if (((a[i][1] <= y) && (a[i + 1][1] > y)) || ((a[i][1] > y) && (a[i + 1][1] <= y))) {
xi[k] = (int)(a[i][0] + slope[i] * (y - a[i][1])); k++;
}
}
for (j = 0; j < k - 1; j++) {
for (i = 0; i < k - 1; i++) {
if (xi[i] > xi[i + 1]) {
temp = xi[i]; xi[i] = xi[i + 1];
xi[i + 1] = temp; }
}
}
setcolor(7);
for (i = 0; i < k; i += 2) {
line(xi[i], y, xi[i + 1] + 1, y);
}
}
getch();
closegraph();
return 0;
}

Output:

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

7. Program to implement the 4-connected Boundary fill algorithm.


Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.>
#include<dos.h>
void boundary_fill(int x, int y, int fcolor, int bcolor) {
if ((getpixel(x, y) != bcolor) && (getpixel(x, y) != fcolor)) {
delay(10); putpixel(x,y,fcolor);
boundary_fill(x + 1, y, fcolor, bcolor);
boundary_fill(x - 1, y, fcolor, bcolor);
boundary_fill(x, y + 1, fcolor, bcolor);
boundary_fill(x, y- 1, fcolor, bcolor); }
}
int main() {
int x, y, fcolor, bcolor; int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
printf("Enter the seed point (x, y) for a circle(200,200,45): ");
scanf("%d%d", &x, &y);
printf("Enter boundary color: ");
scanf("%d", &bcolor); printf("Enter new color: ");
scanf("%d", &fcolor); circle(200, 200, 45);
boundary_fill(x, y, fcolor, bcolor); getch();
}

Output:

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

8. Program for 8-connected flood fills.


Source Code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
void flood(int, int, int, int); int main() {
int gd, gm = DETECT;
detectgraph(&gd, &gm);
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
rectangle(200, 200, 250, 250);
flood(220, 220, 7, 0);
getch(); }
void flood(int x, int y, int fill_col, int old_col) {
if (getpixel(x, y) == old_col) {
delay(10);
putpixel(x, y, fill_col);
flood(x + 1, y, fill_col, old_col);
flood(x - 1, y, fill_col, old_col);
flood(x, y + 1, fill_col, old_col);
flood(x, y - 1, fill_col, old_col);
flood(x + 1, y + 1, fill_col, old_col);
flood(x - 1, y + 1, fill_col, old_col);
flood(x + 1, y - 1, fill_col, old_col);
flood(x - 1, y - 1, fill_col, old_col); }
}

Output:

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

9. Program for creating House shape.


Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>#
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(7);
rectangle(60,80,150,200);
rectangle(95,140,120,200);
line(60,80,100,15); line(100,15,150,80);
circle(100,60,10);
getch();
closegraph();
}

Output:

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

LAB 2

1. Program for scaling.


Source Code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
int main()
{
int gd = DETECT, gm;
float x1, y1, x2, y2, sx, sy, x3, y3, x4, y4;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
printf("\nEnter the starting point coordinates (x1, y1): ");
scanf("%f %f", &x1, &y1);
printf("\nEnter the ending point coordinates (x2, y2): ");
scanf("%f %f", &x2, &y2);
printf("\nEnter scaling factors (sx, sy): ");
scanf("%f %f", &sx, &sy);
setcolor(7);
line(x1, y1, x2, y2);
outtextxy(x2 + 5, y2, "Object");
x3 = x1 * sx; y3 = y1 * sy;
x4 = x2 * sx; y4 = y2 * sy;
setcolor(15);
line(x3, y3, x4, y4);
outtextxy(x4 + 5, y4, "Image");
getch();
closegraph();
}

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

Output:

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

2. Program for reflection along x-axis.


Source Code:
#include <stdio.h>
#include <conio.h>
#include<graphics.h>
#include <math.h>
char IncFlag;
int PolygonPoints[3][2] = {{10, 100}, {110, 100}, {110, 200}};
void PolyLine() {
int iCnt;
cleardevice();
line(0, 240, 640, 240);
line(320, 0, 320, 480);
for (iCnt = 0; iCnt < 3; iCnt++) {
line(PolygonPoints[iCnt][0], PolygonPoints[iCnt][1], PolygonPoints[(iCnt + 1) % 3][0],
PolygonPoints[(iCnt + 1) % 3][1]);
}
}
void Reflect() {
float Angle;
int iCnt;
int Tx, Ty;
printf("endl");
for (iCnt = 0; iCnt < 3; iCnt++)
PolygonPoints[iCnt][1] = 480 - (PolygonPoints[iCnt][1]);
}
int main() {
int gDriver = DETECT, gMode; int iCnt;
initgraph(&gDriver, &gMode, "C:\\TurboC3\\BGI");
for (iCnt = 0; iCnt < 3; iCnt++) {
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine();

Don’t Forget to Follow Uni Bytes


Prepared by SHREE KRISHNA KAFLE/ www.unibytes.xyz

getch();
Reflect();
PolyLine();
getch();
closegraph();
}

Output:

Don’t Forget to Follow Uni Bytes


Uni Bytes / www.unibytes.xyz

3. Program for Reflection along y-axis.


Source Code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
char IncFlag;
int PolygonPoints[3][2] = {{10, 100}, {110, 100}, {110, 200}};
void PolyLine()
int iCnt; cleardevice();
line(0, 240, 640, 240);
line(320, 0, 320, 480);
for (iCnt = 0; iCnt < 3; iCnt++) {
line(PolygonPoints[iCnt][0], PolygonPoints[iCnt][1], PolygonPoints[(iCnt + 1) % 3][0],
PolygonPoints[(iCnt + 1) % 3][1]);
}
}
void Reflect() {
float Angle;
int iCnt; int Tx, Ty;
printf("Reflecting...\n");
for (iCnt = 0; iCnt < 3; iCnt++)
PolygonPoints[iCnt][0] = 640 - PolygonPoints[iCnt][0];
}
int main() {
int gd = DETECT, gm;
int iCnt;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
for (iCnt = 0; iCnt < 3; iCnt++) {
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine();
getch();
Don’t Forget to Follow Uni Bytes
Uni Bytes / www.unibytes.xyz

Reflect();
PolyLine();
getch();
closegraph();
}

Output:

Don’t Forget to Follow Uni Bytes


Uni Bytes / www.unibytes.xyz

4. Program for X-shear.


Source Code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
int main() {
int gd = DETECT, gm;
float shx, shy;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
printf("Enter shear factor shx along x-axis :");
scanf("%f", &shx);
line(100, 0, 200, 0);
line(200, 0, 200, 200);
line(200, 200, 100, 200);
line(100, 200, 100, 0);
printf("X-shear");
setcolor(12);
line((100 + (0 * shx)), 0, (200 + (0 * shx)), 0);
line((200 + (0 * shx)), 0, (200 + (200 * shx)), 200);
line((200 + (200 * shx)), 200, (100 + (200 * shx)), 200);
line((100 + (200 * shx)), 200, (100 + (0 * shx)), 0);
getch();
closegraph();
}

Output:

Don’t Forget to Follow Uni Bytes


Uni Bytes / www.unibytes.xyz

5. Program for Y-shear.


Source Code:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
int main() {
int gd = DETECT, gm;
float shx, shy;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
printf("Enter shear factor shy along y-axis :");
scanf("%f", &shy);
line(100, 10, 200, 10);
line(200, 10, 200, 200);
line(200, 200, 100, 200);
line(100, 200, 100, 10);
printf("Y-shear");
setcolor(12);
line(100, 10 + (shy * 100), 200, 10 + (shy * 200));
line(100, 200 + (shy * 100), 100, 10 + (shy * 100));
line(200, 10 + (shy * 200), 200, 200 + (shy * 200));
line(200, 200 + (shy * 200), 100, 200 + (shy * 100));
getch();
closegraph();
}

Don’t Forget to Follow Uni Bytes

You might also like