Thursday, 22 June 2023

C Programming Chapter 10 Graphics

 Chapter Graphics

Q.1. Write a program to show a rectangle in C graphics.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

          {

          int gd=DETECT,gm;

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

          rectangle(10,10,300,400);

          getch();

          closegraph();

          restorecrtmode();

          }

 

Q.2. Write a program to show a circle in C graphics.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

          {

          int gd=DETECT,gm;

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

          circle(250,200,60);

          getch();

          closegraph();

          restorecrtmode();

          }

 

Q.3. Write a program to make filled rectangle.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

          {

          int gd=DETECT,gm;

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

          bar(10,10,300,400);

          getch();

          closegraph();

          restorecrtmode();

          }

 

Q. Write a program to place a text middle of the screen.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

          {

          int gd=DETECT,gm;

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

          outtextxy(180,240,"Technologica Computer Education Society");

          getch();

          closegraph();

          restorecrtmode();

          }

 

Q. Write a program to apply effect on a text.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

          {

          int gd=DETECT,gm;

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

          outtextxy(40,10,"Technologica Computer Education Society");

          settextstyle(1,0,1);

          outtextxy(40,20,"Technologica Computer Education Society");

          settextstyle(2,0,7);

          outtextxy(40,45,"Technologica Computer Education Society");

          settextstyle(3,0,1);

          outtextxy(40,60,"Technologica Computer Education Society");

          settextstyle(4,0,1);

          outtextxy(40,80,"Technologica Computer Education Society");

          settextstyle(5,0,1);

          outtextxy(40,100,"Technologica Computer Education Society");

          settextstyle(6,0,1);

          outtextxy(40,120,"Technologica Computer Education Society");

          settextstyle(7,0,1);

          outtextxy(40,140,"Technologica Computer Education Society");

          settextstyle(8,0,1);

          outtextxy(40,160,"Technologica Computer Education Society");

          settextstyle(9,0,1);

          outtextxy(40,180,"Technologica Computer Education Society");

          settextstyle(10,0,1);

          outtextxy(40,210,"Technologica Computer Education Society");

          settextstyle(11,0,1);

          outtextxy(40,260,"Technologica Computer Education Society");

          settextstyle(12,0,1);

          outtextxy(40,280,"Technologica Computer Education Society");

          settextstyle(13,1,1);

          outtextxy(10,10,"Technologica Computer Education Society");

          getch();

          closegraph();

          restorecrtmode();

          }

 

Q. Write a program to change the color of text.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

          {

          int gd=DETECT,gm;

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

          setcolor(1);

          outtextxy(40,10,"Technologica Computer Education Society");

          settextstyle(1,0,1);

          setcolor(2);

          outtextxy(40,20,"Technologica Computer Education Society");

          settextstyle(2,0,7);

          setcolor(3);

          outtextxy(40,45,"Technologica Computer Education Society");

          settextstyle(3,0,1);

          setcolor(4);

          outtextxy(40,60,"Technologica Computer Education Society");

          settextstyle(4,0,1);

          setcolor(5);

          outtextxy(40,80,"Technologica Computer Education Society");

          settextstyle(5,0,1);

          setcolor(6);

          outtextxy(40,100,"Technologica Computer Education Society");

          settextstyle(6,0,1);

          setcolor(7);

          outtextxy(40,120,"Technologica Computer Education Society");

          settextstyle(7,0,1);

          setcolor(8);

          outtextxy(40,140,"Technologica Computer Education Society");

          settextstyle(8,0,1);

          setcolor(9);

          outtextxy(40,160,"Technologica Computer Education Society");

          settextstyle(9,0,1);

          setcolor(10);

          outtextxy(40,180,"Technologica Computer Education Society");

          settextstyle(10,0,1);

          setcolor(11);

          outtextxy(40,210,"Technologica Computer Education Society");

          settextstyle(11,0,1);

          setcolor(12);

          outtextxy(40,260,"Technologica Computer Education Society");

          settextstyle(12,0,1);

          setcolor(13);

          outtextxy(40,280,"Technologica Computer Education Society");

          settextstyle(13,1,1);

          setcolor(14);

          outtextxy(10,10,"Technologica Computer Education Society");

          setcolor(15);

          settextstyle(0,0,0);

          outtextxy(40,310,"Technologica Computer Education Society");

          getch();

          closegraph();

          restorecrtmode();

          }

 

