0% found this document useful (0 votes)
75 views21 pages

CG File

The document contains a list of 10 programming assignments for a BCA student named Sarthak Sharma. The assignments involve writing programs in C to draw various shapes and images using computer graphics algorithms like DDA, Bresenham, and Midpoint. The shapes include lines, circles, rectangles, arcs, ellipses, triangles, chessboards, and a moving wheel. For each assignment, a space is provided for the student to sign and date upon completion.

Uploaded by

Sarthak Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views21 pages

CG File

The document contains a list of 10 programming assignments for a BCA student named Sarthak Sharma. The assignments involve writing programs in C to draw various shapes and images using computer graphics algorithms like DDA, Bresenham, and Midpoint. The shapes include lines, circles, rectangles, arcs, ellipses, triangles, chessboards, and a moving wheel. For each assignment, a space is provided for the student to sign and date upon completion.

Uploaded by

Sarthak Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Jagannath International Management School

Vasant Kunj, New Delhi – 110070

(Affiliated to Guru Gobind Singh Indraprastha University, New Delhi)


Recognized u/s 2(f) by UGC & Accredited with ‘A’ Grade by NAAC
Participant of UNGC & UNPRME, New York ISO 9001:2015 Quality
Certified

Bachelor of Computer Application (BCA)

Batch: 2020-2023
Session: August- December 2022

Course-BCA Name- Sarthak sharma Subject Code:


20351 Enrol. No.- 35514202020

S. No. Name of the Practical Date Signature


1 Write a program to draw the following using
predefined functions in computer graphics:

Circle
Line
Rectangle
Arc
Ellipse

Also, change the background colour.

2 Write a program to draw a moving Cycle using Pre


defined functions in computer graphics

3 Write a program using pre-defined functions in


computer graphics to draw a night sky and stars
flickering

4 Write a program in C to draw a line using Digital


Differential Analyzer (DDA) Algorithm.

5 Write a menu driven program in C to draw an


Equilateral, Isosceles and Scalene triangle using line
drawing function.
6 Write a program in C to draw a line using
Bresenham’s Algorithm.

7 Write a program in C to draw a chessboard using


Digital Differential Analyzer (DDA) Algorithm.

8 Write a program in C to draw a circle using


Bresenham’s Algorithm.

9 Write a program in C to draw a circle using MidPoint


Algorithm.

10 Write a program in C to draw a moving wheel using


Mid-Point and DDA line algorithm.
• Write a program to draw the following using pre-defined functions in
computer graphics:

• Circle
• Line
• Rectangle
• Arc
• Ellipse
Also, change the background colour.

Code
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph (&gd,&gm,"c:\\turboc3\\bgi");
setbkcolor(RED);
printf("\t\t\t\n\nLINE");
line(50,40,190,40);
printf("\t\t\n\n\n\nRECTANGLE");
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\nARC");
arc(170,200,180,0,30);
printf("\t\n\n\n\nCIRCLE");
circle(170,270,30);
printf("\t\n\n\n\nELLIPSE");
ellipse(170,350,0,360,50,30);
getch();
}
Output

 Write a program to draw a moving Cycle using Pre-defined functions in


computer graphics.
Code
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm,x;
initgraph(&gd,&gm,"c://turboc3//bgi");
while(!kbhit())
{
for(x=0;x<=650;x++)
{
cleardevice();
//road
line(0,401,650,401);
//wheels
circle(40+x,370,30);
circle(150+x,370,30);
//frame
line(40+x,370,100+x,370);
line(40+x,370,60+x,330);
line(100+x,370,120+x,330);
line(60+x,330,120+x,330);
//handle
line(150+x,370,100+x,300);
line(100+x,300,90+x,300);
//handle
line(60+x,330,60+x,310);
line(50+x,310,70+x,310);
delay(20);
}
}
}

Output

 Write a program using pre-defined functions in computer graphics to


draw a night sky and stars flickering.
Code
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm,x,y,i;
initgraph(&gd,&gm,"c://turboc3//bgi");
//max resolution
getmaxx();
//color in pixels
putpixel(x,y, GREEN);
//coordinates
while(!kbhit())
{
for(i=0;i<500;i++)
{
x=rand()%getmaxx();
y=rand()%getmaxy();
putpixel(x,y,WHITE);
//WHITE COLOR
}
delay(850);
cleardevice();
}
}

Output

 Write a program in C to draw a line using Digital Differential Analyzer


(DDA) Algorithm.

Code
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdio.h>
void dda(int x1,int y1,int x2,int y2)
{
int dx=x2-x1,dy=y2-y1,s,k;
float xinc,yinc, x=x1,y=y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else s=abs(dy);
xinc=dx/s;
yinc=dy/s;
putpixel(x,y,15);
for(k=0;k<dx;k++)
{
x+=xinc;
y+=yinc;
putpixel(abs(x),abs(y),15);
delay(35);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
dda(50,50,150,150);
getch();
closegraph();
}

Output
 Write a menu driven program in c to draw an equilateral, isosceles and
scalene triangle using line drawing function.

Code
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<ctype.h>
void main()
{
int gd=DETECT,gm;
int c,x1,y1,x2,y2,x3,y3,s1,t=0;
char ask='y';
clrscr();
do
{
printf("Choose which type of triangle you want to draw: \n");
printf("1. Equilateral Triangle \n");
printf("2. Isoceles Triangle \n");
printf("3. Scalene Triangle \n");
printf("4. Exit\n");
printf("Enter the number : ");
scanf("%d",&c);
clrscr();
if(c==1)
{
printf("Enter the side of equilateral triangle \n");
scanf("%d",&s1);
printf("Enter the coordinates for any 2 points of the triangle \nX1=");
scanf("%d",&x1); printf("Y1=");
scanf("%d",&y1); printf("X2=");
scanf("%d",&x2); printf("Y2=");
scanf("%d",&y2);
x3=abs((x1+x2+sqrt(3)*(y1-y2))/2);
y3=abs((y1+y2+sqrt(3)*(x2-x1))/2);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
delay(5000);
closegraph();
printf("Was this the required one ? y or n \n");
scanf("%s",&ask);
if(tolower(ask)=='n')
{
int x,y;
x=x1;
y=y1;
x1=x2;
y1=y2;
x2=x;
y2=y;
x3=abs((x1+x2+sqrt(3)*(y1-y2))/2);
y3=abs((y1+y2+sqrt(3)*(x2-x1))/2);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
getch();
closegraph();
}
else
{
printf("\nOKAY!");
delay(3000);
clrscr();
}
}
else if(c==2 || c==3)
{
printf("Enter the coordinates for all points of the ");
if(c==2)
{
printf("Isosceles ");
}
else if(c==3)
{
printf("Scalene ");
}
printf("triangle \n X1=");
scanf("%d",&x1);
printf("Y1=");
scanf("%d",&y1);
printf("X2=");
scanf("%d",&x2);
printf("Y2=");
scanf("%d",&y2);
printf("X3=");
scanf("%d",&x3);
printf("Y3=");
scanf("%d",&y3);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
getch();
closegraph();
}
else if(c==4)
{
t=1;
}
}
while(t<1);
}

Output
 Write a program in C to draw a line using Bresenham’s Algorithm.

Code
#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;
}
}
int main()
{
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
 Write a program in c to draw a chessboard using Digital Differential
Analyzer (DDA) Algorithm.

Code
#include<graphics.h>
#include<conio.h>
#include<math.h>
void DDA(int x1, int y1, int x2, int y2)
{
int dx = x2 - x1, dy = y2 - y1, s, k;
float xinc, yinc, x = x1, y = y1;
if (abs(dx) > abs(dy))
{
s = abs(dx);
}
else
{
s = abs(dy);
}
xinc = dx / s;
yinc = dy / s;
putpixel(x, y, 15);
for(k = 0; k < s; k++)
{
x += xinc;
y += yinc;
putpixel(abs(x), abs(y), 15) ;
}
}
void fill(int x, int y)
{
int i;
for(i=x;i<(x+50);i++)
DDA(i,y,i,y+50);
}
void main()
{
int i,j,c=0;
int gd=DETECT,gm;
initgraph(&gd, &gm, "c://turboc3//bgi");
DDA(100, 50, 100, 450);
DDA(100, 50, 500, 50);
DDA(500, 50, 500, 450);
DDA(100, 450,500, 450);
for(i=100;i<500;i=i+50)
{
for(j=50;j<450;j=j+50)
{
if(c%2==0)
fill(i,j);
c++;
}
c++;
}
getch();
}

Output
 Write a program in c to draw a circle Bresenham’s
Algorithm.

Code
#include<graphics.h>
#include<stdio.h>
void pixel(int xc,int yc,int x,int y);
int main()
{
int gd,gm,xc,yc,r,x,y,p;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C://TurboC3//BGI");
printf("Enter center of circle :");
scanf("%d%d",&xc,&yc);
printf("Enter radius of circle :");
scanf("%d",&r);
x=0;
y=r;
p=3-2*r;
pixel(xc,yc,x,y);
while(x<y)
{
if(p<0)
{
x++;
p=p+4*x+6;
}
else
{
x++;
y--;
p=p+4*(x-y)+10;
}
pixel(xc,yc,x,y);
}
getch();
closegraph();
return 0;
}
void pixel(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);
}
Output
 Write a program in C to draw a circle using Mid-Point Algorithm.
