Wednesday, March 12, 2014

Butterfly animation in C graphics

 #include<graphics.h>
#include<conio.h>
#include<stdlib.h>
char *buff1,*buff2;
void main()
{
    int gd=DETECT,gm;
    void *buff;
    int area1,area2,x,y,inc_x=10,inc_y=10;

    initgraph(&gd,&gm,"c:\\tc\\bgi");

    //  structure of butterfly
     ellipse(240,80,115,65,10,40);//body

     circle(240,40,10);   // face
     circle(240,30,2);    // lips
     circle(235,37,2);   // left eye
     circle(245,37,2);   // right eye

     ellipse(215,40,15,100,20,40);    //antenna
     ellipse(265,40,85,165,20,40);    //antenna

     ellipse(160,90,270,0,80,25);   //left wing
     ellipse(160,60,0,90,80,25);

     ellipse(160,75,270,90,15,40);   // wing border
     ellipse(320,75,90,270,15,40);   //wing border

     ellipse(320,90,180,270,80,25); //  right wing
     ellipse(320,60,90,180,80,25);

     setfillstyle(SOLID_FILL,YELLOW);
     floodfill(240,40,WHITE);
     setfillstyle(SOLID_FILL,BLUE);
     floodfill(200,80,WHITE);
     floodfill(290,60,WHITE);
     setfillstyle(SOLID_FILL,RED);
     floodfill(240,80,WHITE);
     setfillstyle(SOLID_FILL,GREEN);
     floodfill(240,110,WHITE);
     //Store first image action
     area1=imagesize(160,0,320,120);
     buff1=malloc(area1);

     getimage(160,0,320,120,buff1);
     clearviewport();
     // second image creation part
     ellipse(240,80,120,60,10,40);

     circle(240,40,10);
     circle(240,30,2);
     circle(235,37,2);
     circle(245,37,2);

     ellipse(215,40,15,100,20,30);
     ellipse(265,40,85,165,20,30);

     ellipse(320,90,180,260,80,40);
     ellipse(320,60,100,180,80,40);
     ellipse(310,75,115,245,15,60);

     ellipse(160,90,280,0,80,40); //  left
     ellipse(160,60,0,80,80,40);
     ellipse(170,75,295,65,15,60);
     setfillstyle(SOLID_FILL,YELLOW);
     floodfill(240,40,WHITE);
     setfillstyle(SOLID_FILL,BLUE);
     floodfill(200,80,WHITE);
     floodfill(290,60,WHITE);
     setfillstyle(SOLID_FILL,RED);
     floodfill(240,80,WHITE);
     setfillstyle(SOLID_FILL,GREEN);
     floodfill(240,110,WHITE);

     //Store second image    (WINGS BEHIND)
     area2=imagesize(160,0,320,140);
     buff2=malloc(area2);
     getimage(160,0,320,140,buff2);
     clearviewport();

     x=10;y=1;
     while(!kbhit())
     {
    x += inc_x;
        if(x > getmaxx()-175)
            inc_x = -5;
        if(x < 10 )
            inc_x = 10;
        y += inc_y;
        if(y > getmaxy()-135)
            inc_y = -10;
        if(y < 10 )
            inc_y = 10;
       putimage(x,y,buff1,COPY_PUT);
       delay(500);
       clearviewport();
       putimage(x,y,buff2,COPY_PUT);
       delay(500);
       clearviewport();
      }
      getch();
      closegraph();
}

Tuesday, February 11, 2014

C graphics program to display Indian National Flag

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

void main()
{
int gd=DETECT,gm,i,j=0;
clrscr();
initgraph(&gd,&gm,"..\\bgi");
delay(100);
setcolor(6);
rectangle(225,125,355,155);
while(j<=30)
{
for (i=0;i<125;i++)
outtextxy(225+i,125+j,"Û");
j++;
}
delay(300);
setcolor(7);
rectangle(225,155,355,185);
j=0;
while(j<=30)
{
for (i=0;i<125;i++)
outtextxy(225+i,155+j,"Û");
j++;
}
delay(300);
setcolor(2);
rectangle(225,185,355,215);
j=0;
while(j<=30)
{
for (i=0;i<125;i++)
outtextxy(225+i,185+j,"Û");
j++;
}
delay(300);
setcolor(9);
rectangle(220,120,225,440);
j=0;
while(j<=312)
{
for (i=0;i<1;i++)
outtextxy(220+i,120+j,"Û");
j++;
}
delay(300);
rectangle(200,440,245,450);
rectangle(190,450,260,460);
rectangle(175,460,275,470);
delay(300);
circle(291,170,13);
delay(300);
line(291,158,291 ,183);
line(279,170,303,170);
line(282,160,300,180);
line(282,180,300,160);
delay(300);
settextstyle(3,0,7);
setcolor(5);
outtextxy(100,5,"INDIAN FLAG");
delay(1000);
getch();
closegraph();
}

Friday, January 24, 2014

C graphics program to display Digital Clock

#include<dos.h>
#include<conio.h>
#include<graphics.h>

void main()
{
int gd=DETECT,gm,hr,min,sec;
char x[3],y[3],z[3];
static struct time *t;
initgraph(&gd,&gm,"..\\bgi");
setcolor(3);
rectangle(150,100,500,200);
setcolor(4);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,5);
outtextxy(257,125,":");
outtextxy(367,125,":");
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
outtextxy(250,50,"Digital Clock");
setcolor(6);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,5);
while( kbhit()==0)
{
setcolor(15);
gettime(t);
hr=t->ti_hour;
min= t->ti_min;
sec=t->ti_sec;
sprintf(x,"%d",hr);
outtextxy(210,125,x);
sprintf(y,"%d",min);
outtextxy(320,125,y);
sprintf(z,"%d",sec);
outtextxy(430,125,z);
delay(1000);
setcolor(0);
outtextxy(430,125,z);
outtextxy(320,125,y);
outtextxy(210,125,x);
}
closegraph();
}



Tuesday, January 21, 2014

Animated Fan Rotation

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd= DETECT, gm,i,m;
initgraph(&gd,&gm,"..\\bgi");
while(kbhit()==0)
{
for(i=80;i>=0;i-=20)
{
setcolor(BLUE);
circle(320,240,65);
circle(320,240,67);
setfillstyle(SOLID_FILL,BLUE);
bar(310,307,330,360);
bar(290,360,350,375);
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
pieslice(320,240,i,i+40,60);
pieslice(320,240,i+120,i+160,60);
pieslice(320,240,i+240,i+280,60);
setcolor(BLUE);
circle(320,240,10);
setfillstyle(SOLID_FILL,BLUE);
floodfill(320,240,BLUE);
delay(100);
setcolor(0);
setfillstyle(SOLID_FILL,0);
pieslice(320,240,i,i+40,60);
pieslice(320,240,i+120,i+160,60);
pieslice(320,240,i+240,i+280,60);
setcolor(BLUE);
circle(320,240,10);
setfillstyle(SOLID_FILL,BLACK);
floodfill(320,240,BLUE);
}
}
closegraph();
}

Analog Clock Program

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd= DETECT, gm,i,j=96,m;
float k=90.5;
initgraph(&gd,&gm,"..\\bgi");

rectangle(210,130,430,350);
circle(320,240,105);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
setcolor(14);
outtextxy(245,85,"Analog Clock");
setfillstyle(SOLID_FILL,4);
floodfill(211,131,15);
setfillstyle(SOLID_FILL,7);
floodfill(320,240,15);
settextstyle(GOTHIC_FONT,HORIZ_DIR,1);
outtextxy(315,130,"12");
outtextxy(370,145,"1");
outtextxy(402,182,"2");
outtextxy(415,228,"3");
outtextxy(398,280,"4");
outtextxy(360,313,"5");
outtextxy(312,325,"6");
outtextxy(264,311,"7");
outtextxy(231,276,"8");
outtextxy(217,229,"9");
outtextxy(230,180,"10");
outtextxy(264,144,"11");
while(kbhit()==0)
{
k -=0.5;
j -=6;
for(i=90;i>=1;i-=6)   // required to start rotation from 12 'O clock position
{
if(kbhit()!=0) exit();
setcolor(15);
setfillstyle(SOLID_FILL,WHITE);
pieslice(320,240,i,i-1,80);
pieslice(320,240,j,j-1,65);
pieslice(320,240,k,k-1,50);
sleep(1);
setcolor(7);   // required to erase the just drawn arms
setfillstyle(SOLID_FILL,7);
pieslice(320,240,i,i-1,80);
pieslice(320,240,j,j-1,65);
pieslice(320,240,k,k-1,50);

}
for(i=359;i>=91;i -=6)
{
if(kbhit()!=0) exit();
setcolor(15);
setfillstyle(SOLID_FILL,WHITE);
pieslice(320,240,i,i-1,80);
pieslice(320,240,j,j-1,65);
pieslice(320,240,k,k-1,50);
sleep(1);
setcolor(7);
setfillstyle(SOLID_FILL,7);
pieslice(320,240,i,i-1,80);
pieslice(320,240,j,j-1,65);
pieslice(320,240,k,k-1,50);
}
if(j<=6) j=365;
if(k<=0.5) k=359;
}
}
closegraph();
}

Character Generation using BitMap

Program for character generation using bitMap

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd= DETECT, gm,i,j,m;
char c[8][8],s[1];  // needed to delcare 's' as string inorder to use it with outtextxy()
initgraph(&gd,&gm,"..\\bgi");
printf("Enter a Character:");
scanf("%s",s);
clearviewport();
outtextxy(1,1,s);
for(i=0;i<textwidth("S");i++)      // any  capital character can be given instead of S
{
for(j=0;j<textheight("S");j++)
{
c[i][j]=getpixel(i,j);
}
}
printf("Enter the size to enlarge the inputted character \n");
scanf("%d",&m);
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
setfillstyle(SOLID_FILL,c[i][j]);
bar(100+(i*m),100+(j*m),100+(i+1)*m,100+(j+1)*m);
}
}
getch();
closegraph();
}


Sample input :
                       Enter a Character: A
                       Enter the size to enlarge the inputted character: 30

Wednesday, December 18, 2013

Program to display a Flying Kite

#include<stdio.h>
#include<dos.h>
#include<graphics.h>

void main()
{
    int gd=DETECT,gm;
    int color;
    int x=10,y=1,inc_x=10,inc_y=10;
    int poly[10];

    initgraph(&gd,&gm,"..\\bgi");

    while(!kbhit())
    {
        x += inc_x;
        if(x > getmaxx()-175)
            inc_x = -5;
        if(x < 0 )
            inc_x = 10;
        y += inc_y;
        if(y > 200)
            inc_y = -10;
        if(y < 0 )
            inc_y = 10;
        cleardevice();
        setcolor(WHITE);
        setbkcolor(CYAN);

        poly[0]=100+x;
        poly[1]=50+y;
        poly[2]=140+x;
        poly[3]=100+y;
        poly[4]=100+x;
        poly[5]=155+y;
        poly[6]=60+x;
        poly[7]=100+y;
        poly[8]=100+x;
        poly[9]=50+y;
        drawpoly(5,poly);
        setfillstyle(SOLID_FILL,LIGHTRED);
        fillpoly(5,poly);
        setcolor(LIGHTBLUE);
        setlinestyle(SOLID_LINE,1,3);
        line(100+x,155+y,100+x,300+y);
        line(100+x,155+y,110+x,300+y);
        line(100+x,155+y,90+x,300+y);
        setcolor(WHITE);
        setlinestyle(SOLID_LINE,1,0);
        line(100,480,100+x,90+y);
        line(100+x,90+y,130+x,100+y);
        line(100+x,90+y,70+x,100+y);
        line(100+x,90+y,100+x,70+y);

        delay(260);
    }
    setlinestyle(SOLID_LINE,0,0);
    closegraph();
}

Saturday, December 14, 2013

Bézier curve fitting program


Bézier curves can be defined for any degree n. A recursive definition for the Bézier curve of degree n expresses it as a point-to-point linear combination (linear interpolation) of a pair of corresponding points in two Bézier curves of degree n − 1.


Let \mathbf{B}_{\mathbf{P}_0\mathbf{P}_1\ldots\mathbf{P}_n} denote the Bézier curve determined by any selection of points P0, P1, ..., Pn. Then to start,
\mathbf{B}_{\mathbf{P}_0}(t) = \mathbf{P}_0 \text{, and}
\mathbf{B}(t) = \mathbf{B}_{\mathbf{P}_0\mathbf{P}_1\ldots\mathbf{P}_n}(t) = (1-t)\mathbf{B}_{\mathbf{P}_0\mathbf{P}_1\ldots\mathbf{P}_{n-1}}(t) + t\mathbf{B}_{\mathbf{P}_1\mathbf{P}_2\ldots\mathbf{P}_n}(t)




 The formula can be expressed explicitly as follows:
\begin{align}  \mathbf{B}(t) = {} &\sum_{i=0}^n {n\choose i}(1 - t)^{n - i}t^i\mathbf{P}_i \\                = {} &(1 - t)^n\mathbf{P}_0 + {n\choose 1}(1 - t)^{n - 1}t\mathbf{P}_1 + \cdots \\                  {} &\cdots + {n\choose n - 1}(1 - t)t^{n - 1}\mathbf{P}_{n - 1} + t^n\mathbf{P}_n,\quad t \in [0,1]\end{align}








 where \scriptstyle {n \choose i} are the binomial coefficients.
For example, for n = 5:

\begin{align}
  \mathbf{B}_{\mathbf{P}_0\mathbf{P}_1\mathbf{P}_2\mathbf{P}_3\mathbf{P}_4\mathbf{P}_5}(t) = \mathbf{B}(t)
    = {} & (1 - t)^5\mathbf{P}_0 + 5t(1 - t)^4\mathbf{P}_1 + 10t^2(1 - t)^3 \mathbf{P}_2 \\
    {} & + 10t^3 (1-t)^2 \mathbf{P}_3 + 5t^4(1-t) \mathbf{P}_4 + t^5 \mathbf{P}_5,\quad t \in [0,1]
\end{align}  


Program to Create Bezier curve

This program  use only  simple array operations to compute polynomial coefficients.
Can be done using factorial too .

 #include <stdio.h>
#include <graphics.h>
#include <math.h>
void bezier (int x1[], int y1[], int no_ctrlpt)
{
   int i,j,row,col;
   double t,xt=0,yt=0; // xt , yt - points to plot curve
   int pcoeff[20][20];  // used to save the coefficents of the polynomial
                                 // created using pascal triangle
// code to find the coefficient of the polynomial
    for(i=0;i<no_ctrlpt;i++)   // no_ctrlpt - number of control points .
                                         // one point is a set of x, y coordinate
    {
        for(j=0;j<=i;j++)
        {
        if(j==0||i==j)
        {
        pcoeff[i][j]=1;
        }
        else
        {
        pcoeff[i][j]=pcoeff[i-1][j-1]+pcoeff[i-1][j];
        }
        }

    }
// code to compute the blend and to fit the curve
        for (t = 0.0; t < 1.0; t += 0.005)

          {
          int k, n= no_ctrlpt-1;
          double blend, term1,term2;

          xt=0.0;
          yt=0.0;
          for(k=0;k<no_ctrlpt;k++)
          {
          if(k==0)   // check needed since if k=0 then pow (t,k) will return domain error
          term1=1; //since anything raise to zero is 1
          else
          term1=pow(t,k);
          term2=pow( 1-t, n-k);
          blend = (double)pcoeff[no_ctrlpt-1][k]*term1*term2; // no_ctrlpt - 1 need since
                                              //only last row  of the generated pascal triangle is needed
                                        
          xt=xt+x1[k]*blend;
          yt=yt+y1[k]*blend;
          }

    putpixel ((int)xt,(int)yt, RED);
  }
    for (i=0; i<no_ctrlpt; i++)
    putpixel (x1[i], y1[i], YELLOW);
   }

 void main()
 {
   int gd = DETECT, gm;
   int x[20], y[20]; // used to store x, y coordinate system
   int i,n;
   initgraph (&gd, &gm, "..\\bgi");
   setbkcolor(BLUE);
   printf("Enter the number of control points");
   scanf("%d",&n);
   printf ("Enter the x- and y-coordinates of the  control points.\n");
   for (i=0; i<n; i++)
   scanf ("%d%d", &x[i], &y[i]);
   bezier (x, y,n);    // calling  function to fit the curve
   getch();
   closegraph();
 }

The above program modified to draw the x and y axis and to draw the Bézier curve is listed below :

#include <stdio.h>
#include <graphics.h>
#include <math.h>
void bezier (int x1[], int y1[], int no_ctrlpt)
{
   int i,j,row,col;
   double t,xt=0,yt=0;
   int pcoeff[20][20];


    for(i=0;i<no_ctrlpt;i++)
    {
        for(j=0;j<=i;j++)
        {
        if(j==0||i==j)
        {
        pcoeff[i][j]=1;
        }
        else
        {
        pcoeff[i][j]=pcoeff[i-1][j-1]+pcoeff[i-1][j];
        }
        }

    }

        for (t = 0.0; t < 1.0; t += 0.005)

          {
          int k, n= no_ctrlpt-1;
          double blend, term1,term2;

          xt=0.0;
          yt=0.0;
          for(k=0;k<no_ctrlpt;k++)
          {
          if(k==0)
          term1=1; //since anything raise to zero is 1
          else
          term1=pow(t,k);
          term2=pow( 1-t, n-k);
          blend = (double)pcoeff[no_ctrlpt-1][k]*term1*term2;
          xt=xt+x1[k]*blend;
          yt=yt+y1[k]*blend;
          }

    putpixel ((int)xt,(int)yt, RED);
  }
    for (i=0; i<no_ctrlpt; i++)
    putpixel (x1[i], y1[i], YELLOW);
   }

 void main()
 {
   int gd = DETECT, gm;
   int x[20], y[20],gap=50;
   char str[5];
   int i,n;
   initgraph (&gd, &gm, "..\\bgi");
   setbkcolor(BLUE);
   line(5,getmaxy()-10,getmaxx()-5,getmaxy()-10);
   line(3,8,3,getmaxy()-8);

   for( i= gap;i<getmaxx();i=i+gap)    // gap required for spacing of co-ordinate values
   {
   outtextxy(i,getmaxy()-14,"|");
   itoa(i,str,10);
   outtextxy(i,getmaxy()-8,str);
   }
   for( i=gap;i<getmaxy();i=i+gap)
   {
   outtextxy(1,getmaxy()-i,"-");
   itoa(i,str,10);
   outtextxy(8,getmaxy()-i,str);
   }

   printf("Enter the number of control points");
   scanf("%d",&n);
   printf ("Enter the x- and y-coordinates of the four control points.\n");
   printf(" eg: if 3 control point then enter 50 50 100 100 150 50 \n");
   for (i=0; i<n; i++)
   {
   scanf ("%d%d", &x[i], &y[i]);
   y[i]= getmaxy()-y[i]; // this step required for shifting (0,0) position from top left to bottom left
   }
   bezier (x, y,n);
   getch();
   closegraph();
 }


Sample Input :
           if 3 control points : 200 200 250 150 300 200
           if 4 control points : 200 200 250 150 300 200 350 250



Saturday, December 7, 2013

C graphics program to fill a rectangle using 4 -connected floodfill algorithm

#include <graphics.h>
#include <conio.h>
void flood_fill4(int x,int y,int newColor,int oldColor)
{
int c;
c=getpixel(x,y);
if(c==oldColor)
{
setcolor(newColor);
putpixel (x,y,newColor);
delay(10);
flood_fill4(x+1,y,newColor,oldColor);
flood_fill4(x,y+1,newColor,oldColor);
flood_fill4(x-1,y,newColor,oldColor);
flood_fill4(x,y-1,newColor,oldColor);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
rectangle(250,200,300,250);
flood_fill4(251,201,4,0); // 4(red) - newColor  0(black) - oldColor
          // x y point should be one greater than rectangle border
getch();
closegraph();
}

Monday, October 28, 2013

C graphics program to shear a rectangle

#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm,option,xref,yref;
int i,maxx,maxy,x1,y1,x2,y2,x3,y3,x4,y4,gap=50;
float shx=0.0,shy=0.0;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("enter the  endpoints of the top of the rectangle (x1,y1) & (x2,y2):");
 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
 printf("enter the endpoints of bottom of the rectangle (x3,y3) & (x4,y4)");
 scanf("%d%d%d%d",&x3,&y3,&x4,&y4);
 printf(" Enter the axis to shear\n");
 printf(" 1 - X axis Shear\n");
 printf(" 2 - Y axis shear\n");
 scanf("%d",&option );
 if(option==1)
 {
 printf("enter the value for x-axis shear( can be fraction too):");
 scanf("%f",&shx);
 }
 else
 {
 printf("enter the value for y-axis shear( can be fraction too):");
 scanf("%f",&shy);
 }

clearviewport();
maxx= getmaxx();
maxy=getmaxy();
line(3,maxy-1,maxx-5,maxy-1);
line(5,5,5,maxy-3);

for( i= 0;i<maxx-5;i=i+gap)              // code to display co-ordinates
{
outtextxy(i+3,maxy-7,"|");
itoa(i,str,10);
outtextxy(i,maxy-10,str);
}
for( i= maxy;i>0;i=i-gap)
{
outtextxy(3,i,"-");
itoa(maxy-i,str,10);
outtextxy(9,i,str);

}
 setcolor(50);                // drawing rectangle using endpoints
 line(x1,maxy-y1,x2,maxy-y2);
 line(x3,maxy-y3,x4,maxy-y4);
 line(x1,maxy-y1,x3,maxy-y3);
 line(x2,maxy-y2,x4,maxy-y4);
 outtextxy(10,10,"hit any key to see the shearing effect" );
 getch();
 setcolor(0);    // to hide the rectangle drawn
  line(x1,maxy-y1,x2,maxy-y2);
  line(x3,maxy-y3,x4,maxy-y4);
  line(x1,maxy-y1,x3,maxy-y3);
  line(x2,maxy-y2,x4,maxy-y4);

   setcolor(58);    // to redraw the rectangle
 if(option==1)
  {
  // shearing about x axis so only  points x1 and x2 need to be recomputed
  line(x1+shx*y1,maxy-y1,x2+shx*y2,maxy-y2);
  line(x3,maxy-y3,x4,maxy-y4);
  line(x1+shx*y1,maxy-y1,x3,maxy-y3);
  line(x2+shx*y2,maxy-y2,x4,maxy-y4);

 }
 else
 {
   // shearing about y axis so only  points y2 and y4 need to be recomputed
  line(x1,maxy-y1,x2,maxy-(y2+shy*x2));
  line(x3,maxy-y3,x4,maxy-(y4+shy*x4));
  line(x1,maxy-y1,x3,maxy-y3);
  line(x2,maxy-(y2+shy*x2),x4,maxy-(y4+shy*x4));

 }
 getch();
 closegraph();
}

 
        Original Image             Shearing w.r.t  Y - axis     Shearing w.r.t  X - axis   

Sample Input
    x1 ,y1, x2, y2 : 200 200 300 200
    x3, y3, x4, y4 : 200 100 300 100  
x- shear value: any value even fractional ( eg 0.4)
y- shear value: any value even fractional ( eg 0.5)

Friday, October 25, 2013

C graphics program to display smiling face

/* Computer C Graphics Program to display smiling face */



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

void main()
{
   int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
   void *p;

   initgraph(&gd,&gm,"..\\bgi");

   setcolor(YELLOW);
   circle(50,100,25);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(50,100,YELLOW);

   setcolor(RED);
   setfillstyle(SOLID_FILL,RED);
   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,"Smiling 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(200);
      left = temp1;
      top = temp2;
      delay(200);
   }

   getch();
   closegraph();
}


Program to display circles within circle

/* Program to display circles within circle */



#include<graphics.h>
#include<conio.h>
#include<stdio.h>

void main()
{
   int gd = DETECT, gm, x, y, color, angle = 0;
   struct arccoordstype a, b;

   initgraph(&gd, &gm, "..\\bgi");
   delay(2000);

   while(angle<=360)

   {
   setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,50);
      setcolor(BLUE);
      getarccoords(&a);
      circle(a.xstart,a.ystart,25);
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
      setcolor(RED);
      getarccoords(&a);
      circle(a.xstart,a.ystart,25);
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
      getarccoords(&a);
      setcolor(GREEN);
      circle(a.xstart,a.ystart,25);
      angle = angle+5;
      delay(50);
   }

   getch();
   closegraph();
   }

Monday, October 21, 2013

C graphics program to simulate a Traffic light


/* This program will simulate a traffic light */

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

main()
{
   int gd = DETECT, gm, midx, midy;

   initgraph(&gd, &gm, "..\\bgi");

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

   setcolor(RED);
   settextstyle(SCRIPT_FONT, HORIZ_DIR, 3);
   settextjustify(CENTER_TEXT, CENTER_TEXT);
   outtextxy(midx, midy-10, "Traffic Light Simulation");
   outtextxy(midx, midy+10, "Press any key to start");
   getch();
   cleardevice();
   setcolor(WHITE);
   settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy-50, 22);
   setfillstyle(SOLID_FILL,RED);
   floodfill(midx, midy-50,WHITE);
   setcolor(BLUE);
   outtextxy(midx,midy-50,"STOP");
   delay(1000);
   graphdefaults();
   cleardevice();
   setcolor(WHITE);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy, 20);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(midx, midy,WHITE);
   setcolor(BLUE);
   outtextxy(midx-18,midy-3,"READY");

   delay(2000);
   cleardevice();
   setcolor(WHITE);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy+50, 22);
   setfillstyle(SOLID_FILL,GREEN);
   floodfill(midx, midy+50,WHITE);
   setcolor(BLUE);
   outtextxy(midx-7,midy+48,"GO");
   setcolor(RED);
   settextstyle(SCRIPT_FONT, HORIZ_DIR, 4);
   outtextxy(midx-150, midy+100, "Press any key to exit...");

   getch();
   closegraph();
   return 0;
}

C graphics program to simulate Solar system

 /* This program will simulate a Solar system */

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void main()
{
int i=0,j=260,k=30,l=150,m=90;
int n=230,o=10,p=280,q=220;
float pi=3.1424,a,b,c,d,e,f,g,h,z;
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
outtextxy(0,10,"SOLAR SYSTEM");
outtextxy(500,10,"press any key...");
circle(320,240,20);               /* sun */
setfillstyle(1,4);
floodfill(320,240,15);
outtextxy(310,237,"sun");
circle(260,240,8);
setfillstyle(1,2);
floodfill(258,240,15);
floodfill(262,240,15);
outtextxy(240,220,"mercury");
circle(320,300,12);
setfillstyle(1,1);
floodfill(320,298,15);
floodfill(320,302,15);
outtextxy(335,300,"venus");
circle(320,160,10);
setfillstyle(1,5);
floodfill(320,161,15);
floodfill(320,159,15);
outtextxy(332,150, "earth");
circle(453,300,11);
setfillstyle(1,6);
floodfill(445,300,15);
floodfill(448,309,15);
outtextxy(458,280,"mars");
circle(520,240,14);
setfillstyle(1,7);
floodfill(519,240,15);
floodfill(521,240,15);
outtextxy(500,257,"jupiter");
circle(169,122,12);
setfillstyle(1,12);
floodfill(159,125,15);
floodfill(175,125,15);
outtextxy(130,137,"saturn");
circle(320,420,9);
setfillstyle(1,13);
floodfill(320,417,15);
floodfill(320,423,15);
outtextxy(310,400,"urenus");
circle(40,240,9);
setfillstyle(1,10);
floodfill(38,240,15);
floodfill(42,240,15);
outtextxy(25,220,"neptune");
circle(150,420,7);
setfillstyle(1,14);
floodfill(150,419,15);
floodfill(149,422,15);
outtextxy(120,430,"pluto");
getch();
while(!kbhit())             /*animation*/
{
a=(pi/180)*i;
b=(pi/180)*j;
c=(pi/180)*k;
d=(pi/180)*l;
e=(pi/180)*m;
f=(pi/180)*n;
g=(pi/180)*o;
h=(pi/180)*p;
z=(pi/180)*q;
cleardevice();
circle(320,240,20);
setfillstyle(1,4);
floodfill(320,240,15);
outtextxy(310,237,"sun");

circle(320+60*sin(a),240-35*cos(a),8);
setfillstyle(1,2);
pieslice(320+60*sin(a),240-35*cos(a),0,360,8);
circle(320+100*sin(b),240-60*cos(b),12);
setfillstyle(1,1);
pieslice(320+100*sin(b),240-60*cos(b),0,360,12);
circle(320+130*sin(c),240-80*cos(c),10);
setfillstyle(1,5);
pieslice(320+130*sin(c),240-80*cos(c),0,360,10);
circle(320+170*sin(d),240-100*cos(d),11);
setfillstyle(1,6);
pieslice(320+170*sin(d),240-100*cos(d),0,360,11);
circle(320+200*sin(e),240-130*cos(e),14);
setfillstyle(1,7);
pieslice(320+200*sin(e),240-130*cos(e),0,360,14);
circle(320+230*sin(f),240-155*cos(f),12);
setfillstyle(1,12);
pieslice(320+230*sin(f),240-155*cos(f),0,360,12);
circle(320+260*sin(g),240-180*cos(g),9);
setfillstyle(1,13);
pieslice(320+260*sin(g),240-180*cos(g),0,360,9);
circle(320+280*sin(h),240-200*cos(h),9);
setfillstyle(1,10);
pieslice(320+280*sin(h),240-200*cos(h),0,360,9);
circle(320+300*sin(z),240-220*cos(z),7);
setfillstyle(1,14);
pieslice(320+300*sin(z),240-220*cos(z),0,360,7);
delay(20);
i++;
j++;
k++;
l++;
m++;
n++;
o++;
p++;
q+=2;
}
getch();
}

program using cohen sutherland line clipping algorithm

/* This program will clip a line using cohen sutherland line clipping algorithm */

#include<stdio.h>
#include<graphics.h>
#include<conio.h>

enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };

void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{

int gd,gm;
unsigned int code0,code1,codeout;
int accept = 0, done=0;

code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

do{
if(!(code0 | code1))
{ accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);

if(accept) line(x0,y0,x1,y1);

rectangle(xwmin,ywmin,xwmax,ywmax);


}


int calcode (x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;

if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)
code |= LEFT;

return(code);
}


void main()
{

float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;

clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("\n\n\tEnter the co-ordinates of Line :");

printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);

printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);


printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clearviewport();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clearviewport();

lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();

}

Sample Input :

                   x1 ,y1        : 50 50
                   x2 ,y2        : 100 100
                  xwin , ymin : 75 75
                  xmax , ymax : 150 150

C graphics program for reflection about X- axis, Y axis,Y=X axis

/* This program will  reflect an object ( triangle ) about X- axis, Y- axis and Y=X axis */

#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp,option;
int x1dash,x2dash,x3dash,y1dash,y2dash,y3dash;
double theta;
float m;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of the triangle\n");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the triangle\n");
scanf("%d  %d",&x2,&y2);
printf("Enter third co-ords of the triangle\n");
scanf("%d  %d",&x3,&y3);

xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);

setcolor(255);
printf("Reflection about \n");
printf("X axis :  1\n");
printf("Y axis : 2\n");
printf("X=Y axis : 3\n");
printf(" Enter the option (1-3):");
scanf("%d",&option);
switch( option)
{
case 1:  y1=-y1; y2=-y2;y3=-y3; break;

case 2:  x1=-x1;x2=-x2;x3=-x3;break;

case 3:  y1=-y1; y2=-y2;y3=-y3;
     theta= ((double) 90 *3.14f )/(double)180;
    x1dash=x1*cos(theta)-y1*sin(theta);
    x2dash=x2*cos(theta)-y2*sin(theta);
    x3dash=x3*cos(theta)-y3*sin(theta);
    y1dash=x1*sin(theta)+y1*cos(theta);
    y2dash=x2*sin(theta)+y2*cos(theta);
    y3dash=x3*sin(theta)+y3*cos(theta);
    x1=x1dash;x2=x2dash;x3=x3dash;
    y1=y1dash;y2=y2dash;y3=y3dash;

}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);

getch();
closegraph();
}

Sample Input :

  x1 ,y1   :  50   50
  x2, y2   : 100 100
  x3 ,y3   :150   50   

Tuesday, October 8, 2013

Program using Bresenham's line drawing algorithm ( all quadrants)

/* This program will draw x- y coordinate system and will draw lines in all quadrants using   Bresenham's line drawing algorithm  */

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x,y,dy,dx,p,gap=50,temp;
float m;
char str[3];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of line\n");
scanf("%d",&x1);
scanf("%d",&y1);
printf("Enter second co-ords of line\n");
scanf("%d",&x2);
scanf("%d",&y2);
if (y1>y2)    // needed for -ve slope to work
{temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
}
dy=abs(y2-y1);
dx=abs(x2-x1);
m=(float)(dy)/(dx);
dy=abs(dy);
dx=abs(dx);
xmid= getmaxx()/2;
ymid =getmaxy()/2;
x2=x2+xmid;
y2=ymid-y2;
x=x1;y=y1;
if(m>1.0)
{
x=y1;
y=x1;
}