Q. Write a program to draw ellipse in c graphics.

#include<graphics.h>

#include<conio.h>

 main()

{

   int gd = DETECT, gm;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   ellipse(100, 100, 0, 360, 50, 25);

   getch();

   closegraph();

   return 0;

}

 

Q. write  a program for   fillellipse

#include <graphics.h>

#include <conio.h>

 int main()

{

   int gd = DETECT, gm;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   fillellipse(100, 100, 50, 25);

   getch();

   closegraph();

   return 0;

}

Q. Write a program for arc in c graphics

#include <graphics.h>
#include <conio.h>
 main()
{
   int gd = DETECT, gm;
   initgraph(&gd, &gm, "C:\\TC\\BGI");
   arc(100, 100, 0, 135, 50);
   getch();
   closegraph();
   return 0;
}

 

Q. Write a program for draw polygone

#include <graphics.h>
#include <conio.h>
 main()
{
   int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
   initgraph(&gd, &gm, "C:\\TC\\BGI");
   drawpoly(4, points);
   getch();
   closegraph();
   return 0;
}

 

Q. Write a program for fillpoly

#include <graphics.h>
#include <conio.h>
 main()
{
   int gd=DETECT,gm,points[]={320,150,440,340,230,340,320,150};
   initgraph(&gd, &gm, "C:\\TC\\BGI");
   fillpoly(4, points);
   getch();
   closegraph();
   return 0;
}
 

Q. Write a program for flood fill

#include <graphics.h>
#include <conio.h>
main()
{
   int gd = DETECT, gm;
   initgraph(&gd, &gm, "C:\\TC\\BGI");
   setcolor(RED);
   circle(100,100,50);
   floodfill(100,100,RED);
   getch();
   closegraph();
   return 0;
}

 

Q. Write a program in C graphics to make a loading screen.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void main()

          {

          int gd=DETECT,gm,i;

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

          setcolor (BLUE);

          setcolor(GREEN);

          rectangle(98,48,402,302);

          setfillstyle(1,15);

          bar(100,50,400,300);

          setfillstyle(1,GREEN);

          bar(150,100,450,350);

          setfillstyle(1,RED);

          bar(200,150,500,400);

          setcolor(15);

          settextstyle(5,0,4);

          outtextxy(220,320,"Software Pro 1.0");

          settextstyle(0,0,0);

          for(i=0;i<650;i++)

                   {

                   setfillstyle(1,6);

                   bar(i,450,i,470);

                   setcolor(15);

                   outtextxy(200,460,"L  o  a  d   i  n  g........");

                    delay(10);

                   }

          getch();

          closegraph();

          restorecrtmode();

          }

 

 

Q. Write a program to make count down timer in C graphics.

 

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

int main()

{

   int gd = DETECT, gm, i;

   char a[5];

   initgraph( &gd, &gm, "C:\\TC\\BGI");

   settextjustify( CENTER_TEXT, CENTER_TEXT );

   settextstyle(DEFAULT_FONT,HORIZ_DIR,3);

   setcolor(RED);

   for (i = 30; i >=0; i--)

   {

      sprintf(a,"%d",i);

      outtextxy(getmaxx()/2, getmaxy()/2, a);

      delay(1000);

       if ( i == 0 )

         break;

      cleardevice();

   }

   getch();

   closegraph();

   return 0;

}

 

 

 

Q. Write a program to make a bar chart in C graphics.

 

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

 main()

