Practical List CG

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 65

Practical List

Q1.Solve the following:


b) Draw a co-ordinate axis at the center of
the screen.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm,a,b;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\Turbo\\BGI ");
a=getmaxx();
b=getmaxy();
putpixel(a/2,b/2,10);
getch();
}

Q2. Solve the following:


a) Divide your screen into four regions,
draw circle, rectangle, ellipse and half
ellipse in each region with appropriate
message.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
int x,y,;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
line(x,0,x,getmaxy());
line(0,y,getmaxx(),y);
outtext("Circle");
circle(x/2,y/2,50);
rectangle(x+50,y-200,x+200,y-50);
ellipse(x/2,y+y/2,0,360,100,50);
ellipse(x+x/2,y+y/2,0,180,100,50);
getch();
}

b) Draw a simple hut on the screen


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
setcolor(RED);
rectangle(50,180,150,300);
rectangle(150,180,320,300);
rectangle(80,250,120,300);
line(100,100,50,180);
line(100,100,150,180);
line(100,100,300,100);
line(300,100,320,180);
getch();
closegraph();
}
Q3 Draw the following basic shapes in the
centre of the screen:
i-Circle (x,y,Radius)
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
int main()
{
int gd,gm;
int x ,y ,radius=80;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
x = getmaxx()/2;
y = getmaxy()/2;
setcolor(RED);
outtextxy(200,300,”circle");
setcolor(RED);
// circle(20, 30, radius);
getch();
closegraph();
return 0;
}
ii- Rectangle (left,top,right,bottom)
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
int main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm, "C:\\Turboc3\\BGI");
outtext("Rectangle");
rectangle(150, 50, 400, 150);
getch();
closegraph();
return 0;
}

IV- Concentric circle


#include<iostream.h>
#include<graphics.h>
#include<conio.h>
int main()
{
   int gd = DETECT,gm;
   int x ,y;
   initgraph(&gd, &gm, "C:\\TC\\BGI");
   /* Initialize center of circle with center of
screen */
   x = getmaxx()/2;
   y = getmaxy()/2;
   outtextxy(240, 50, "Concentric Circles");
   setcolor(RED);
   circle(x, y, 30);
   setcolor(GREEN);
   circle(x, y, 50);
   setcolor(YELLOW);
   circle(x, y, 70);
   setcolor(BLUE);
   circle(x, y, 90);
   getch();
   closegraph();
   return 0;
}

v. Ellipse
(x,y,startingangle,endangle,xradius,yradius)
#include<iostream-.h>
#include<graphics.h>
#include<conio.h>
int main()
{
int gd,gm;
int x ,y;
detectgraph(&gd,&gm);
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(x-100, 50, "ELLIPSE");
ellipse(x, y, 0, 360, 120, 60);
getch();
closegraph();
return 0;
}

iv- Line (x1,y1,x2,y2)


#include<iostream.h>
#include <graphics.h>
#include <conio.h>
void main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
outtext("Line");
line(110,210,200,10);
getch();
closegraph();
}

Q4.Solve the following:


a) Develop the program for DDA Line
drawing algorithm.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
void main()
{
clrscr();
int gd,gm,i;
float x,y,x1,y1,x2,y2,dx,dy,delx,dely,l;
cout<<"\n Enter coordinates:";
cin>>x1;
cin>>y1;
cin>>x2;
cin>>y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\Turboc3\\BGI");
dx=x2-x1;
dy=y2-y1;
if(dx>=dy)
l=dx;
else
l=dy;
delx=dx/l;
dely=dy/l;
x=x1+0.5;
y=y1+0.5;
i=1;
while(i<=l)
{
putpixel(x,y,WHITE);
x=x+delx;
y=y+dely;
i=i+1;
delay(50);
}
getch();
closegraph();
}
b) Develop the program for Breshnam’s Line
Algorithm.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
sign(int,int);
void main()
{
int
x,y,x1,x2,y1,y2,s1,s2,dx,dy,temp,err,i,intchg;
int gd,gm;
cout<<"Enter the end points of a line:";
cin>>x1>>y1>>x2>>y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
x=x1;y=y1;
dx=abs(x2-x1);
dy=abs(y2-y1);
s1=sign(x2,x1);
s2=sign(y2,y1);
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
intchg=1;
}
else
intchg=0;
err=2*dy-dx;
for(i=1;i<=dx;i++)
{
if((x>=0&&y>=0)||(x>=0&&y<=0))
putpixel(x+getmaxx()/2,getmaxy()/2-
y,WHITE);
else if((x<=0&&y>=0)||(x<=0&&y<=0))
putpixel(getmaxx()/2+x,getmaxy()/2-y,5);

while(err>=0)
{
if(intchg==1)
x=x+s1;
else
y=y+s2;
err=err-2*dx;
}
if(intchg==1)
y=y+s2;
else
x=x+s1;
err=err+2*dy;
}
getch();
closegraph();
}
int sign(int x2,int x1)
{
if(x2<x1)return(-1);
if(x2==x1)return(0);
return(1);
}
Q5. Solve the following:
a) Develop the program for the mid-
point circle drawing algorithm.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
float midfactor;
int x,y,radius,graphdriver,graphmode,i;
cout<<"Enter the radius of circle:";
cin>>radius;
detectgraph(&graphdriver,&graphmode);
initgraph(&graphdriver,&graphmode,"C:\\
TurboC3\\BGI");
x=0;
y=radius;
midfactor=(5/4)-radius;
line(getmaxx()/2,0,getmaxx()/
2,getmaxy());
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
do
{
putpixel(getmaxx()/2+x,getmaxy()/2+y,5);
putpixel(getmaxx()/2+y,getmaxy()/2+x,5);
putpixel(getmaxx()/2+y,getmaxy()/2-
x,5);
putpixel(getmaxx()/2+x,getmaxy()/2-
y,5);
putpixel(getmaxx()/2-x,getmaxy()/2-
y,5);
putpixel(getmaxx()/2-y,getmaxy()/2-
x,5);
putpixel(getmaxx()/2-y,getmaxy()/2+x,5);
putpixel(getmaxx()/2-x,getmaxy()/2+y,5);

if(midfactor<0)
midfactor=midfactor+2*x+3;
else
{
midfactor=midfactor+2*(x-y)+5;
y=y-1;
}
x=x+1;
}
while(x<y);
getch();
closegraph();
}
b) Develop the program for the mid-point
Ellipse drawing algorithm.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
clrscr();
long int yk,xk,x0,y0,k,p0,pk,sk,rx,ry;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\
BGI");

cout<<"Enter the value of radius along x


axis:";
cin>>rx;

cout<<"Enter the value of radius along y


axis:";
cin>>ry;
setcolor(WHITE);

line(320,0,320,640);
line(0,240,640,240);

xk=0;
yk=ry;
pk=(ry*ry)-(rx*rx*ry)+(0.25*rx*rx);

while((2*(ry*ry)*xk)<(2*(rx*rx)*yk))
{
putpixel(xk+320,yk+240,WHITE);
putpixel(-xk+320,yk+240,WHITE);
putpixel(xk+320,-yk+240,WHITE);
putpixel(-xk+320,-yk+240,WHITE);
if(pk<0)
{
xk=xk+1;
pk=pk+2*(ry*ry)*xk+(ry*ry);
}
else
{ xk=xk+1;
yk=yk-1;
pk=pk+2*(ry*ry)*xk-2*(rx*rx)*yk+
(rx*rx);
}
delay(10);
}
x0=xk;
y0=yk;
sk=(ry*ry*pow((x0+0.5),2))+
(rx*rx*pow((y0-1),2))-(rx*rx*ry*ry);