line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}

x=x+xmid;
y=ymid -y;
p=2*dy-dx;
while(x<x2)
{
if(p>=0)
{
putpixel(x,y,5);

y=y-1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,5);
p=p+2*dy;
}
x=x+1;
}
getch();
closegraph();
}

C graphics program to rotate an object about the origin


/* This program will  rotate an object ( triangle) about the origin */

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp;
int x1dash,x2dash,x3dash,y1dash,y2dash,y3dash;
float m;
double theta;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of the triangle\n");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the triangle\n");
scanf("%d  %d",&x2,&y2);
printf("Enter third co-ords of the triangle\n");
scanf("%d  %d",&x3,&y3);

xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);

printf("Enter the degree to rotate");
scanf("%lf",&theta);
theta= ((float) theta *3.14f )/(float)180;
x1dash=x1*cos(theta)-y1*sin(theta);
x2dash=x2*cos(theta)-y2*sin(theta);
x3dash=x3*cos(theta)-y3*sin(theta);
y1dash=x1*sin(theta)+y1*cos(theta);
y2dash=x2*sin(theta)+y2*cos(theta);
y3dash=x3*sin(theta)+y3*cos(theta);

line(x1dash+xmid,ymid-y1dash,x2dash+xmid,ymid-y2dash);
line(x2dash+xmid,ymid-y2dash,x3dash+xmid,ymid-y3dash);
line(x3dash+xmid,ymid-y3dash,x1dash+xmid,ymid-y1dash);

getch();
closegraph();
}


Sample Input :

  x1 ,y1   :  50   50
  x2, y2   : 100 100
  x3 ,y3   :150   50   

C graphics program to rotate an object using arbitrary point

/* This program will  rotate an object ( triangle) using an arbitrary point */

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp,xr,yr;
int x1dash,y1dash,x2dash,y2dash,x3dash,y3dash;
float m;
double theta;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of the triangle\n");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the triangle\n");
scanf("%d  %d",&x2,&y2);
printf("Enter third co-ords of the triangle\n");
scanf("%d  %d",&x3,&y3);

xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);

printf("Enter the degree to rotate");
scanf("%lf",&theta);
theta= ((float) theta *3.14f )/(float)180;  // converting theta to radian
printf("Enter the arbitrary point to rotate");
scanf("%d%d",&xr,&yr);
x1dash=xr+(x1-xr)*cos(theta)-(y1-yr)*sin(theta);
x2dash=xr+(x2-xr)*cos(theta)-(y2-yr)*sin(theta);
x3dash=xr+(x3-xr)*cos(theta)-(y3-yr)*sin(theta);

y1dash=yr+(x1-xr)*sin(theta)+(y1-yr)*cos(theta);
y2dash=yr+(x2-xr)*sin(theta)+(y2-yr)*cos(theta);
y3dash=yr+(x3-xr)*sin(theta)+(y3-yr)*cos(theta);

line(x1dash+xmid,ymid-y1dash,x2dash+xmid,ymid-y2dash);
line(x2dash+xmid,ymid-y2dash,x3dash+xmid,ymid-y3dash);
line(x3dash+xmid,ymid-y3dash,x1dash+xmid,ymid-y1dash);

getch();
closegraph();
}

Tuesday, October 1, 2013

C graphics program to scale a triangle ( in all quadrants)

 /* This program will  scale an object ( triangle )   in all quadrants  */

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp;
float m,sxscale,syscale;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of the triangle ( eg : 50 50 ) \n");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the triangle ( eg : 100 100 )\n");
scanf("%d  %d",&x2,&y2);
printf("Enter third co-ords of the triangle ( eg : 150 150 ) \n");
scanf("%d  %d",&x3,&y3);

xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);
printf("\nEnter the scaling factor for x axis");
scanf("%f",&sxscale);
printf("\nEnter the scaling factor for y axis");
scanf("%f",&syscale);

setcolor(0);  // to hide drawn triangle
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);
setcolor(255);

line(x1*sxscale+xmid,ymid-y1*syscale,x2*sxscale+xmid,ymid-y2*syscale);
line(x2*sxscale+xmid,ymid-y2*syscale,x3*sxscale+xmid,ymid-y3*syscale);
line(x3*sxscale+xmid,ymid-y3*syscale,x1*sxscale+xmid,ymid-y1*syscale);

getch();
closegraph();
}


Sample Input :

  x1 ,y1   :  50   50
  x2, y2   : 100 100
  x3 ,y3   :150   50