Practical List CG
Practical List CG
Practical List CG
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;
}
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");
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);
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 == 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>
/***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;
}