Tuesday, May 25, 2010

Draw 3D object and animate on it

#include
#include
#include
#include
#include

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\tc\bgi");
int c=12;
setbkcolor(0);
//setlinestyle(0,1,2);
int t;
while(1)
{
settextstyle(2,0,5);
outtextxy(100,10,"Press L,H ,T,P");
outtextxy(100,30,"Press 1 for Quit");
as:
setcolor(13);
ellipse(380,127,20,152,130,35);
//////////////////////////////rear//////////////////////////

line(490,109,560,142);
line(560,142,569,142);
line(569,142,582,102);
line(582,102,620,92);
line(593,132,617,125);

line(617,124,627,96);
line(620,92,628,97);

line(472,86,602,96);
line(501,113,575,121);
line(443,77,475,80);

line(443,77,432,93);
line(475,80,472,85);
//setcolor(4);
line(593,132,593,137);
line(593,137,600,141);
line(600,141,600,185);
line(600,185,608,192);
line(608,192,608,234);
line(608,234,586,253);
line(586,253,577,248);

///////////////////////// mirror
line(263,112,363,127);
line(193,160,263,112);
line(193,160,220,170);
line(220,170,280,180);
line(280,180,320,185);
line(320,185,363,127);
////////////////////////////////sidemirror
line(340,194,460,169);
line(460,169,519,152);

ellipse(512,144,300,30,10,10);
ellipse(467,143,28,100,50,30);
line(510,128,521,138);
line(435,116,440,171);

// setcolor(4);
////////////////////////////////////////cont//
line(339,194,372,144);
// line(372,140,386,128);
ellipse(454,208,87,123,128,95);
line(372,144,384,128);
int b,x,y;
////////////////////////lower
line(365,298,524,264);
line(365,298,330,310);
line(330,310,323,310);


///////////////////////////////bumper
ellipse(162,221,135,190,90,40);
line(96,193,140,174);
line(140,174,160,168);
line(160,168,192,161);

//////////////////////front
ellipse(75,246,95,190,18,18);
line(57,251,57,286);
//setcolor(4);
ellipse(181,178,232,263,200,137);
ellipse(195,180,256,286,200,137);


ellipse(191,171,228,247,200,100);
ellipse(231,198,234,275,200,80);

//setcolor(9);
//ellipse(195,170,256,286,200,137);
//setcolor(12);

ellipse(196,167,228,246,200,90);
ellipse(231,184,234,276,200,80);


ellipse(191,200,228,246,200,90);
ellipse(228,218,234,276,200,80);

ellipse(258,268,180,220,200,40);
ellipse(178,296,244,355,16,10);

ellipse(238,249,227,250,200,60);


/////////////wheel1
ellipse(302,281,320,77,26,45);
ellipse(290,277,65,162,40,45);
ellipse(278,288,144,212,31,45);

/////////////wheel2
//setcolor(5);
ellipse(302+260,229,328,87,26,45);
ellipse(290+280-7,277-50+2,90,162,40,45);
ellipse(278+270,288-50,144,215,27,45);
b=0;
int v=0;

/////////
ellipse(302+250+v,227+b,295,90,29,41);
ellipse(302+234+v,231+b,245,306,50,40);
//setlinestyle(3,0,3);
ellipse(302+248+v,229+b,0,360,21,30);

ellipse(302+247+v,229+b,0,360,8,10);
setfillstyle(6,11);
//floodfill(302+248+v,230+b,13);
//line(546,201,546,257);
//line(554,201,554,257);
//setcolor(4);

line(546+v,201+b,546+v,220+b);
line(551+v,201+b-2,551+v,220+b);

line(546+v,238+b,546+v,257+b);
line(551+v,238+b+2,551+v,257+b+2);


line(530+v,225+b,541+v,225+b);
line(530+v,230+b,541+v,230);

line(557+v,225+b,570+v,225+b);
line(557+v,230+b,570+v,230+b);



line(563+v,206+b,552+v,222+b);
line(534+v,246+b,543+v,232+b);

line(566+v,210+b,556+v,223+b);
line(536+v,250+b,544+v,238+b);

line(536+v,207+b,546+v,222+b);
line(532+v,213+b,542+v,224+b);

line(556+v,235+b,566+v,247+b);
line(551+v,237+b,563+v,253+b);



////////////////////////////////////////////////////
v=-260;
b=56;
ellipse(302+233+v,221+b,260,60,49,51);
//ellipse(302+234+v,231+b,245,306,50,40);
//setlinestyle(3,0,3);
ellipse(302+243+v,224+b,0,360,28,35);
// line(249,328,269,328);
ellipse(300+245+v,223+b,0,360,10,12);

ellipse(285+249+v,239+b,210,260,30,33);
//floodfill(285+258+v,230+b,12);
b=45;
v=v-4;
line(546+v,201+b,546+v,220+b+2);
line(551+v,201+b,551+v,220+b+2);
b=b+8;
line(546+v,238+b,546+v,257+b+4);
line(551+v,238+b,551+v,257+b+4);
v=v-2;
line(530+v-6,225+b,541+v,225+b);
line(530+v-6,230+b,541+v,230+b);
v=v+5;
line(557+v,225+b,570+v+3,225+b);
line(557+v-1,230+b,570+v+3,230+b);


b=b-5;
v=v-5;
line(565+v+3,206+b,552+v+4,222+b-2);
b=b+15;

line(534+v,246+b,543+v+3,232+b-5);
b=b-10;
line(566+v+7,210+b-5,556+v+4,220+b);
line(536+v-5,250+b,544+v-2,238+b-4);


line(536+v,207+b-8,545+v,222+b-5);
line(531+v,212+b-8,542+v,224+b-2);

line(556+v,235+b,566+v+3,247+b+5);
line(551+v,237+b,563+v+2,253+b+3);

///////////////////lights
ellipse(199,250,144,345,18,8);
line(185,245,206,230);
//setcolor(4);
ellipse(223,234,340,110,8,5);
line(230,237,217,252);
line(206,230,220,229);
//setfillstyle(1,4);

//floodfill(200,240,12);

/////////////////////////////////////
line(90,223,152,236);
line(152,236,137,254);
line(90,223,90,242);

//setfillstyle(10,9);
//floodfill(91,230,14);
ellipse(240,270,104,136,100,60);
ellipse(185,237,120,160,100,60);
ellipse(80,221,357,134,10,10);

line(152,236,168,228);
///////////////////////////////////////////////
line(435,116,440,171);
//////////////////////////////////////////hp
//line(134,185,220,210);
line(134,185,196,160);
line(214,212,318,185);
/////////////////////////////////////////////////light

//setcolor(14);
ellipse(166,247,99,330,8,8);
ellipse(171,243,310,129,7,7);
putpixel(174,250,13);
putpixel(173,251,13);
putpixel(164,239,13);
putpixel(165,238,13);

/////////////////////////////////////////road/////////////////////
setcolor(13);
line(1,430,639,300);
line(1,445,639,315);

line(1,210,93,194);
line(1,195,194,158);

//line(1,170,639,71);
//line(1,170,229,135);
line(520,90,639,71);
line(478,86,639,56);

int c=0;

line(10,194+c,10,208+c);
line(40,189+c,40,204+c);
line(70,183+c,70,198+c);
line(100,176+c,100,190+c);
line(130,170+c,130,177+c);
line(160,166+c,160,168+c);
line(190,160+c,190,161+c);

line(190+330,78+c,190+330,89+c);

line(190+360,72+c,190+360,85+c);
line(190+390,67+c,190+390,81+c);
line(190+420,62+c,190+420,76+c);
line(190+449,57+c,190+449,71+c);



c=236;

line(10,192+c,10,208+c);
line(40,189+c-2,40,204+c-3);
line(70,183+c-3,70,198+c-3);
line(100,176+c-2,100,190+c-2);
line(130,170+c-2,130,177+c+5);
line(160,166+c-3,160,168+c+8);
line(190,160+c-4,190,161+c+9);

line(190+30,156+c-5,190+30,170+c-5);


line(190+30+30,156+c-12,190+30+30,170+c-12);

line(190+90,156+c-18,190+90,170+c-17);

line(190+120,156+c-25,190+120,170+c-25);

line(190+150,156+c-30,190+150,170+c-30);

line(190+180,156+c-37,190+180,170+c-36);


line(190+210,156+c-42,190+210,170+c-42);


line(190+240,156+c-48,190+240,170+c-48);


line(190+270,156+c-55,190+270,170+c-54);


line(190+300,156+c-61,190+300,170+c-61);



line(190+330,78+c+10,190+330,89+c+13);

line(190+360,72+c+11,190+360,85+c+13);
line(190+390,67+c+10,190+390,81+c+10);
line(190+420,62+c+8,190+420,76+c+10);
line(190+449,57+c+8,190+449,71+c+8);




/////////////////road

setcolor(12); /////////////////////////////1

line(1,310,25,306);
line(6,318,30,315);
line(1,310,6,318);
line(25,306,30,314);
int k,m;
k=13*45+19;
m=16*(-8);
//2
setcolor(12);

line(605,310-128,629,306-128);
line(610,318-128,634,315-128);
line(605,310-128,610,318-128);
line(629,306-128,634,314-128);

setcolor(12); //////////////////////////////////3
k=45;
m=-8;
line(46,302,70,298);
line(51,310,75,307);
line(46,302,51,310);
line(70,298,75,306);


setfillstyle(1,0);
floodfill(64,303,12);

setfillstyle(1,14);
floodfill(14,314,12);
floodfill(617,183,12);

setfillstyle(1,0);
floodfill(14,314,12);
floodfill(617,183,12);

setfillstyle(1,14);
floodfill(64,303,12);

t=getch();
if(t=='1')
exit(0);
if(t=='h')
{
sound(710);
delay(500);
nosound();
//break;
}
if(t=='t')
{
while(!kbhit()) {
setfillstyle(1,0);
floodfill(536,213,13);
floodfill(563,213,13);
floodfill(561,244,13);
floodfill(538,244,13);
floodfill(274,295,13);
floodfill(294,295,13);
floodfill(274,265,13);
floodfill(294,265,13);
floodfill(548,250,13);
floodfill(548,214,13);
floodfill(533,228,13);
floodfill(563,228,13);
floodfill(262,281,13);
floodfill(308,281,13);
floodfill(284,251,13);
floodfill(284,295,13);

setfillstyle(1,random(12));

floodfill(200,250,13);
delay(10);
//setfillstyle(1,11);

floodfill(170,250,13);
floodfill(80,230,13);


}

setfillstyle(1,0);

floodfill(200,250,13);
delay(10);
//setfillstyle(1,11);

floodfill(170,250,13);
floodfill(80,230,13);

}


if(t=='l')
{
while(!kbhit())
{

delay(120);
setfillstyle(6,0); //////////////////////////ty
floodfill(536,213,13);
floodfill(563,213,13);
floodfill(561,244,13);
floodfill(538,244,13);
floodfill(274,295,13);
floodfill(294,295,13);
floodfill(274,265,13);
floodfill(294,265,13);

setfillstyle(1,0);
floodfill(64,303,12);

///////////////////////////////////road

setfillstyle(9,0); /////////////////////color
floodfill(81-40+5,419+7,13);
floodfill(151-40,409+7,13);
floodfill(211-40,397+7,13);
floodfill(271-40,380+7,13);
floodfill(331-40,368+7,13);
floodfill(396-40,355+7,13);
floodfill(450-40,345+7,13);
floodfill(510-40,335+7,13);
floodfill(570-40,325+7,13);
floodfill(630-40,312+7,13);


//////////////////////
floodfill(50,197,13);
floodfill(110,177,13);
floodfill(166,165,13);
floodfill(527,86,13);
floodfill(587,71,13);




setfillstyle(6,14); //////////////////////////ty
floodfill(548,250,13);
floodfill(548,214,13);
floodfill(533,228,13);
floodfill(563,228,13);
floodfill(262,281,13);
floodfill(308,281,13);
floodfill(284,251,13);
floodfill(284,295,13);
////////////////////////////////////////road

setfillstyle(9,10);///////////////////////////////////color
floodfill(19,429,13);
floodfill(81,419,13);
floodfill(151,409,13);
floodfill(211,397,13);
floodfill(271,380,13);
floodfill(331,368,13);
floodfill(396,355,13);
floodfill(450,345,13);
floodfill(510,335,13);
floodfill(570,325,13);
floodfill(630,312,13);
//////////////////////////////////////
floodfill(20,197,13);
floodfill(80,187,13);
floodfill(133,174,13);
floodfill(517,86,13);
floodfill(557,81,13);
floodfill(627,70,13);

setfillstyle(1,14);
floodfill(14,314,12);
floodfill(617,183,12);

///////////////////////////////////////
setfillstyle(10,4);
floodfill(302+248,230,13);
floodfill(302+248+v,230+b,13);
///light
setfillstyle(6,11); ///////////

floodfill(200,250,13);

floodfill(170,250,13);
floodfill(80,230,13);

delay(120);

setfillstyle(6,0);/////////////////////ty
floodfill(548,250,13);
floodfill(548,214,13);
floodfill(533,228,13);
floodfill(563,228,13);
floodfill(262,281,13);
floodfill(308,281,13);
floodfill(284,251,13);
floodfill(284,295,13);
/////////////////////////////////////road
setfillstyle(9,0); ///////////////color

floodfill(19,429,13);
floodfill(81,419,13);
floodfill(151,409,13);
floodfill(211,397,13);
floodfill(271,380,13);
floodfill(331,368,13);
floodfill(396,355,13);
floodfill(450,345,13);
floodfill(510,335,13);
floodfill(570,325,13);
floodfill(630,312,13);
///////////////////////////////////////////////////////
floodfill(20,197,13);
floodfill(80,187,13);
floodfill(133,174,13);
floodfill(517,86,13);
floodfill(557,81,13);
floodfill(627,70,13);
/////////////////////////////
setfillstyle(1,0);
floodfill(14,314,12);
floodfill(617,183,12);

setfillstyle(6,10); /////////////ty

floodfill(536,213,13);
floodfill(563,213,13);
floodfill(561,244,13);
floodfill(538,244,13);
floodfill(274,295,13);
floodfill(294,295,13);
floodfill(274,265,13);
floodfill(294,265,13);
////////////////////////////////////////////////road
setfillstyle(9,14);/////////////////////////////////////////color
floodfill(81-40+5,419+7,13);
floodfill(151-40,409+7,13);
floodfill(211-40,397+7,13);
floodfill(271-40,380+7,13);
floodfill(331-40,368+7,13);
floodfill(396-40,355+7,13);
floodfill(450-40,345+7,13);
floodfill(510-40,335+7,13);
floodfill(570-40,325+7,13);
floodfill(630-40,312+7,13);
/////////////////////////////////////////

floodfill(50,197,13);
floodfill(110,177,13);
floodfill(166,165,13);
floodfill(527,86,13);
floodfill(587,71,13);
setfillstyle(1,14);
floodfill(64,303,12);

setfillstyle(9,4);
floodfill(302+248,230,13);
floodfill(302+248+v,230+b,13);

delay(20);
setfillstyle(1,14);

floodfill(200,250,13);

floodfill(170,250,13);
floodfill(80,230,13);

delay(20);
setfillstyle(1,0);

floodfill(200,250,13);

floodfill(170,250,13);
floodfill(80,230,13);




} }




if(t=='p')
{
int n=0;
while(!kbhit())
{
if(n<=60)
n++;
setcolor(0);
rectangle(1+1,-10,90-1,-12+n);
delay(14);

setcolor(9);
rectangle(1,-10,90,-10+n);
if(n==60)
{

outtextxy(10,10,"L-LIGHTS");
outtextxy(10,20,"H-HORN");
//outtextxy(10,30,"T-AllOY");
delay(400);
}


}
setcolor(0);
rectangle(1,-10,90,-10+n);
rectangle(1,-10,90,-11+n);
outtextxy(10,10,"L-LIGHTS");
outtextxy(10,20,"H-HORN");
//outtextxy(10,30,"T-AllOY");

}

}

circle(300,100,3);

nosound();

getch();
}

Draw a ball and animate it.

#include
#include
#include
#include
#include
void main()
{

int d=DETECT,m;
initgraph(&d,&m,"e:\tcc\bgi");
float x=1,y=0.00000,j=.5,count=.1;
float r=15;
setcolor(14);
line(0,215,650,215);
sleep(1);
for(int k=0;k<=7;k++) { for(float i=90;i<270;i+=10) { y=cos(((i*22/7)/180))/j; if(y>0)
y=-y;
x+=5;

setcolor(14);
setfillstyle(1,14);
circle(x,y*100+200,r);
floodfill(x,y*100+200,14);

delay(100);

setcolor(0);
setfillstyle(1,0);
circle(x,y*100+200,r);
floodfill(x,y*100+200,0);

}

j+=count;
count+=.1;

}
getch();
}

Four Quadratic circle with fillup different color for each

#include
#include
#include
#include

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 90, endangle = 179, radius = 100;
int stangle1 = 0, endangle1 = 89;
int stangle2 = 180, endangle2 = 269;
int stangle3 = 270, endangle3 = 360;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;

/* set fill style and draw a pie slice */
setfillstyle(EMPTY_FILL, getmaxcolor());
pieslice(midx, midy, stangle1, endangle1, radius);
setfillstyle(SOLID_FILL, getmaxcolor());
pieslice(midx, midy, stangle, endangle, radius);
setfillstyle(BKSLASH_FILL, getmaxcolor());
pieslice(midx,midy, stangle2, endangle2, radius);
setfillstyle(LINE_FILL, getmaxcolor());
pieslice(midx, midy, stangle3, endangle3, radius);
/* clean up */
getch();
closegraph();
return 0;
}

Bresenham’s Circle Generating Algorithm

#include
#include
#include
#include<.h>
#include
#include
#include
class myCircle
{
private:
int x,y,r,d,x1,y1;
public:
myCircle(); //Constructor
void showCircle();
};

myCircle::myCircle()
{
x=0;y=0;
cout<<" "Enter The Co-Ordinates Of The Circle ":="; cin>>x>>y;
cout<<" "Enter The Radius Of The Circle ":="; cin>>r;
}

void myCircle::showCircle()
{
char *s;
x1=0;y1=r;
d=3-2*r;
while(x1<=y1) { putpixel((x+x1+320),(y+y1+240),5); putpixel((x-x1+320),(y+y1+240),5); putpixel((x+x1+320),(y-y1+240),5); putpixel((x-x1+320),(y-y1+240),5); putpixel((x+y1+320),(y+x1+240),5); putpixel((x-y1+320),(y+x1+240),5); putpixel((x+y1+320),(y-x1+240),5); putpixel((x-y1+320),(y-x1+240),5); if(d<0) d+=4*x1+6; else { d+=4*(x1-y1)+10; y1--; } x1++; } setcolor(5); outtextxy(318+x,235+y,"."); setcolor(15); sprintf(s,"Center(%d,%d)",x,y); outtextxy(20,10,"The Center Is At"); outtextxy(20,20,s); sprintf(s,"Radius=%d",r); outtextxy(20,30,s); getch(); } void main() { int gd=DETECT,gm,i,j,xx=190,xxx=480; clrscr(); myCircle a; char *mess[]={"B","R","E","S","E","N","H","A","M","'","S"," ","C","I","R","C","L","E"," ","A","L","G","O","R","I","T","H","M"}; initgraph(&gd,&gm,"..\bgi"); cleardevice(); rectangle(120,40,320,240); rectangle(320,40,520,240); rectangle(120,240,320,440); rectangle(320,240,520,440); for(i=0,j=27;i<16,j>=14;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showCircle();
// closegraph();
}

Polygon Clipping

#include
#include
#include
#include
#include
#include

int pixels[2][4];
float xn1,xn2,yn1,yn2,x3,y3,m;


void show_quadrant()
{
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(int i=130;i<=510;i+=10) { if(i==320) continue; outtextxy(i,237,"+"); } for(i=50;i<=430;i+=10) { if(i==240) continue; outtextxy(317,i,"-"); } outtextxy(310,230,"O"); outtextxy(530,240,"X"); outtextxy(320,450,"-Y"); outtextxy(100,240,"-X"); outtextxy(320,30,"Y"); } void midpt(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax) { int fl=1; int pixels[2][4],i,j; for(i=0;i<2;i++) for(j=0;j<4;j++) pixels[i][j]=0; if(y1>ymax)
pixels[0][0]=1;
if(y1xmax)
pixels[0][2]=1;
if(x1ymax)
pixels[1][0]=1;
if(y2xmax)
pixels[1][2]=1;
if(x2ymax || y1xmax || x1ymax || y3xmax || x3ymax || y2xmax || x2ymax || y3xmax || x3=12;i++,j--)
{
gotoxy(xx,1);
cout< clearviewport();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
for(i=0;i line(320+x[i],240-y[i],320+x[i+1],240-y[i+1]);
getch();
cleardevice();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
for(i=0;i {
midpt(x[i],y[i],x[i+1],y[i+1],xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}

Object Shading

#include
#include
#include
#include
#include

/* the names of the fill styles supported */
char *fname[] = { "EMPTY_FILL",
"SOLID_FILL",
"LINE_FILL",
"LTSLASH_FILL",
"SLASH_FILL",
"BKSLASH_FILL",
"LTBKSLASH_FILL",
"HATCH_FILL",
"XHATCH_FILL",
"INTERLEAVE_FILL",
"WIDE_DOT_FILL",
"CLOSE_DOT_FILL",
"USER_FILL"
};

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int style, midx, midy;
char stylestr[40];

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;

for (style = EMPTY_FILL; style < USER_FILL; style++)
{
/* select the fill style */
setfillstyle(style, getmaxcolor());

/* convert style into a string */
strcpy(stylestr, fname[style]);

/* fill a bar */
bar3d(0, 0, midx-10, midy, 0, 0);

/* output a message */
outtextxy(midx, midy, stylestr);

/* wait for a key */
getch();
cleardevice();
}

/* clean up */
getch();
closegraph();
return 0;
}

3D Transformation

#include
#include
#include
#include
#include
#include
#include

int gd=DETECT,gm;
double x1,x2,y1,y2;

void show_message()
{
char *mess[]={"-","=","["," ","3","D","-","T","r","a","n","s",
"f","o","r","m","a","t","i","o","n"," ","]","=","-"};
int xx=28,xxx=52,i,j;
_setcursortype(_NOCURSOR);
for(i=0,j=24;i<15,j>=12;i++,j--)
{
gotoxy(xx,1);
cout<>a>>b>>c;
initgraph(&gd,&gm,"..\bgi");
clearviewport();
for(i=0;i<20;i++) { edge[i][0]=edge[i][0]*a; edge[i][1]=edge[i][1]*b; edge[i][2]=edge[i][2]*c; } draw_cube(edge); closegraph(); } void translate(double edge[20][3]) { int a,b,c; int i; cout<<" Enter The Translation Factors "; cin>>a>>b>>c;
initgraph(&gd,&gm,"..\bgi");
clearviewport();
for(i=0;i<20;i++) { edge[i][0]+=a; edge[i][0]+=b; edge[i][0]+=c; } draw_cube(edge); closegraph(); } void rotate(double edge[20][3]) { int ch; int i; double temp,theta,temp1; clrscr(); cout<<"=[ Rotation About ]=-"; cout<<"1:==> X-Axis ";
cout<<"2:==> Y-Axis ";
cout<<"3:==> Z-Axis ";
cout<<"Enter Your Choice "; cin>>ch;
switch(ch)
{
case 1:
cout<<" Enter The Angle "; cin>>theta;
theta=(theta*3.14)/180;
for(i=0;i<20;i++) { edge[i][0]=edge[i][0]; temp=edge[i][1]; temp1=edge[i][2]; edge[i][1]=temp*cos(theta)-temp1*sin(theta); edge[i][2]=temp*sin(theta)+temp1*cos(theta); } draw_cube(edge); break; case 2: cout<<" Enter The Angle "; cin>>theta;
theta=(theta*3.14)/180;
for(i=0;i<20;i++) { edge[i][1]=edge[i][1]; temp=edge[i][0]; temp1=edge[i][2]; edge[i][0]=temp*cos(theta)+temp1*sin(theta); edge[i][2]=-temp*sin(theta)+temp1*cos(theta); } draw_cube(edge); break; case 3: cout<<" Enter The Angle "; cin>>theta;
theta=(theta*3.14)/180;
for(i=0;i<20;i++) { edge[i][2]=edge[i][2]; temp=edge[i][0]; temp1=edge[i][1]; edge[i][0]=temp*cos(theta)-temp1*sin(theta); edge[i][1]=temp*sin(theta)+temp1*cos(theta); } draw_cube(edge); break; } } void reflect(double edge[20][3]) { int ch; int i; clrscr(); cout<<"-=[ Reflection About ]=-"; cout<<"1:==> X-Axis ";
cout<<"2:==> Y-Axis ";
cout<<"3:==> Z-Axis ";
cout<<" Enter Your Choice :="; cin>>ch;
switch(ch)
{
case 1:
for(i=0;i<20;i++) { edge[i][0]=edge[i][0]; edge[i][1]=-edge[i][1]; edge[i][2]=-edge[i][2]; } draw_cube(edge); break; case 2: for(i=0;i<20;i++) { edge[i][1]=edge[i][1]; edge[i][0]=-edge[i][0]; edge[i][2]=-edge[i][2]; } draw_cube(edge); break; case 3: for(i=0;i<20;i++) { edge[i][2]=edge[i][2]; edge[i][0]=-edge[i][0]; edge[i][1]=-edge[i][1]; } draw_cube(edge); break; } } void perspect(double edge[20][3]) { int ch; int i; double p,q,r; clrscr(); cout<<"=[ Perspective Projection About ]=-"; cout<<"1:==> X-Axis ";
cout<<"2:==> Y-Axis ";
cout<<"3:==> Z-Axis ";
cout<<" Enter Your Choice :="; cin>>ch;
switch(ch)
{
case 1:
cout<<" Enter P :="; cin>>p;
for(i=0;i<20;i++) { edge[i][0]=edge[i][0]/(p*edge[i][0]+1); edge[i][1]=edge[i][1]/(p*edge[i][0]+1); edge[i][2]=edge[i][2]/(p*edge[i][0]+1); } draw_cube(edge); break; case 2: cout<<" Enter Q :="; cin>>q;
for(i=0;i<20;i++) { edge[i][1]=edge[i][1]/(edge[i][1]*q+1); edge[i][0]=edge[i][0]/(edge[i][1]*q+1); edge[i][2]=edge[i][2]/(edge[i][1]*q+1); } draw_cube(edge); break; case 3: cout<<" Enter R :="; cin>>r;
for(i=0;i<20;i++) { edge[i][2]=edge[i][2]/(edge[i][2]*r+1); edge[i][0]=edge[i][0]/(edge[i][2]*r+1); edge[i][1]=edge[i][1]/(edge[i][2]*r+1); } draw_cube(edge); break; } closegraph(); } void main() { int choice; double edge[20][3]= { 100,0,0, 100,100,0, 0,100,0, 0,100,100, 0,0,100, 0,0,0, 100,0,0, 100,0,100, 100,75,100, 75,100,100, 100,100,75, 100,100,0, 100,100,75, 100,75,100, 75,100,100, 0,100,100, 0,100,0, 0,0,0, 0,0,100, 100,0,100 }; while(1) { clrscr(); show_message(); cout<<"1:==> Draw Cube ";
cout<<"2:==> Scaling ";
cout<<"3:==> Rotation ";
cout<<"4:==> Reflection ";
cout<<"5:==> Translation ";
cout<<"7:==> Exit ";
cout<<" Enter Your Choice ="; cin>>choice;
switch(choice)
{
case 1:
draw_cube(edge);
break;

case 2:
scale(edge);
break;

case 3:
rotate(edge);
break;

case 4:
reflect(edge);
break;

case 5:
translate(edge);
break;


case 7:
exit(0);

default:
cout<<" Press A Valid Key...!!! ";
getch();
break;
}
closegraph();
}
}

2D Transformation

#include
#include
#include
#include
#define MAXSIZE 3

class D_2
{
private:
double Points[MAXSIZE][MAXSIZE];
void Mult(double [MAXSIZE][MAXSIZE]);
void MultTwoMat(double [MAXSIZE][MAXSIZE],double [MAXSIZE][MAXSIZE]);
void Print();
int x,y;
public:
D_2();
void initialize();
void GetPoints();
void Draw(int);
void DrawCord();
void Translate();
void Rotate();
void Reflect();
void Display(double[MAXSIZE][MAXSIZE]);
void Shear();
void Scale_Fixed();
void Scale_Dir();
};


D_2::D_2()
{
for(int i=0;i>Points[i][j];
}
}
initialize();
}

void D_2::Mult(double temp[MAXSIZE][MAXSIZE])
{
int i,j,k;
double z[MAXSIZE][MAXSIZE];
for(i=0;i=0;i=i+50,j=j-50)
{
fillellipse(i,y/2,2,2);
fillellipse(j,y/2,2,2);
}
for(i=(y/2+50),j=(y/2-50);i<=x,j>=0;i=i+50,j=j-50)
{
fillellipse(x/2,i,2,2);
fillellipse(x/2,j,2,2);
}

outtextxy(x/2+3,y/2+4,"0");

outtextxy(x/2+45,y/2+5,"50");
outtextxy(x/2+95,y/2+5,"100");
outtextxy(x/2+145,y/2+5,"150");
outtextxy(x/2+195,y/2+5,"200");
outtextxy(x/2+245,y/2+5,"250");
outtextxy(x/2+295,y/2+5,"300");

outtextxy(x/2-65,y/2+5,"-50");
outtextxy(x/2-115,y/2+5,"-100");
outtextxy(x/2-165,y/2+5,"-150");
outtextxy(x/2-215,y/2+5,"-200");
outtextxy(x/2-265,y/2+5,"-250");
outtextxy(x/2-315,y/2+5,"-300");

outtextxy(x/2+5,y/2+45,"-50");
outtextxy(x/2+5,y/2+95,"-100");
outtextxy(x/2+5,y/2+145,"-150");
outtextxy(x/2+5,y/2+195,"-200");

outtextxy(x/2+5,y/2-50,"50");
outtextxy(x/2+5,y/2-100,"100");
outtextxy(x/2+5,y/2-150,"150");
outtextxy(x/2+5,y/2-200,"200");

}

void D_2::MultTwoMat(double temp[MAXSIZE][MAXSIZE],double
temp1[MAXSIZE][MAXSIZE])
{
int i,j,k;
double z[MAXSIZE][MAXSIZE];
for(i=0;i>Tx;
cout<<"Enter Translation Factor Along Y-Axis: "; cin>>Ty;
initialize();
for(int j=0;j>ang;
cout<<"Enter Point Of Rotation:X: "; cin>>xr;
cout<<"Y: "; cin>>yr;
initialize();
ang = (PI * ang)/180.0;
setcolor(YELLOW);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(x/2+xr,y/2-yr,2,2);
outtextxy(x/2+xr,y/2-yr-2," Point Of Rotation");

//Transformation Matrix
//Translate arbitrary point to origin then rotate then translate back.
for(int i=0;i>a;
cout<<"b: "; cin>>b;
cout<<"c: "; cin>>c;
if(b!=0)
{
yr = (-c/b);
xr = 0;
double m = -a/b;
ang = atan(m);

}
else
{
yr = 0;
xr = (-c/a);
ang = 22.0/14.0; // Angle = PI/2
}


initialize();
//Transformation Matrix
//Translate arbitrary point to origin then rotate then translate back.
for(int i=0;i>a;
cout<<"b: "; cin>>b;
cout<<"c: "; cin>>c;
cout<<"Enter Shearing Factor Along X-Axis: "; cin>>shx;
if(b!=0)
{
yr = (-c/b);
xr = 0;
double m = -a/b;
ang = atan(m);

}
else
{
yr = 0;
xr = (-c/a);
ang = 22.0/14.0; // Angle = PI/2
}


initialize();

//Transformation Matrix
for(int i=0;i>sx;
cout<<"Enter The Scaling Factor Along Y-Axis: "; cin>>sy;
cout<<"Enter Point Of Scaling:X: "; cin>>xr;
cout<<"Y: "; cin>>yr;
initialize();

//Transformation Matrix
for(int i=0;i>sx;
cout<<"Enter The Scaling Factor Along Y-Axis: "; cin>>sy;
cout<<"Enter The Direction Of Scaling: "; cin>>ang;
ang = (PI * ang)/180.0;
initialize();


//Transformation Matrix
for(int i=0;i>ch;
D1.initialize();
switch(ch)
{
case 1:
D1.GetPoints();
D1.Draw(GREEN);
getch();
break;

case 2:
cleardevice();
D1.Translate();
getch();
closegraph();
break;
case 3:
cleardevice();
D1.Scale_Fixed();
getch();
closegraph();
break;
case 4:
cleardevice();
D1.Rotate();
getch();
closegraph();
break;
case 5:
return;
default:
cout<<"WRONG CHOICE.";
getch();
break;
}
}while(1);
}



Draw arc

#include
#include
#include
#include

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 45, endangle = 135;
int radius = 100;

/* initialize graphics and local
variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();

exit(1); /* terminate with an error code */
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());

/* draw arc */
arc(midx, midy, stangle, endangle, radius);

/* clean up */
getch();
closegraph();
return 0;
}

Three Concentric Circle

#include
#include
#include
#include

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int radius = 100;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());

/* draw the circle */
circle(midx, midy, radius);
circle(midx, midy, radius+25);
circle(midx, midy, radius+50);
/* clean up */
getch();
closegraph();
return 0;
}

Rotation of triangle

#include
#include
#include
#include
#include
#include
#include

void render(float,float,float, float,float,float,
float,float,float,float,float,float);
void initialize(void);
void firstpage(void);
void call_first(void);
float intensity,alpha,thita,tempy,tempz,tempx;
char ch='4';
char ch1='1';
char ch2='1';
int pts1[5][3];
float tx,ty,tz,d=.5;
void assign(float,float,float,float,float,float,float,float,float);
void scan_line(float,float,float,float,float,float,float,float,float);
void drawpyramid(float,float,float,float,float,float);
void call_assign(void);
void display(void);
void tranform(void);
void draw(void);
void drawscale(void);
float pts[5][3]={-100,0,0, 0,0,45, 100,0,0, 0,0,-45, 0,130,0};
float pts2[5][3]={228,273,0, 305,295,0, 428,273,0, 350,250,0
,328,143,0};
float pt[5][3]={-100,0,0, 0,0,45,100,0,0,0,0-45,0,130,0};

void main()
{
int i;
float sx,sy,sz=1;
struct palettetype pal;
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\tc\bgi");
getpalette(&pal);
firstpage();
for(i=16;i>0;i--)
setrgbpalette(pal.colors[i],0,4*i,0);


L1: display();

while(ch1!='4')
{
ch='2';
L2: call_assign();
clearviewport();
gotoxy(1,2);
cout<<"1. Translation"; cout<<"2. Rotation"; cout<<"3. Scaling "; cout<<"4. Back "; ch1=getch(); if(ch1=='4') { clearviewport(); goto L1; } if(ch1=='1') { clearviewport(); while(ch1!='4') { gotoxy(2,2); cout<<"a. X+"; cout<<" b. X-"; cout<<" c. Y+"; cout<<" d. Y- "; cout<<" e. Z+"; cout<<" f. Z-"; cout<<" g. Back"; call_assign(); ch1=getch(); clearviewport(); if(ch1=='g') goto L2; if(ch1=='a') tx=5; if(ch1=='b') tx=-5; if(ch1=='c') ty=5; if(ch1=='d') ty=-5; if(ch1=='e') tz=10; if(ch1=='f') tz=-10; for(i=0;i<5;i++) { pts[i][0]+=tx; pts[i][1]+=ty; pts[i][1]+=tz; } } } if(ch1=='3') { clearviewport(); cout<<"Enter sx:"; cin>>sx;
cout<<"Enter sy:"; cin>>sy;
for(i=0;i<5;i++) { pts2[i][0]=abs(pts2[i][0]*sx+200*(1-sx)); pts2[i][1]=abs(pts2[i][1]*sy+200*(1-sy)); } drawscale(); getch(); } if(ch1=='2') { while(ch2!='4') { clearviewport(); gotoxy(1,2); cout<<"1.X-axis rotation"; gotoxy(1,3); cout<<"2.Y-axis rotation"; gotoxy(1,4); cout<<"3.Z-axis rotation"; gotoxy(1,5); cout<<"4.Back"; ch2=getch(); if(ch2=='4') break; if(ch2=='1') { alpha=0; while(alpha<360) { alpha=alpha+10; thita=(alpha*3.142)/180; initialize(); for(i=0;i<5;i++) { tempy=(pts1[i][1]*cos(thita)+pts1[i][2]*sin(thita)); pts1[i][2]=(pts1[i][1]*sin(thita)-pts1[i][2]*cos(thita)); pts1[i][1]=tempy; } clearviewport(); draw(); delay(100); } } if(ch2=='2') { alpha=0; while(alpha<360) { alpha=alpha+10; thita=(alpha*3.142)/180; initialize(); for(i=0;i<5;i++) { tempz=(pts1[i][2]*cos(thita)+pts1[i][0]*sin(thita)); pts1[i][0]=(pts1[i][2]*sin(thita)-pts1[i][0]*cos(thita)); pts1[i][2]=tempz; } clearviewport(); draw(); delay(100); } } if(ch2=='3') { alpha=0; while(alpha<360) { alpha=alpha+10; thita=(alpha*3.142)/180; initialize(); for(i=0;i<5;i++) { tempx=(pts1[i][0]*cos(thita)-pts1[i][1]*sin(thita)); pts1[i][1]=(pts1[i][0]*sin(thita)+pts1[i][1]*cos(thita)); pts1[i][0]=tempx; } clearviewport(); draw(); delay(100); clearviewport(); draw(); } } } } } closegraph(); restorecrtmode(); } void initialize() { pts1[0][0]=-100; pts1[0][1]=-65; pts1[0][2]=0; pts1[1][0]=0; pts1[1][1]=-65; pts1[1][2]=-45; pts1[2][0]=100; pts1[2][1]=-65; pts1[2][2]=0; pts1[3][0]=0; pts1[3][1]=-65; pts1[3][2]=45; pts1[4][0]=0; pts1[4][1]=65; pts1[4][2]=0; } void firstpage() { clearviewport(); setcolor(WHITE); settextstyle(2,HORIZ_DIR,5); outtextxy(250,15,"A Project on"); setcolor(GREEN); settextstyle(3,HORIZ_DIR,4); outtextxy(170,25,"PYRAMID MODELING"); rectangle(300,120,580,320); rectangle(295,115,585,325); setcolor(6); settextstyle(4,HORIZ_DIR,3); outtextxy(50,100, "OPTIONS"); settextstyle(3,HORIZ_DIR,1); setcolor(11); outtextxy(20,150,"1. VISIBLE SURFACE DETECTION"); outtextxy(20,190,"2. SURFACE RENDERING"); outtextxy(20,230,"3. TRANSFORMATIONS"); outtextxy(20,270,"4. WIREFRAME DISPLAY"); outtextxy(20,310,"5. EXIT"); call_first(); //display(); setcolor(WHITE); getch(); cleardevice(); clearviewport(); } void display(void) { while(ch!='3') { clearviewport(); gotoxy(2,2); cout<<"1. Visible Surface Detection "; gotoxy(2,3); cout<<"2. Surface Rendering"; gotoxy(2,4); cout<<"3. Transformations"; gotoxy(2,5); cout<<"4. Wireframe Display"; gotoxy(2,6); cout<<"5. Exit "; call_assign(); ch=getch(); if(ch=='5') exit(0); clearviewport(); if(ch=='3') break; } } void call_assign(void) { assign(pts[0][0],pts[0][1],pts[0][2],pts[1][0],pts[1][1],pts[1][2],pts[4][ 0],pts[4][1],pts[4][2]); assign(pts[1][0],pts[1][1],pts[1][2],pts[2][0],pts[2][1],pts[2][2],pts[4][ 0],pts[4][1],pts[4][2]); assign(pts[2][0],pts[2][1],pts[2][2],pts[3][0],pts[3][1],pts[3][2],pts[4][ 0],pts[4][1],pts[4][2]); assign(pts[0][0],pts[0][1],pts[0][2],pts[4][0],pts[4][1],pts[4][2],pts[3][ 0],pts[3][1],pts[3][2]); } void call_first(void) { assign(pt[0][0],pt[0][1],pt[0][2],pt[1][0],pt[1][1],pt[1][2],pt[4][0],pt[4 ][1],pt[4][2]); assign(pt[1][0],pt[1][1],pt[1][2],pt[2][0],pt[2][1],pt[2][2],pt[4][0],pt[4 ][1],pt[4][2]); assign(pt[2][0],pt[2][1],pt[2][2],pt[3][0],pt[3][1],pt[3][2],pt[4][0],pt[4 ][1],pt[4][2]); assign(pt[0][0],pt[0][1],pt[0][2],pt[4][0],pt[4][1],pt[4][2],pt[3][0],pt[3 ][1],pt[3][2]); } void drawpyramid(float x1,float y1,float x2,float y2,float x3,float y3) { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void assign(float x1,float y1,float z1,float x2,float y2,float z2,float x3,float y3,float z3) { float A,B,C; float temp,An,Bn,Cn,X,Y,Z; float Xl=-6,Yl=10,Zl=50; float templ; A=y1*(z2-z3)+y2*(z3-z1)+y3*(z1-z2); B=z1*(x2-x3)+z2*(x3-x1)+z3*(x1-x2); C=x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2); temp=sqrt(A*A+B*B+C*C); templ=sqrt(Xl*Xl+Yl*Yl+Zl*Zl); X=(float)Xl/templ; Y=(float)Yl/templ; Z=(float)Zl/templ; An=(A/temp); Bn=(float)B/temp; Cn=(float)C/temp; intensity=15*(An*X+Bn*Y+Cn*Z); if (intensity<0) intensity=0; if (intensity>15)
intensity=15;

z1=55-z1;
x1=x1+300+(d*z1); y1=300-y1-(d*z1);
z2=55-z2;
x2=x2+300+(d*z2); y2=300-y2-(d*z2);
z3=55-z3;
x3=x3+300+(d*z3); y3=300-y3-(d*z3);

if(ch=='1')
{ if(intensity==0) return;
drawpyramid(x1,y1,x2,y2,x3,y3);
return;
}
if(ch=='3')
exit(0);

if(ch=='4')
drawpyramid(x1,y1,x2,y2,x3,y3);

if(ch=='2')
{
if(intensity==0) return;
if ((y1>y2) && (y1>y3) && (y2>y3))
scan_line(x1,y1,z1,x2,y2,z2,x3,y3,z3);

if ((y1>y2) && (y1>y3) && (y3>y2))
scan_line(x1,y1,z1,x3,y3,z3,x2,y2,z2);

if ((y2>y1) && (y2>y3) && (y1>y3))
scan_line(x2,y2,z2,x1,y1,z1,x3,y3,z3);

if ((y2>y1) && (y2>y3) && (y3>y1))
scan_line(x2,y2,z2,x3,y3,z3,x1,y1,z1);

if ((y3>y1) && (y3>y2) && (y1>y2))
scan_line(x3,y3,z3,x1,y1,z1,x2,y2,z2);

if ((y3>y1) && (y3>y2) && (y2>y1))
scan_line(x3,y3,z3,x2,y2,z2,x1,y1,z1);
}
}

void scan_line(float x1,float y1,float z1,float x2,float y2,float
z2,float
x3,float y3,float z3)
{
int i;
float tempx,tempx1,tempy;
float m1,m2,thita,alpha;
alpha=0;

tempx=x1; tempx1=x1; tempy=y1;
m1=(y2-y1)/(x2-x1);
m2=(y3-y1)/(x3-x1);

while((int)tempy!=(int)y2)
{ alpha=alpha+5;
thita=(alpha*3.14/180);
tempx=tempx-1/m1;
tempx1=tempx1-1/m2;
if(tempx {
for(i=0;i+tempx<=tempx1;i++)
{
putpixel(tempx+i,tempy,intensity);
}
}
else
if (tempx1 { for(i=0;i+tempx1<=tempx;i++)
{
putpixel(tempx1+i,tempy,intensity);
}
}
tempy--;
}

m1=(float)(y3-y2)/(x3-x2);

while((int)tempy!=(int)y3)
{
tempx=tempx-1/m1;
tempx1=tempx1-1/m2;
if(tempx {
for(i=0;i+tempx<=tempx1;i++)
putpixel(tempx+i,tempy,intensity);
}
else
{
for(i=0;i+tempx1<=tempx;i++)
putpixel(tempx1+i,tempy,intensity);
}
tempy--;
}
}
void draw()
{ int i;
for(i=0;i<5;i++)
{
pts1[i][2]=50+pts1[i][2]+50;
pts1[i][0]=pts1[i][0]+300+.5*pts1[i][2];
pts1[i][1]=200+65-pts1[i][1]-.5*pts1[i][2];
}
line(pts1[0][0],pts1[0][1],pts1[1][0],pts1[1][1]);
line(pts1[1][0],pts1[1][1],pts1[2][0],pts1[2][1]);
line(pts1[2][0],pts1[2][1],pts1[3][0],pts1[3][1]);
line(pts1[3][0],pts1[3][1],pts1[0][0],pts1[0][1]);
line(pts1[0][0],pts1[0][1],pts1[4][0],pts1[4][1]);
line(pts1[1][0],pts1[1][1],pts1[4][0],pts1[4][1]);
line(pts1[2][0],pts1[2][1],pts1[4][0],pts1[4][1]);
line(pts1[3][0],pts1[3][1],pts1[4][0],pts1[4][1]);
}
void drawscale()
{
line(pts2[0][0],pts2[0][1],pts2[1][0],pts2[1][1]);
line(pts2[1][0],pts2[1][1],pts2[2][0],pts2[2][1]);
line(pts2[2][0],pts2[2][1],pts2[3][0],pts2[3][1]);
line(pts2[3][0],pts2[3][1],pts2[0][0],pts2[0][1]);
line(pts2[0][0],pts2[0][1],pts2[4][0],pts2[4][1]);
line(pts2[1][0],pts2[1][1],pts2[4][0],pts2[4][1]);
line(pts2[2][0],pts2[2][1],pts2[4][0],pts2[4][1]);
line(pts2[3][0],pts2[3][1],pts2[4][0],pts2[4][1]);
}

Draw ellipse

#include
#include
#include
#include
#include
#include
#include
class myCircle
{
private:
int x,y,r,d,x1,y1,minor,major,dtheta,ratio,a,b,x2,y2;
public:
myCircle(); //Constructor
void showCircle();
};

myCircle::myCircle()
{
x=0;y=0;
cout<<" "Enter The Major & Minor Axis Of Ellipse ":="; cin>>major>>minor;
cout<<" "Enter The Center Of The Ellipse ":="; cin>>x>>y;
}

void myCircle::showCircle()
{
char *s;
int ax,ay;
float ar;
x1=1;
ratio=major/minor;
getaspectratio(&ax,&ay);
ar=1;
// ar=ay/ax;
while(x1<=major) { y1=minor*sqrt((1-(x1*x1/major*major))); putpixel((x+x1*ar+320),(y+y1+240),5); putpixel((x+x1*ar+320),(y-y1+240),5); putpixel((x-x1*ar+320),(y-y1+240),5); putpixel((x-x1*ar+320),(y+y1+240),5); dtheta=1/sqrt(x1*x1+y1*y1); // x2=x1+cos(dtheta)-ratio*y1*sin(dtheta); // y2=y1+cos(dtheta)+1/ratio*x1*sin(dtheta); x1++; // y1=y2; } setcolor(5); outtextxy(318+x,235+y,"."); setcolor(15); sprintf(s,"Center(%d,%d)",x,y); outtextxy(20,10,"The Center Is At"); outtextxy(20,20,s); sprintf(s,"Radius=%d",r); outtextxy(20,30,s); getch(); } void main() { int gd=DETECT,gm,i,j,xx=190,xxx=480; clrscr(); myCircle a; char *mess[]={"B","R","E","S","E","N","H","A","M","'","S"," ","C","I","R","C","L","E"," ","A","L","G","O","R","I","T","H","M"}; initgraph(&gd,&gm,"..\bgi"); cleardevice(); rectangle(120,40,320,240); rectangle(320,40,520,240); rectangle(120,240,320,440); rectangle(320,240,520,440); for(i=0,j=27;i<16,j>=14;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showCircle();
}

Bresunhams line drawing algorithm

#include
#include
#include
#include
#include
#include
#include
class lines
{
private:
int length,x1,y1,x2,y2,x,y,dx,dy,wx,wy,w,width;
public:
lines(); //Constructor
void showline();
int sign(int);
};

int lines::sign(int xx)
{
if(xx<0) return -1; if(xx==0) return 0; if(xx>0)
return 1;
return 0;
}
lines::lines()
{
x=0;y=0;
cout<<"Enter The Co-Ordinates (x1,y1) "; cin>>x1>>y1;
cout<<"Enter The Co-Ordinates (x2,y2) "; cin>>x2>>y2;
cout<<"Enter The Width Of The Line "; cin>>width;
}

void lines::showline()
{
char *s;
int s1,s2,ic;
float temp;
x=x1;y=1;
w=width;
if(y2-y1)
wx=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(y2-y1));
if(x2-x1)
wy=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(x2-x1));
dx=abs(x2-x1);
dy=abs(y2-y1);
s1=sign(x2-x1);
s2=sign(y2-y1);
if(dy>dx)
wy=wx;
if(dy>dx)
{
temp=dy;
dy=dx;
dx=temp;
ic=1;
}
else
ic=0;
float d=2*dy-dx;
setcolor(0);
for(int i=1;i<=dx;i++) { for(int j=0;j=0)
{
if(ic==1)
x+=s1;
else
y+=s2;
d-=2*dx;
}
if(ic==1)
y+=s2;
else
x+=s1;
d+=2*dy;
}
setcolor(15);
outtextxy(20,10,"The Points Are:=");
sprintf(s,"A(%d,%d)",x1,y1);
outtextxy(20,20,s);
sprintf(s,"B(%d,%d)",x2,y2);
outtextxy(20,30,s);
getch();
}
void main()
{
int gd=DETECT,gm,i,j,xx=200,xxx=470;
clrscr();
lines a;
char *mess[]={"B","R","E","S","E","N","H","A","M","'","S"," ","L","I","N","E"," ","A","L","G","O","R","I","T","H","M"};
initgraph(&gd,&gm,"..\bgi");
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(i=0,j=25;i<14,j>=12;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showline();
closegraph();
}

DDA Algorithm

Program to implement DDA Line Drawing Algorithm

#include
#include
#include
#include
#include
#include
#include
class lines
{
private:
int length,x1,y1,x2,y2,x,y,dx,dy,wx,wy,w,width;
public:
lines(); //Constructor
void showline();
int sign(int);
};

int lines::sign(int xx)
{
if(xx<0) return -1; if(xx==0) return 0; if(xx>0)
return 1;
return 0;
}
lines::lines()
{
x=0;y=0;
cout<<"Enter The Co-Ordinates (x1,y1) "; cin>>x1>>y1;
cout<<"Enter The Co-Ordinates (x2,y2) "; cin>>x2>>y2;
cout<<"Enter The Width Of The Line "; cin>>width;
}

void lines::showline()
{
char *s,*s1;
if(abs(x2-x1)>=abs(y2-y1))
length=abs(x2-x1);
else
length=abs(y2-y1);
w=width;
wx=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(y2-y1));
wy=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(x2-x1));
dx=(x2-x1)/length;
dy=(y2-y1)/length;
if(dy>dx)
wy=wx;
x=x1+0.5*sign(dx);
y=y1+0.5*sign(dy);
int i=1;
setcolor(0);
while(i<=length) { for(int j=0;j=6;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showline();
closegraph();
}


Draw circle and rectangle using inheritance

#include
#include
#include
#include
#include
class Base
{
public:
void crcle ()
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int radius = 100;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
circle(midx, midy, radius);
getch();
}
void recangle()
{
int gdriver = DETECT, gmode, errorcode;
int left, top, right, bottom;

initgraph(&gdriver, &gmode, "");

errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

left = getmaxx() / 2 - 50;
top = getmaxy() / 2 - 50;
right = getmaxx() / 2 + 50;
bottom = getmaxy() / 2 + 50;

/* draw a rectangle */
rectangle(left,top,right,bottom);
getch();
}
} ;
class Der : public Base
{

public:
void Process()
{
crcle();

recangle();
}
} d ;
int main(void)
{
d.Process();
return 0;
}