Code
#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");
printf("Enter radius of circle: ");
scanf("%d", &r);
printf("Enter co-ordinates of center(x and y): ");
scanf("%d%d", &x, &y);
drawcircle(x, y, r);
getch();
return 0;
}

Output
 Write a program in C to draw a moving wheel using Mid-Point and
DDA line algorithm.

Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
int l = 1;
void ddaline(int x1, int y1, int x2, int y2)
{
int s, dx, dy, m;
float xi, yi, x, y;
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy))
s = abs(dx); else
s = abs(dy);
xi = dx / (float) s;
yi = dy / (float) s;
x = x1;
y = y1;
putpixel(x1 + 0.5, y1 + 0.5, 15);
for (m = 0; m < s; m++)
{
x += xi;
y += yi;
putpixel(x + 0.5, y + 0.5, 15);
}
}
void plotpoints1(int x, int y, int cx, int cy)
{
putpixel(cx + x, cy + y, 15);
putpixel(cx - x, cy - y, 15);
putpixel(cx - y, cy + x, 15);
putpixel(cx + y, cy - x, 15);
if (l % 20 == 0)
{
ddaline(cx - x, cy - y, cx + x, cy + y);
ddaline(cx - y, cy + x, cx + y, cy - x);
}
l++;
}
void plotpoints2(int x, int y, int cx, int cy)
{
putpixel(cx - x, cy + y, 15);
putpixel(cx + x, cy - y, 15);
putpixel(cx + y, cy + x, 15);
putpixel(cx - y, cy - x, 15);
if (l % 20 == 0)
{
ddaline(cx + x, cy - y, cx - x, cy + y);
ddaline(cx - y, cy - x, cx + y, cy + x);
}
l++;
}
void mcircle(int cx, int cy, int r)
{
int x = 0, y, p;
y = r;
p = 1 - r;
while (x < y)
{
plotpoints1(x, y, cx, cy);
x++;
if (p < 0)
p += 2 * x + 1;
else
{
y--;
p += 2 * (x - y) + 1;
}
}
x = y + 1;
while(abs(x) > 0)
{
plotpoints2(x, y, cx, cy);
x--;
if (p >= 0)
p = p - 2 * x - 1;
else
{
y++;
p = p - 2 * (x - y) - 1;
}
}
}
void main()
{
int gd = DETECT, gm = DETECT;
int i = 0;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
while (!kbhit())
{
if (i > 640)
i = -200;
cleardevice();
mcircle(100 + (i++), 200, 100);
delay(90);
i++;
}
getch();
}
Output

You might also like