{

   int gd = DETECT, gm;

 

   initgraph(&gd, &gm, "C:\\TC\\BGI");

 

   setcolor(YELLOW);

   rectangle(0,30,639,450);

   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);

   setcolor(WHITE);

   outtextxy(275,0,"Bar Chart");

   setlinestyle(SOLID_LINE,0,2);

   line(100,420,100,60);

   line(100,420,600,420);

   line(90,70,100,60);

   line(110,70,100,60);

   line(590,410,600,420);

   line(590,430,600,420);

   outtextxy(95,35,"Y");

   outtextxy(610,405,"X");

   outtextxy(85,415,"O");

   setfillstyle(LINE_FILL,BLUE);

   bar(150,100,200,419);

   setfillstyle(XHATCH_FILL,RED);

   bar(225,150,275,419);

   setfillstyle(WIDE_DOT_FILL,GREEN);

   bar(300,200,350,419);

   setfillstyle(INTERLEAVE_FILL,MAGENTA);

   bar(375,125,425,419);

   setfillstyle(HATCH_FILL,BROWN);

   bar(450,175,500,419);

   getch();

   return 0;

}

 

Q. Write a program to make 3d bar chart in c graphics.

 

#include <graphics.h>

#include <conio.h>

 int main()

{

   int gd = DETECT, gm;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   setcolor(YELLOW);

   rectangle(0,30,639,450);

   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);

   setcolor(WHITE);

   outtextxy(275,0,"Bar Chart");

 

   setlinestyle(SOLID_LINE,0,2);

   line(100,420,100,60);

   line(100,420,600,420);

   line(90,70,100,60);

   line(110,70,100,60);

   line(590,410,600,420);

   line(590,430,600,420);

   outtextxy(95,35,"Y");

   outtextxy(610,405,"X");

   outtextxy(85,415,"O");

   setfillstyle(LINE_FILL,BLUE);

   bar(150,100,200,419);

   setfillstyle(XHATCH_FILL,RED);

   bar(225,150,275,419);

   setfillstyle(WIDE_DOT_FILL,GREEN);

   bar(300,200,350,419);

   setfillstyle(INTERLEAVE_FILL,MAGENTA);

   bar(375,125,425,419);

   setfillstyle(HATCH_FILL,BROWN);

   bar(450,175,500,419);

   getch();

   return 0;

}

 

 

 

 

Q. Write a program to make Pie chart in c graphics.

 

#include<graphics.h>

#include<conio.h>

 int main()

{

   int gd = DETECT, gm, midx, midy;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   setcolor(MAGENTA);

   rectangle(0,40,639,450);

   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);

   setcolor(WHITE);

   outtextxy(275,10,"Pie Chart");

   midx = getmaxx()/2;

   midy = getmaxy()/2;

 

   setfillstyle(LINE_FILL,BLUE);

   pieslice(midx, midy, 0, 75, 100);

   outtextxy(midx+100, midy - 75, "20.83%");

   setfillstyle(XHATCH_FILL,RED);

   pieslice(midx, midy, 75, 225, 100);

   outtextxy(midx-175, midy - 75, "41.67%");

   setfillstyle(WIDE_DOT_FILL,GREEN);

   pieslice(midx, midy, 225, 360, 100);

   outtextxy(midx+75, midy + 75, "37.50%");

   getch();

   return 0;

}

 

Q. Write a program to show multiple circles on different  distance from a same center.

 

#include <graphics.h>

int main()

{

   int gd = DETECT, gm;

   int x = 320, y = 240, radius;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   for ( radius = 25; radius <= 125 ; radius = radius + 20)

      circle(x, y, radius);

   getch();

   closegraph();

   return 0;

}

 

Q. Write a program to create a moving car.

 

#include <graphics.h>

#include <dos.h>

int main()

{

   int i, j = 0, gd = DETECT, gm;

   initgraph(&gd,&gm,"C:\\TC\\BGI");

   settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

   outtextxy(25,240,"Press any key to view the moving car");

   getch();

 

   for( i = 0 ; i <= 420 ; i = i + 10, j++ )

   {

      rectangle(50+i,275,150+i,400);

      rectangle(150+i,350,200+i,400);

      circle(75+i,410,10);

      circle(175+i,410,10);

      setcolor(j);

      delay(100);

      if( i == 420 )

         break;

      if ( j == 15 )

         j = 2;

       cleardevice(); // clear screen

   }

 

   getch();

   closegraph();

   return 0;

}

 