while(yk>0)
{
putpixel(xk+320,yk+240,WHITE);
putpixel(-xk+320,yk+240,WHITE);
putpixel(xk+320,-yk+240,WHITE);
putpixel(-xk+320,-yk+240,WHITE);

if(sk>0)
{
xk=xk;
yk=yk-1;
sk=sk-2*(rx*rx)*yk+(rx*rx);
}
else
{
xk=xk+1;
yk=yk-1;
sk=sk+2*(ry*ry)*xk-2*(rx*rx)*yk+
(rx*rx);
}
}
delay(10);
getch();
closegraph();
}
Q6 .Solve the following:
a) Write a program to implement 2D
scaling.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm;
int x1,y1,x2,y2,x3,y3,sx,sy;
cout<<"Enter value for SX & SY=";
cin>>sx>>sy;
cout<<"Enter value for the co-
ordinates=";
cin>>x1>>y1>>x2>>y2>>x3>>y3;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
setcolor(BROWN);
line(x1*sx,y1*sy,x2*sx,y2*sy);
line(x2*sx,y2*sy,x3*sx,y3*sy);
line(x3*sx,y3*sy,x1*sx,y1*sy);
getch();
}
b) Write a program to implement 2D
Translation.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm;
int x1,y1,x2,y2,x,y;
cout<<"Enter points to draw a line:";
cin>>x1>>y1>>x2>>y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
line(x1,y1,x2,y2);
cout<<"Enter translation value=";
cin>>x>>y;
x1=(x1+x);
y1=(y1+y);
x2=(x2+x);
y2=(y2+y);
cout<<"Line after Translation:";
line(x1,y1,x2,y2);
getch();
}
Q7 Solve the following:
a) Perform 2D Rotation on a given object.
#include <conio.h>
#include <iostream.h>
#include <graphics.h>
#include <math.h>
void main(){
int
x1=200,y1=200,x2=250,y2=250,x3=180,y3=27
0,option;
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TurboC3\\
BGI");
do{
cleardevice();
gotoxy(1,1);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
cout<<"Rotation=";
cin>>option;
switch(option)
{
case 3:
float deg;
cout<<"Enter angle:";
cin>>deg;
deg = deg*3.14/180;
int x,y;
x=x1;y=y1;
x1 = x*cos(deg)-y*sin(deg);
y1 = x*sin(deg)+y*cos(deg);
x=x2;y=y2;
x2 = x*cos(deg)-y*sin(deg);
y2 = x*sin(deg)+y*cos(deg);
x=x3;y=y3;
x3 = x*cos(deg)-y*sin(deg);
y3 = x*sin(deg)+y*cos(deg);
break;
case 4:
break;
default:
cout<<"Invalid choice";
}
}while(option!=4);
closegraph();
}
Q8 Solve the following:
a.Write a program to implement Cohen-
Sutherland clipping.
}#include <iostream>
const int INSIDE = 0;
const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
const int x_max = 10;
const int y_max = 8;
const int x_min = 4;
const int y_min = 4;
int computeCode(double x, double y)
{
int code = INSIDE;
if (x < x_min)
code |= LEFT;e
else if (x > x_max)
code |= RIGHT;
if (y < y_min)
code |= BOTTOM;
else if (y > y_max)
code |= TOP;

return code;
}
void cohenSutherlandClip(double x1, double
y1,
double x2, double y2)
{
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);

bool accept = false;

