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

Lab Manual Gor Computer Graphics and Multimedia

The document contains 10 questions and answers about drawing various shapes and images using C graphics programming. It provides code snippets to draw lines, squares, circles, faces, flags, huts, increasing lines, circle-rectangles, swastikas, and lines using the Digital Differential Analyzer (DDA) algorithm. For each question, it lists the input code and output from running the code.

Uploaded by

kartikey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Lab Manual Gor Computer Graphics and Multimedia

The document contains 10 questions and answers about drawing various shapes and images using C graphics programming. It provides code snippets to draw lines, squares, circles, faces, flags, huts, increasing lines, circle-rectangles, swastikas, and lines using the Digital Differential Analyzer (DDA) algorithm. For each question, it lists the input code and output from running the code.

Uploaded by

kartikey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Q1 Write a program to draw a straight line?

ANS

Input

#include<graphics.h>

#include<stdlib.h>

#include<stdio.h>

#include <conio.h>

Void main()

int gdriver=DETECT,&gmode;

int x1,y1,x2,y2,i,step,xn,yn, dx,dy;

clrscr();

initgraph(&gdriver,&gmode,“C:\\TURBOC3\\BGI”);

printf(“Enter the starting coordinates:”);

scanf(“%d%d”,&x1,&y1);

printf(“Enter the end coordinates:”);

scanf(“%d%d”,&x2,&y2);

dx=x2-x1;

dy=y2-y1;

if(abs(dx)> abs(dy))

step= abs(dx);

else
{

step= abs(dy);

xn=dx/step;

yn=dy/step;

for(i=1;i<step;i++)

putpixel(x1,y1,LIGHTBLUE);

x1=x1+xn;

y1=y1+yn;

getch();

closegraph();

Output

Enter the starting coordinates

Enter the end coordinates

Q2 Write a program to draw a square?

Ans

Input

#include <graphics.h>

#include<stdlib.h>

#include<stdio.h>

#include <conio.h>

int main()
{

int gdriver = DETECT, gmode;

int left = 150, top = 150;

int right = 450, bottom = 450;

initgraph(&gdriver,&gmode,“C:\\TURBOC3\\BGI”);

rectangle(left, top, right, bottom);

getch();

closegraph();

return 0;

Output

Q3 Write a program to draw a circle?

Ans

Input

#include <stdio.h>

#include <graphics.h>
#include <stdlib.h>

#include <conio.h>

int main()

int gdriver, gmode;

int x, y, radius;

gdriver = DETECT;

initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

setcolor(CYAN);

x = 200;

y = 300;

radius = 100;

circle(x, y, radius);

getch();

closegraph();

return 0;

output
Q4 Write a program to draw a smiling face?

Ans

Input

#include <stdio.h>

#include <graphics.h>

#include <stdlib.h>

#include <conio.h>

int main()

int gdriver = DETECT, gmode;

initgraph(&gdriver, &gmode, " C:\\TURBOC3\\BGI ");

setcolor(YELLOW);

circle(300, 100, 40);

setfillstyle(SOLID_FILL, YELLOW);

floodfill(300, 100, YELLOW);

setcolor(BLACK);

setfillstyle(SOLID_FILL, BLACK);

fillellipse(310, 85, 2, 6);


fillellipse(290, 85, 2, 6);

ellipse(300, 100, 205, 335, 20, 9);

ellipse(300, 100, 205, 335, 20, 10);

ellipse(300, 100, 205, 335, 20, 11);

getch();

closegraph();

return 0;

Output

Q5 Write a program to draw a flag?

Input

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<graphics.h>

#include<math.h>

void main()

{
int gdriver, gmode;

int r,i,a,b,x,y;

float PI=3.14;

detectgraph(&gdriver,& gmode);

initgraph(&gdriver,& gmode," C:\\TURBOC3\\BGI ");

settextstyle(7,HORIZ_DIR,1);

outtextxy(300,150,"HAPPY INDEPENDENCE DAY");

outtextxy(400,200,"VANDE MATARAM");

setcolor(LIGHTGRAY);

rectangle(90,100,100,400);

setfillstyle(SOLID_FILL,LIGHTGRAY);

floodfill(91,101,LIGHTGRAY);

// DRAW THE FIRST STAIR AND COLOR IT

setfillstyle(SOLID_FILL,LIGHTGRAY);

line(40,400,150,400);

line(150,400,150,420);

line(150,420,40,420);

line(40,420,40,400);

floodfill(41,401,LIGHTGRAY);

// DRAW THE SECOND STAIR AND COLOR IT


setfillstyle(SOLID_FILL,LIGHTGRAY);

line(20,420,170,420);

line(20,440,170,440);

line(20,420,20,440);

line(170,420,170,440);

floodfill(21,421,LIGHTGRAY);

// DRAW THE TOP RECTANGLE AND COLOR IT

setcolor(LIGHTRED);

rectangle(100,100,250,125);

setfillstyle(SOLID_FILL,LIGHTRED);

floodfill(101,101,LIGHTRED);

// DRAW THE MIDDLE RECTANGLE AND COLOR IT

setcolor(WHITE);

rectangle(100,125,250,150);

setfillstyle(SOLID_FILL,WHITE);

floodfill(101,126,WHITE);

// DRAW THE BOTTOM RECTANGLE AND COLOR IT


setcolor(GREEN);

rectangle(100,150,250,175);

setfillstyle(SOLID_FILL,GREEN);

floodfill(101,151,GREEN);

// DRAW THE CIRCLE

a=175; //CENTER

b=137; //CENTER

r=12; //RADIUS

setcolor(BLUE);

circle(a,b,r);

// SPOKES

for(i=0;i<=360;i=i+15)

x=r*cos(i*PI/180);

y=r*sin(i*PI/180);

line(a,b,a+x,b-y);

getch();

closegraph();
}

Output

Q6 Write a program to draw a hut?

Ans
Input
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void main()

    int gdriver=DETECT,&gmode;

initgraph(&gdriver,&gmode,“C:\\TURBOC3\\BGI”);

//house

line(100,100,150,50);

line(150,50,200,100);

line(150,50,350,50);

line(350,50,400,100);
rectangle(100,100,200,200);

rectangle(200,100,400,200);

rectangle(130,130,170,200);

rectangle(250,120,350,180);

//color

setfillstyle(2,3);

floodfill(131,131,WHIITE);

floodfill(201,101,WHIITE);

setfillstyle(11,7);

floodfill(101,101,WHIITE);

floodfill(150,50,WHIITE);

floodfill(163,55,WHIITE);

floodfill(251,121,WHIITE);

getch();

closegraph();

Output
Q7 Write a program to draw a line in increasing order?

Input

#include<graphics.h>
#include<math.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
void main()
{
 float x,y,x1,y1,x2,y2,dx,dy,e;
 int i, gdriver =DETECT, gmode;
 clrscr();

 cout<<"Enter the value of x1 :\t";


 cin>>x1;
 cout<<"Enter the value of y1 :\t";
 cin>>y1;
 cout<<"Enter the value of x2 :\t";
 cin>>x2;
 cout<<"Enter the value of y2 :\t";
 cin>>y2;

 initgraph(&gdriver,& gmode," C:\\TURBOC3\\BGI ");

   dx=abs(x2-x1);
   dy=abs(y2-y1);
   x = x1;
   y = y1;

   e=2*dy-dx;
   i=1;    
do
 {
  putpixel(x,y,WHITE);
  delay(60);
  while(e >= 0)
     {
      y=y+1;
      e=e-2*dx;
     }
  x=x+1;
  e=e+2*dy;
  i=i+1;
 }
while(i <= dx);
 getch();
 closegraph();
}

output

enter the value of x1 :100 100

enter the value of y1 : enter the value of x2 : 150 140


Q8 Write a program to draw a circle rectangle?

Input

#include<stdio.h>

#include <conio.h>

#include<graphics.h>

Void main()

int gdriver = DETECT, gmode;

initgraph(&gdriver, &gmode, " C:\\TURBOC3\\BGI ");

setcolor(GREEN);

rectangle(50,50,110,110);

circle(80,80,50);

getch();

closegraph();

Output
Q9 Write a program to draw a swastik?

Input

#include<stdio.h>
int main()
{
int rows, cols, i, j;
printf("Enter number of rows and columns: \n");
scanf("%d%d", &rows, &cols);
for(i=0; i < (rows*2 + 1); i++)
{
for(j=0; j < (cols*2 + 1); j++)
{
if(i == rows || j == cols)
printf("*");
else if(i == 0 && j > cols)
printf("*");
else if(j == cols*2 && i > rows)
printf("*");
else if(i == rows*2 && j < cols)
printf("*");
else if(j == 0 && i < rows)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 1;
}
Output

Q10 write a program to draw a line using DDA algorithm?

Input

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void main()

int gdrive = DETECT,gmode,i;

float x, y,dx,dy,steps;

int x0, x1, y0, y1;


initgraph(&gdrive, &gmode, " C:\\TURBOC3\\BGI ");

setbkcolor(WHITE);

x0 = 100 , y0 = 200, x1 = 500, y1 = 300;

dx = (float)(x1 - x0);

dy = (float)(y1 - y0);

if(dx>=dy)

steps = dx;

else

steps = dy;

dx = dx/steps;

dy = dy/steps;

x = x0;

y = y0;

i = 1;

while(i<= steps)

{
putpixel(x, y, RED);

x += dx;

y += dy;

i=i+1;

getch();

closegraph();

Output

You might also like