Q. Write a program to show and restrict the mouse pointer in a circle.

 

#include<graphics.h>

#include<conio.h>

#include<dos.h>

#include<stdlib.h>

#include<math.h>

union REGS i, o;

int initmouse()

{

   i.x.ax = 0;

   int86(0X33, &i, &o);

   return ( o.x.ax );

}

 

void showmouseptr()

{

   i.x.ax = 1;

   int86(0X33, &i, &o);

}

 

void hidemopuseptr()

{

   i.x.ax = 2;

   int86(0X33,&i,&o);

}

 

void getmousepos(int *x, int *y)

{

   i.x.ax = 3;

   int86(0X33, &i, &o);

   *x = o.x.cx;

   *y = o.x.dx;

 

}

 

void movemouseptr(int x, int y)

{

   i.x.ax = 4;

   i.x.cx = x;

   i.x.dx = y;

   int86(0X33, &i, &o);

}

 

main()

{

   int gd = DETECT, gm, midx, midy, radius, x, y, tempx, tempy;

   radius = 100;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   if(!initmouse())

   {

      closegraph();

      exit(1);

   }

 

   midx = getmaxx()/2;

   midy = getmaxy()/2;

   showmouseptr();

   movemouseptr(midx, midy);

   circle(midx, midy, radius);

   x = tempx = midx;

   y = tempy = midy;

   while(!kbhit())

   {

      getmousepos(&x, &y);

 

      if((pow(x-midx,2)+pow(y-midy,2)-pow(radius,2))>0)

      {

         movemouseptr(tempx, tempy);

         x = tempx;

         y = tempy;

      }

 

      tempx = x;

      tempy = y;

   }

 

   closegraph();

   return 0;

}

 

Q. Write a program to make a C – Paint in C Graphics.

 

#include<graphics.h>

#include<dos.h>

#include<math.h>

#include<stdlib.h>

#include<stdio.h>

#include<conio.h>

 

union REGS i, o;

int leftcolor[15];

 

int get_key()

{

   union REGS i,o;

 

   i.h.ah = 0;

   int86(22,&i,&o);

 

   return ( o.h.ah );

}

 

void draw_color_panel()

{

   int left, top, c, color;

 

   left = 100;

   top = 436;

 

   color = getcolor();

   setcolor(GREEN);

   rectangle(4,431,635,457);

   setcolor(RED);

   settextstyle(TRIPLEX_FONT,0,2);

   outtextxy(10,431,"Colors : ");

 

   for( c = 1 ; c <= 15 ; c++ )

   {

      setfillstyle(SOLID_FILL, c);

      bar(left, top, left+16, top+16);

      leftcolor[c-1] = left;

      left += 26;

   }

 

   setcolor(color);

}

 

void draw_shape_panel()

{

   int left, top, c, color;

 

   left = 529;

   top = 45;

 

   color = getcolor();

   setcolor(GREEN);

   rectangle(525,40,633,255);

 

   for( c = 1 ; c <= 7 ; c++ )

   {

      rectangle(left, top, left+100, top+25);

      top += 30;

   }

   setcolor(RED);

   outtextxy(530,45,"Bar");

   outtextxy(530,75,"Line");

   outtextxy(530,105,"Pixel");

   outtextxy(530,135,"Ellipse");

   outtextxy(530,165,"Freehand");

   outtextxy(530,195,"Rectangle");

   outtextxy(530,225,"Clear");

   setcolor(color);

}

 

void change_color(int x, int y)

{

   int c;

 

   for( c = 0 ; c <= 13 ; c++ )

   {

      if( x > leftcolor[c] && x < leftcolor[c+1] && y > 437 && y < 453 )

         setcolor(c+1);

      if( x > leftcolor[14] && x < 505 && y > 437 && y < 453 )

         setcolor(WHITE);

   }

}

 