while (true)
{
if ((code1 == 0) && (code2 == 0))
{

accept = true;
break;
}
else if (code1 & code2)
{

break;
}
else
{
int code_out;
double x, y;

if (code1 != 0)
code_out = code1;
else
code_out = code2;

if (code_out & TOP)


{

x = x1 + (x2 - x1) * (y_max - y1) / (y2 -


y1);
y = y_max;
}
else if (code_out & BOTTOM)
{

x = x1 + (x2 - x1) * (y_min - y1) / (y2 -


y1);
y = y_min;
}
else if (code_out & RIGHT)
{

y = y1 + (y2 - y1) * (x_max - x1) / (x2 -


x1);
x = x_max;
}
else if (code_out & LEFT)
{
y = y1 + (y2 - y1) * (x_min - x1) / (x2 -
x1);
x = x_min;
}

if (code_out == code1)
{
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else
{
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept)
{
cout <<"Line accepted from " << x1 << ", "
<< y1 << " to "<< x2 << ", " << y2 <<
endl;

}
else
cout << "Line rejected" << endl;
}

int main()
{
cohenSutherlandClip(5, 5, 7, 7);
cohenSutherlandClip(7, 9, 11, 4);
cohenSutherlandClip(1, 5, 4, 1);

return 0;

Output:

Before clipping :
After clipping :
Q9 Solve the following:
a)Write a program to fill a circle using
Flood Fill Algorithm.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
void flood_fill8(int x,int y,int fillcolor,int
oldcolor)
{
if(getpixel(x,y)==oldcolor)
{
putpixel(x,y,fillcolor);
delay(15);
flood_fill8(x+1,y,fillcolor,oldcolor);
flood_fill8(x-1,y,fillcolor,oldcolor);
flood_fill8(x,y+1,fillcolor,oldcolor);
flood_fill8(x,y-1,fillcolor,oldcolor);
flood_fill8(x+1,y+1,fillcolor,oldcolor);
flood_fill8(x+1,y-1,fillcolor,oldcolor);
flood_fill8(x-1,y-1,fillcolor,oldcolor);
flood_fill8(x-1,y-1,fillcolor,oldcolor);
}
}

void main()
{
int gd=DETECT,gm;
int mx,my;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");

setcolor(BLUE);
setfillstyle(SOLID_FILL,RED);
rectangle(40,40,100,100);
floodfill(51,51,BLUE);
flood_fill8(51,51,WHITE,RED);

getch();
closegraph();
}
b)Write a program to perform Boundary
Fill Algorithm.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void bfill(int x,int y,int fill,int boundary)


{
int current;
current=getpixel(x,y);
if((current!=boundary)&&(current!=fill))
{
putpixel(x,y,fill);
delay(10);
bfill(x+1,y,fill,boundary);
bfill(x-1,y,fill,boundary);
bfill(x,y+1,fill,boundary);
bfill(x,y-1,fill,boundary);
bfill(x+1,y+1,fill,boundary);
bfill(x+1,y-1,fill,boundary);
bfill(x-1,y+1,fill,boundary);
bfill(x-1,y-1,fill,boundary);
}
}
void main()
{
int gd,gm;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(50,50,100,100);
bfill(55,55,2,15);
getch();
closegraph();
}
Q10 Solve the following:
a) Develop a simple text screen saver
using graphic function.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,i,maxx,maxy,key=0;
initgraph(&gd,&gm,"C:\\TurboC3\\
BGI");
maxx=getmaxx();
maxy=getmaxy();
while(!kbhit())
{
for(i=0;i<maxy;i++)
{
cleardevice();
settextstyle(3,0,5);
outtextxy(maxx/2,i,"Hello");
outtext(“bye”);
delay(30);
}
}
getch();
}
b) Draw the moving car on the screen.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\tc\\bgi");
for (int i=0;i<500;i++){
/***CAR BODY ******/
line(50+i,370,90+i,370);
arc(110+i,370,0,180,20);
line(130+i,370,220+i,370);
arc(240+i,370,0,180,20);
line(260+i,370,300+i,370);
line(300+i,370,300+i,350);
line(300+i,350,240+i,330);
line(240+i,330,200+i,300);
line(200+i,300,110+i,300);
line(110+i,300,80+i,330);
line(80+i,330,50+i,340);
line(50+i,340,50+i,370);

/***CAR Windows***/
line(165+i,305,165+i,330);
line(165+i,330,230+i,330);
line(230+i,330,195+i,305);
line(195+i,305,165+i,305);

line(160+i,305,160+i,330);
line(160+i,330,95+i,330);
line(95+i,330,120+i,305);
line(120+i,305,160+i,305);

/**Wheels**/
circle(110+i,370,17);
circle(240+i,370,17);

delay(10);
cleardevice();

line(0,390,639,390); //ROAD
}
getch();
}
c. Draw smiling face animation using graphic
functions.
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int gd=
DETECT,gm,area,temp1,temp2,left=25,t
op=75;
void*p;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setcolor(YELLOW);
circle(50,100,25);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(50,100,YELLOW);

setcolor(BLACK);
setfillstyle(BKSLASK_FILL,BLACK);
fillellipse(44,85,2,6);
fillellipse(56,85,2,6);
ellipse(50,100,205,335,20,9);
ellipse(50,100,205,335,20,10);
ellipse(50,100,205,335,20,11);
area=imagesize(left,top,left+50,top+50);
p=malloc(area);
setcolor(WHITE);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(155,451,"Smilling Face Animation");
setcolor(BLUE);
rectangle(0,0,639,449);
while(!kbhit())
{
temp1=1+random(588);
temp2=1+random(380);
getimage(left,top,left+50,top+50,p);
putimage(left,top,p,XOR_PUT);
putimage(temp1,temp2,p,XOR_PUT);
delay(100);
left=temp1;
top=temp2;
}
getch();
closegraph();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy