You are on page 1of 29

AMITY UNIVERSITY

RAJASTHAN

Lab File
AIIT

Computer Graphics System

SUBMITTED TO: - SUBMITTED BY: -


Mr. Amit Hirawat Pratiksha Gautam

[Type text]
Computer Graphics LAB
BCA 420

S. No. Task:- Page no.


1. Write a program to plot a pixel 3
2. Write a program to draw a line 4
3. Write a program to draw a rectangle 5
4. Write a program to draw a line of slope between 0 and 1 using DDA 6-7
Algorithm
5. Write a program to draw a line of slope between 1 and ∞ using DDA 8-9
Algorithm
6. Write a program to draw a line of slope between 0 and 1 using 10-11
midpoint algorithm
7. Write a program to draw a line of slope between 1 and ∞ using 12-13
midpoint algorithm
8. Write a program to draw a dashed line of slope 1 14-15
9. Write a program to draw a dotted line of slope 1 16-17
10. Write a program to draw a line of slope between 0 and -1 18-19
11. Write a program to draw a line of slope between -1 and -∞ 20-21
12. Write a program to draw an octant of a circle with center at (0,0) 22-23
13. Write a program to draw a circle with its center at point (0,0) 24-25
14. Write a program to draw an octant of a circle with center at (a,b) 26-27
15. Write a program to draw a circle with its center at point (a,b) 28-29

2 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Program to plot a pixel :-


#include<graphics.h>
#include<conio.h>

main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

putpixel(25, 25, WHITE);

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

OUTPUT :-

3 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write a program to draw a line :-

#include<graphics.h>
#include<stdio.h>

void main()
{
int gdriver = DETECT, gmode;
int x1 = 200, y1 = 200;
int x2 = 300, y2 = 300;

initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");


line(x1, y1, x2, y2);
getch();
closegraph();
}

OUTPUT :-

4 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write a program to draw a rectangle :-

#include<graphics.h>
#include<stdio.h>

void main()
{
int gdriver = DETECT, gmode;
int x1 = 200, y1 = 200;
int x2 = 200, y2 = 300;
int x3 = 300, y3 = 300;
int x4 = 300, y4 = 200;

initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");


line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x4, y4);
line(x4, y4, x1, y1);
getch();
closegraph();
}

OUTPUT :-

5 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write program to draw a line of slope between 0 and 1


using DDA algorithm :-
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,dx,dy,length,i;
float x,y,xinc,yinc;
int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
x1=20;
x2=80;
y1=30;
y2=60;

dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
length=abs(dx);
else
length=abs(dy);

xinc=dx/(float)length;
yinc=dy/(float)length;
x=x1;
y=y1;
putpixel(x,y,10);

for(i=0;i<length;i++)
{
putpixel(x,y,10);
x=x+xinc;
y=y+yinc;
delay(10);
}
getch();
closegraph();
}

6 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

OUTPUT:-

7 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write program to draw a line of slope between 1 and


∞ using DDA algorithm:-

#include<conio.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,dx,dy,length,i;
float x,y,xinc,yinc;
int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
x1=20;
x2=40;
y1=20;
y2=60;

dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
length=abs(dx);
else
length=abs(dy);

xinc=dx/(float)length;
yinc=dy/(float)length;
x=x1;
y=y1;
putpixel(x,y,10);

for(i=0;i<length;i++)
{
putpixel(x,y,10);
x=x+xinc;
y=y+yinc;
delay(10);
}
getch();
closegraph();
}

8 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

OUTPUT:-

9 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write program to draw a line of slope between 0 and 1


using midpoint algorithm :-
#include<stdio.h>
#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)


{
int dx, dy, p, x, y;

dx=x1-x0;
dy=y1-y0;

x=x0;
y=y0;

p=2*dy-dx;

while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
delay(10);
}
}

int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

x1=20;
x2=80;
y1=30;
y2=60;
10 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

drawline(x0, y0, x1, y1);


getch();
return 0;
}

OUTPUT:-

11 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write program to draw a line of slope between 1 and


∞ using DDA algorithm:-

#include<stdio.h>
#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)


{
int dx, dy, p, x, y;

dx=x1-x0;
dy=y1-y0;

x=x0;
y=y0;

p=2*dy-dx;

while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
delay(10);
}
}

int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

x0=20;
x1=40;
12 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

y0=20;
y1=60;

drawline(x0, y0, x1, y1);


getch();
return 0;
}

OUTPUT:-

13 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write a program to draw a dashed line of slope 1 :-


#include<stdio.h>
#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)


{
int dx, dy, p, x, y;

dx=x1-x0;
dy=y1-y0;

x=x0;
y=y0;

p=2*dy-dx;

while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
if (x % 4 == 0)
{
x=x+3;
}
else
{
x=x+1;
}
delay(10);
}
}

int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;

14 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

printf("Enter co-ordinates of first point: ");


scanf("%d%d", &x0, &y0);

printf("Enter co-ordinates of second point: ");


scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
getch();
return 0;
}

OUTPUT:-

15 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write a program to draw a dashed line of slope 1 :-


#include<stdio.h>
#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)


{
int dx, dy, p, x, y;

dx=x1-x0;
dy=y1-y0;

x=x0;
y=y0;

p=2*dy-dx;

while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
if (x % 2 == 0)
{
x=x+2;
}
else
{
x=x+1;
}
delay(10);
}
}

int main()
{

16 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

int gdriver=DETECT, gmode, error, x0, y0, x1, y1;


initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

printf("Enter co-ordinates of first point: ");


scanf("%d%d", &x0, &y0);

printf("Enter co-ordinates of second point: ");


scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
getch();
return 0;
}

OUTPUT:-

17 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write a program to draw a line of slope between 0 and


-1 :-
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,dx,dy,length,i;
float x,y,xinc,yinc;
int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
x1=80;
x2=20;
y1=30;
y2=60;

dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
length=abs(dx);
else
length=abs(dy);

xinc=dx/(float)length;
yinc=dy/(float)length;
x=x1;
y=y1;
putpixel(x,y,10);

for(i=0;i<length;i++)
{
putpixel(x,y,10);
x=x+xinc;
y=y+yinc;
delay(10);
}
getch();
closegraph();
}

18 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

OUTPUT:-

19 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write a program to draw a line of slope between -1 and


-∞ :-
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,dx,dy,length,i;
float x,y,xinc,yinc;
int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
x1=80;
x2=20;
y1=30;
y2=180;

dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
length=abs(dx);
else
length=abs(dy);

xinc=dx/(float)length;
yinc=dy/(float)length;
x=x1;
y=y1;
putpixel(x,y,10);

for(i=0;i<length;i++)
{
putpixel(x,y,10);
x=x+xinc;
y=y+yinc;
delay(10);
}
getch();
closegraph();
}

20 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

OUTPUT:-

21 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write a program to draw an octant of a circle with


center at (0,0) :-
#include<stdio.h>
#include<graphics.h>

void drawcircle(int x0, int y0, int radius)


{
int x = radius;
int y = 0;
int err = 0;

while (x >= y)
{
putpixel(x0 + y, y0 + x, 7);

if (err <= 0)
{
y += 1;
err += 2*y + 1;
}

if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}

int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

r=60;
x=0;
y=0;

drawcircle(x, y, r);

return 0;
}

22 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

OUTPUT:-

23 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write a program to draw a circle with center at (0,0) :-


#include<stdio.h>
#include<graphics.h>

void drawcircle(int x0, int y0, int radius)


{
int x = radius;
int y = 0;
int err = 0;

while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);

if (err <= 0)
{
y += 1;
err += 2*y + 1;
}

if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}

int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

r=60;
x=0;
y=0;

24 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

drawcircle(x, y, r);

return 0;
}

OUTPUT:-

25 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write a program to draw an octant of a circle with


center at (a,b) :-
#include<stdio.h>
#include<graphics.h>

void drawcircle(int x0, int y0, int radius)


{
int x = radius;
int y = 0;
int err = 0;

while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);

if (err <= 0)
{
y += 1;
err += 2*y + 1;
}

if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}

int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

r=60;
x=120;
y=120;

drawcircle(x, y, r);
getch();
closegraph();

return 0;
}
26 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

OUTPUT:-

27 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

Write a program to draw a circle with center at (a,b) :-


#include<stdio.h>
#include<graphics.h>

void drawcircle(int x0, int y0, int radius)


{
int x = radius;
int y = 0;
int err = 0;

while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);

if (err <= 0)
{
y += 1;
err += 2*y + 1;
}

if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}

int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

r=60;
x=120;
y=120;

28 | Page
Pratiksha Gautam
A21704817010
Computer Graphics LAB
BCA 420

drawcircle(x, y, r);
getch();
closegraph();

return 0;
}

OUTPUT :-

29 | Page
Pratiksha Gautam
A21704817010

You might also like