char change_shape(int x, int y)

{

   if ( x > 529 && x < 625 && y > 45 && y < 70 )

      return 'b';

   else if ( x > 529 && x < 625 && y > 75 && y < 100 )

      return 'l';

   else if ( x > 529 && x < 625 && y > 105 && y < 130 )

      return 'p';

   else if ( x > 529 && x < 625 && y > 135 && y < 160 )

      return 'e';

   else if ( x > 529 && x < 625 && y > 165 && y < 190 )

      return 'f';

   else if ( x > 529 && x < 625 && y > 195 && y < 220 )

      return 'r';

   else if ( x > 529 && x < 625 && y > 225 && y < 250 )

      return 'c';

}

 

void showmouseptr()

{

   i.x.ax = 1;

   int86(0x33,&i,&o);

}

 

void hidemouseptr()

{

   i.x.ax = 2;

   int86(0x33,&i,&o);

}

 

void restrictmouseptr( int x1, int y1, int x2, int y2)

{

   i.x.ax = 7;

   i.x.cx = x1;

   i.x.dx = x2;

   int86(0x33,&i,&o);

 

   i.x.ax = 8;

   i.x.cx = y1;

   i.x.dx = y2;

   int86(0x33,&i,&o);

}

 

void getmousepos(int *button,int *x,int *y)

{

   i.x.ax = 3;

   int86(0x33,&i,&o);

 

   *button = o.x.bx;

   *x = o.x.cx;

   *y = o.x.dx;

}

 

main()

{

   int gd = DETECT,gm;

    int maxx,maxy,x,y,button,prevx,prevy,temp1,temp2,key,color;

   char ch = 'f' ;            // default free-hand drawing

 

   initgraph(&gd,&gm,"C:\\TC\\BGI");

 

   maxx = getmaxx();

   maxy = getmaxy();

 

   setcolor(BLUE);

   rectangle(0,0,maxx,maxy);

 

   setcolor(WHITE);

   settextstyle(3,HORIZ_DIR,1);

   outtextxy(maxx/2-180,maxy-28,"C - Paint Technlogica Computer Eeducation Society");

 

   draw_color_panel();

   draw_shape_panel();

 

   setviewport(1,1,maxx-1,maxy-1,1);

 

   restrictmouseptr(1,1,maxx-1,maxy-1);

   showmouseptr();

   rectangle(2,2,518,427);

   setviewport(1,1,519,428,1);

 

   while(1)

   {

      if(kbhit())

      {

         key = get_key();

 

         if( key == 1 )

         {

            closegraph();

            exit(0);

         }

      }

 

      getmousepos(&button,&x,&y);

 

      if( button == 1 )

      {

         if( x > 4 && x < 635 && y > 431 && y < 457 )

            change_color( x, y );

         else if ( x > 529 && x < 625 && y > 40 && y < 250 )

             ch = change_shape( x, y );

 

         temp1 = x ;

         temp2 = y ;

 

         if ( ch == 'f' )

         {

            hidemouseptr();

            while( button == 1 )

            {

               line(temp1,temp2,x,y);

               temp1 = x;

               temp2 = y;

               getmousepos(&button,&x,&y);

            }

            showmouseptr();

         }

 

         while( button == 1)

            getmousepos(&button,&x,&y);

 

         /* to avoid interference of mouse while drawing */

         hidemouseptr();

 

         if( ch == 'p')

            putpixel(x,y,getcolor());

 

         else if ( ch == 'b' )

         {

            setfillstyle(SOLID_FILL,getcolor());

            bar(temp1,temp2,x,y);

         }

         else if ( ch == 'l')

            line(temp1,temp2,x,y);

         else if ( ch == 'e')

            ellipse(temp1,temp2,0,360,abs(x-temp1),abs(y-temp2));

         else if ( ch == 'r' )

            rectangle(temp1,temp2,x,y);

         else if ( ch == 'c' )

         {

            ch = 'f';          // setting to freehand drawing

            clearviewport();

            color = getcolor();

            setcolor(WHITE);

            rectangle(2,2,518,427);

            setcolor(color);

         }

 

         showmouseptr();

      }

   }

}

 

W.A.P. to maximize a  circle

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

 void main()

  {

  int gd=DETECT,gm,r=0,v;

  initgraph(&gd,&gm,"C:\\TC\\BGI");

  v=50;

  while(!kbhit())

          {

          setcolor(RED);

          circle(r+100,250,v);

          delay(200);

          r+=20;

          v-=5;

          if(r>=200)

                   {

                   r=0;

                   cleardevice();

                   v=50;

                   }

          cleardevice();

          }

  getch();

  }

 

W.A.P. to create a cube

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void main()

 {

 int gd=DETECT,gm;

 clrscr();

 initgraph(&gd,&gm,"C:\\TC\\BGI");

 rectangle(200,100,300,200);

 rectangle(240,140,340,240);

 line(200,200,240,240);

 line(200,100,240,140);

 line(300,200,340,240);

 line(300,100,340,140);

 getch();

 }

 

W.A.P. to fill a object with some color

 

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

 void main()

  {

  int gd=DETECT,gm,left,top,right,bottom,i;

  clrscr();

  initgraph(&gd,&gm,"C:\\TC\\BGI");

  left=getmaxx()/2-50;

  top=getmaxy()/2-50;

  right=getmaxx()/2+50;

  bottom=getmaxy()/2+50;

  setfillstyle(1,7);

  rectangle(left,top,right,bottom);

  floodfill(getmaxx()/2,getmaxy()/2,15);

  getch();

  closegraph();

  }

 

W.A.P. for national flag

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

const float PI=3.14154;

 void main()

  {

  int gd=DETECT,gm,left,top,right,bottom,i,j, x,y;

  clrscr();

  initgraph(&gd,&gm,"C:\\TC\\BGI");

 

  setfillstyle(1,6);

  bar(50,50,200,100);

  setfillstyle(1,WHITE);

  bar(50,100,200,150);

  setfillstyle(1,GREEN);

  bar(50,150,200,200);

  setcolor(BLUE);

  circle(120,125,25);

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

          {

          x=25*cos(j*PI/180);

          y=25*sin(j*PI/180);

          line(120,125,120+x,125-y);

          }

  getch();

  closegraph();

  }

 

W.A.P. to create a moving car

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void main()

 {

 int gd=DETECT,gm;

 int i;

 initgraph(&gd,&gm,"C:\\TC\\BGI");

 for(i=0;i<600;i++)

          {

          setfillstyle(SOLID_FILL,WHITE);

          rectangle(i,100,i+90,150);

          rectangle(i+10,110,i+20,120);

          rectangle(i+25,110,i+35,120);

          rectangle(i+40,110,i+50,120);

          rectangle(i+55,110,i+65,120);

          rectangle(i+70,110,i+80,120);

          circle(i+20,150,10);

          circle(i+70,150,10);

          delay(5);

          clrscr();

          }

 getch();

 }

 

W.A.P. for moving circle

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

 void main()

  {

  int gd=DETECT,gm,r=0,i;

  initgraph(&gd,&gm,"C:\\TC\\BGI");

  i=1;

  while(!kbhit())

          {

          setcolor(i);

          circle(300,250,r);

          delay(200);

          r+=10;

          if(r>=100)

                   {

                   r=0;

                   cleardevice();

                   }

          i+=1;

          }

  getch();

  }

 

W.A.P. to create a rectangle

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

 {

 int gd=DETECT,gm;

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

 rectangle(50,50,50,50);

 getch();

 }

 

DDA algorithm

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

 void main()

  {

  int gd=DETECT,gm,s,dx,dy,m,x1,y1,x2,y2;

  float xi,yi,x,y;

  clrscr();

  printf("\nEnter the starting point x1 & y1: ");

  scanf("%d %d",&x1,&y1);

  printf("\nEnter the ending point x2 & y2: ");

  scanf("%d %d",&x2,&y2);

  initgraph(&gd,&gm,"C:\\TC\\BGI");

  cleardevice();

  dx=x2-x1;

  dy=y2-y1;

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

          {

          s=abs(dx);

          }

  else

          {

          s=abs(dy);

          }

  xi=dx/(float)s;

  yi=dy/(float)s;

  x=x1;

  y=y1;

  putpixel(x1,y1,WHITE);

  for(m=0;m<s;m++)

          {

          x+=xi;

          y+=yi;

          }

  putpixel(x,y,WHITE);

  getch();

  }

 

W.A.P.  to implement bezier curve.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<graphics.h>

#include<math.h>

void bezier(int x[4],int y[4])

 {

  int gd=DETECT,gm;

  int i;

  double t;

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

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

          {

           double xt=pow(1-t,3)*x[0]+3*t*pow(1-t,2)*x[1]+

           3*pow(t,2)*(1-t)*x[2]+pow(t,3)*x[3];

 

           double yt=pow(1-t,3)*y[0]+3*t*pow(1-t,2)*y[1]+3*pow(t,2)*(1-t)*y[2]+pow(t,3)*y[3];

           putpixel(xt,yt,RED);

          }

           for(i=0;i<4;i++)

           putpixel(x[i],y[i],YELLOW);

           getch();

           closegraph();

           return;

  }

void main()

 {

 int x[4],y[4];

 int i;

 clrscr();

 printf("\n enter the x-and y-coordinate of the four controll points:\n");

 for(i=0;i<4;i++)

 scanf("%d %d",&x[i],&y[i]);

 bezier(x,y);

 }

 

Write a program of line clipping

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

#define Round(val)((int)(val+0.5))

int maxx,maxy,minx,miny;

void clipping(int,int,int,int);

void main()

{

 int gd=DETECT,gm;

// void clipping(int xa,int ya,int xb,int yb);

 int xa,xb,ya,yb;

 clrscr();

 printf("\nEnter the Window Co-ordination: ");

 scanf("%d%d%d%d",&minx,&miny,&maxx,&maxy);

 printf("\nEnter the two points of the LINE: ");

 scanf("%d%d%d%d",&xa,&ya,&xb,&yb);

 initgraph(&gd,&gm,"C:\\TC\\BGI");

 rectangle(minx,miny,maxx,maxy);

 line(xa,ya,xb,yb);

 clipping(xa,ya,xb,yb);

 getch();

 closegraph();

}

void clipping(int xa,int ya,int xb,int yb)

{

 int Dx=xb-xa,Dy=yb-ya,steps,k;

 int visible1=0,visible2=0;

 float xin,yin,x=xa,y=ya;

 if(abs(Dx)>abs(Dy))

          {

           steps=abs(Dx);

          }

 else

          {

  steps=abs(Dx);

 }

 xin=Dx/(float)steps;

 yin=Dy/(float)steps;

 putpixel(Round(x),Round(y),2);

 for(k=0;k<steps;k++)

          {

           x+=xin;

          y+=yin;

           if((y>miny && y<maxy))

                   {

                   visible1=1;

                   putpixel(Round(x),Round(y),2);

                   }

          else

                   {

                     visible2=1;

                   }

          }

          if(visible1==0)

 {

                   outtextxy(20,200,"Completely INVISIBLE");

                   }

 if(visible1==1 && visible2==1)

                   {

                   outtextxy(20,20,"Partialy VISIBLE");

                   }

          if(visible1==1 && visible2==0)

                   {

                    outtextxy(20,20,"Completely VISIBLE");

                   }

}

 

No comments:

Post a Comment

JAVA PROGRAMMING - OOPS CHAPTER 1

Object-Oriented Programming Chapter 1: Basics by Souradeep Roy Using IntelliJ IDEA Community version:- _____________________________________...