TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Computer Graphics Programs

Computer Graphics Programs with Computer Graphics Tutorial, Line Generation Algorithm, 2D Transformation, 3D Computer Graphics, Types of Curves, Surfaces, Computer Animation, Animation Techniques, Keyframing, Fractals etc.

<< Back to COMPUTER

Computer Graphics Programs

Write a Program to draw basic graphics construction like line, circle, arc, ellipse and rectangle.

#include<graphics.h>
#include<conio.h>
void main()
{
	intgd=DETECT,gm;
	initgraph (&gd,&gm,"c:\\tc\\bgi");
	setbkcolor(GREEN);
	printf("\t\t\t\n\nLINE");
	line(50,40,190,40);
	printf("\t\t\n\n\n\nRECTANGLE");
	rectangle(125,115,215,165);
	printf("\t\t\t\n\n\n\n\n\n\nARC");
	arc(120,200,180,0,30);
	printf("\t\n\n\n\nCIRCLE");
	circle(120,270,30);
	printf("\t\n\n\n\nECLIPSE");
	ellipse(120,350,0,360,30,20);
	getch();
}

Output

Computer Graphics Programs

Write a Program to draw animation using increasing circles filled with different colors and patterns.

#include<graphics.h>
#include<conio.h>
void main()
{
	intgd=DETECT, gm, i, x, y;
	initgraph(&gd, &gm, "C:\\TC\\BGI");
	x=getmaxx()/3;
	y=getmaxx()/3;
	setbkcolor(WHITE);
	setcolor(BLUE);
	for(i=1;i<=8;i++)
          {
		setfillstyle(i,i);
		delay(20);
		circle(x, y, i*20);
		floodfill(x-2+i*20,y,BLUE);
	}
	getch();
	closegraph();
}

Output

Computer Graphics Programs

Program to make screen saver in that display different size circles filled with different colors and at random places.

#include<stdio.h>
#include<conio.h>
#include"graphics.h"
#include"stdlib.h"
void main()
{
	intgd=DETECT,gm,i=0,x,xx,y,yy,r;
	//Initializes the graphics system
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	x=getmaxx();
	y=getmaxy();
	while(!kbhit())
	{
		i++;
	  //	setfillstyle(random(i),random(30));

		circle(xx=random(x),yy=random(y),random(30));
		setfillstyle(random(i),random(30));
		floodfill(xx,yy,getmaxcolor());
		delay(200);
	}
	getch();
}

Output

Computer Graphics Programs

Write a Program to make a moving colored car using inbuilt functions.

#include<graphics.h>
#include<conio.h>
int main()
{
	intgd=DETECT,gm, i, maxx, cy;
	initgraph(&gd, &gm, "C:\\TC\\BGI");
	setbkcolor(WHITE);
	setcolor(RED);
	maxx = getmaxx();
	cy = getmaxy()/2;
	for(i=0;i<maxx-140;i++)
        {
		cleardevice();
		line(0+i,cy-20, 0+i, cy+15);
		line(0+i, cy-20, 25+i, cy-20);
		line(25+i, cy-20, 40+i, cy-70);
		line(40+i, cy-70, 100+i, cy-70);
		line(100+i, cy-70, 115+i, cy-20);
		line(115+i, cy-20, 140+i, cy-20);
		line(0+i, cy+15, 18+i, cy+15);
		circle(28+i, cy+15, 10);
		line(38+i, cy+15, 102+i, cy+15);
		circle(112+i, cy+15,10);
		line(122+i, cy+15 ,140+i,cy+15);
		line(140+i, cy+15, 140+i, cy-20);
		rectangle(50+i, cy-62, 90+i, cy-30);
		setfillstyle(1,BLUE);
		floodfill(5+i, cy-15, RED);
		setfillstyle(1, LIGHTBLUE);
		floodfill(52+i, cy-60, RED);
		delay(10);
         }
	getch();
	closegraph();
	return 0;
}

Output

Computer Graphics Programs
Computer Graphics Programs

Write a Program to print your name in Hindi script on console output in C.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{	
	int gd=DETECT,gm;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	setbkcolor(9);
	line(100,100,370,100);
	
	line(120,100,120,170);
	arc(143,100,0,180,23);
	line(165,100,165,155);
	arc(150,155,100,0,15);
	line(180,100,180,170);

	circle(210,140,10);
	line(210,130,250,130);

	circle(280,140,10);
	line(280,130,330,130);
	line(330,100,330,170);
	line(345,100,345,170);
	ellipse(337,100,0,180,9,18);
	getch();
}

Output

Computer Graphics Programs

Write a Program control a ball using arrow keys.

#include<graphics.h>
#include<stdio.h>
void main()
{
	intgd=DETECT,gm,x,y,r=40;
	charch;
	initgraph(&gd,&gm,"C:/TURBOC3/BGI");
	setbkcolor(3);
	x=getmaxx()/2;
	y=getmaxy()/2;
	setfillstyle(1,RED);
	circle(x,y,r);
	floodfill(x,y,getmaxcolor());
	while((ch=getch())!=13)
      {
		switch(ch)
                     {
			case 75:	if(x>=r+1)
                                {
					cleardevice();
					circle(x-=10,y,r);
					floodfill(x,y,getmaxcolor());
	                      }
				break;
			case 72:	if(y>=r+1)
                                {
					cleardevice();
					circle(x,y-=10,r);
					floodfill(x,y,getmaxcolor());
                                  }
				break;
			case 77:	if(x<=(getmaxx()-r-10))
                               {
					cleardevice();
					circle(x+=10,y,r);
					floodfill(x,y,getmaxcolor());
                                }
 		 		break;
			case 80:	if(y<=(getmaxy()-r-10))
                               {
					cleardevice();
					circle(x,y+=10,r);
					floodfill(x,y,getmaxcolor());
		         }
		}
	}
	getch();
}

Output

Computer Graphics Programs

Write a Program to implement Digital Clock.

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

struct time t;
void display(int,int,int);
void main()
{
	int i=0,gd=DETECT,gm,hr,min,sec;
	clrscr();
	initgraph(&gd,&gm,"c:\\turboc3\\bgi");
	setcolor(GREEN);
	settextstyle(4,0,7);

	while(!kbhit())
	{
	   gettime(&t);
	   hr=t.ti_hour;
	   min=t.ti_min;
	   sec=t.ti_sec;
	   i++;

	   display(100,100,hr);
	   display(200,100,min);
	   display(300,100,sec);
              sound(400);
	   delay(30);
	   nosound();
	   delay(930);
	   cleardevice();
	}
	getch();
}
void display(int x,int y,int num)
{

	 char str[3];
	 itoa(num,str,10);

	 settextstyle(4,0,7);

	 outtextxy(180,100,":");
	 outtextxy(280,100,":");
	 outtextxy(x,y,str);

	 rectangle(90,90,380,200);
	 rectangle(70,70,400,220);

	 outtextxy(90,250,"Digital Clock");
}

Output

Computer Graphics Programs

Write a Program to make puzzle game.

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

int a[5][5];
int t[16]={0,4,11,12,7,1,15,5,13,6,10,3,2,14,8,9};
int test[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

struct pos
     {
       int h,v;
     }
	p[4][4];
 int row=4,col=4;

void game(int); //MOVEMENT
void rec();   //DRAWING RECTANGLE
void pri();   //PRINTING NUMBERS INITIALLY
int getkey();   // TO TRACE KEY PRESSED
inline void space() 
   {
         cout<<""; 
   }
inline void print(int r,int c) 
   { 
          cout<<a[r][c]; 
   }
void init();   //TO STORE CO-ORDINATES
int stop();     // STOPING CRITERION
void gopr(int,int);   //TO PRINT NUMBER IN GAME

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

      int d,cr=1;
      init();
      rec();
      print();

      while(cr!=16)
    {
          d=getkey();
       game(d);
      cr=stop();
  }

  settextstyle(10,0,1);
  outtextxy(400,300,"You are winner!");
  getch();

}
void rec()
{
        setcolor(5);

        for(int i=0;i<200;i+=50)
     {
        for(int j=0;j<240;j+=60)
        rectangle(j+100,i+100,j+50,i+60);
     }

  }

   void pri()
  {
         int k=1;
         for(int x=0,i=6;x<4;x++,i+=3)
    {
         for(int y=0,j=10;y<4&&k<16;y++,j+=7,k++)
        {

         gotoxy(p[x][y].h,p[x][y].v);
         cout<<a[x][y];
        }
    }

  }

      int getkey()
  {
      union REGS i,o;
      while(!kbhit());
       i.h.ah=0;
      int86(22,&i,&o);
      return(o.h.ah);
  } 

     void init()
 {
            int k=1;
            for(int x=0,i=6;x<4;x++,i+=3)
      {
            for(int y=0,j=10;y<4;y++,j+=7)
         {
             p[x][y].h=j;
             p[x][y].v=i;
             a[x][y]=t[k++];
          }
      }

   }

     void game(int s)
  {
	   int r=row-1;
	   int c=col-1;

	    if(s==77 &&c!=0)  //right
	  {
		col--;

	     a[r][c]=a[r][c-1];
               gopr(r,c-1);
               space();
               gopr(r,c);
               print(r,c-1);
              }
             if(s==80 && r!=0)   //down
	  {
	     row--;
               a[r][c]=a[r-1][c];
               gopr(r-1,c);
               space();
               gopr(r,c);
               print(r-1,c);
            }

	      if(s==75 && c!=3)     //left
	 {
	      a[r][c]=a[r][c+1];
	    col++;
	    gopr(r,c+1);
              space();
              gopr(r,c);
                print(r,c+1);

	 }

	    if(s==72 &&r!=3)     //up
	 {

	    a[r][c]=a[r+1][c];
               row++;
               gopr(r+1,c);
               space();
               gopr(r,c);
               print(r+1,c);

           }
 }

	void gopr(int x, int y)
       {
	 gotoxy(p[x][y].h,p[x][y].v);
       }


           int stop()
    {
          int k=0,d=1;
          for(int x=0;x<4;x++)
      {
          for(int y=0;y<4;y++)
         {
	 if(a[x][y]==test[k])
	   d++;
             k++;
          }
      }
       return d;
   }

Output

Computer Graphics Programs
Computer Graphics Programs

Write a Program to implement bouncing ball using sine wave form.

#include<stdio.h>
#include<graphics.h> 
#define HEIGHT getmaxy()
#define WIDTH getmaxx()
#define GROUND 450
#define MAXHEIGHT 420 
void main()
{
        int x,y=0,t=MAXHEIGHT,c=1;
        int gd=DETECT,gm;
       initgraph(&gd,&gm,"C:\\T urboC3\\BGI"); 
      for(x=40;x<=getmaxx();x=x+2)
 {
     //Draw Ground
     rectangle (0,MAXHEIGHT,getmaxx(),MAXHEIGHT+5);
     floodfill (5,MAXHEIGHT+3,WHITE);
    //Draw Ball
   pieslice(x,y,0,360,20);
   //floodfill(x,y,RED);
   delay(100);
   if(y>MAXHEIGHT-20)
     {
         c=0;
         t=t-40;
     }
     if(y<=(MAXHEIGHT-t))
    {
           c=1;
     }
        if(t>=40)
       y=y+(c? 15:-15); 
       cleardevice();
       //Exit upon keypress
       if(kbhit())
       break;
}
getch();
}

Output

Computer Graphics Programs

Write a Program to implement Bouncing Ball in vertical direction.

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
 
int main() 
{
          int gd = DETECT, gm;
          int i, x, y, flag=0;
          initgraph(&gd, &gm, "C:\\TC\\BGI");
 
         x = getmaxx()/2;
         y = 30;
        while (!kbhit()) 
      {
            if(y >= getmaxy()-30 || y <= 30)
           flag = !flag;
           /* draws the gray board */
           setcolor(RED);
           setfillstyle(SOLID_FILL, RED);
           circle(x, y, 30);
           floodfill(x, y, RED);
 
          delay(50); 
          cleardevice();
          if(flag)
      {
           y = y + 5;
      } 
          else
     {
          y = y - 5;
      }
  }
 
    getch();
    closegraph();
    return 0;
}

Output

Computer Graphics Programs

Write a program of Translation, Rotation, and Scaling using Composite Transformation.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,a,b;
void draw();
void rotate();
int main(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter first co-ordinate value for triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter second co-ordinatevalues for triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter third co-ordinate valuesfor triangle:");
scanf("%d%d",&x3,&y3);
draw();
getch();
rotate();
getch();

return 0;
}

void draw()
{
  line(x1,y1,x2,y2);
  line(x2,y2,x3,y3);
  line(x3,y3,x1,y1);
}
 void rotate()
 {
	int a1,a2,a3,b1,b2,b3;
	float angle;
	printf("Enter the rotation angle co-ordinates:");
	scanf("%f",&angle);
	cleardevice();
	  angle=(angle*3.14)/180;
	  a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle);
	  b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle);
	  a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle);
	  b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle);
	  a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle);
	  b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle);
	  printf("ROTATION");
	  printf("\n Changed coordinates\n");
	  printf("%d %d\n%d %d\n%d %d",a1,b1,a2,b2,a3,b3);
	line(a1,b1,a2,b2);
	line(a2,b2,a3,b3);
	line(a3,b3,a1,b1);
 }

Output

Computer Graphics Programs
Computer Graphics Programs

Write a program to draw a Circle using midpoint implementation Method.

#include<stdio.h>
#include<graphics.h>
 
void drawcircle(int x0, int y0, int radius)
{
    int x = radius;
    int y = 0;
    int err = 0;
 
    while (x >= y)
    {
	putpixel(x0 + x, y0 + y, 7);
	putpixel(x0 + y, y0 + x, 7);
	putpixel(x0 - y, y0 + x, 7);
	putpixel(x0 - x, y0 + y, 7);
	putpixel(x0 - x, y0 - y, 7);
	putpixel(x0 - y, y0 - x, 7);
	putpixel(x0 + y, y0 - x, 7);
	putpixel(x0 + x, y0 - y, 7);
           if (err <= 0)
	{
	    y += 1;
	    err += 2*y + 1;
	}
 
	if (err > 0)
	{
	    x -= 1;
	    err -= 2*x + 1;
	}
    }
}
 void main()
{
	int gdriver=DETECT, gmode, error, x, y, r;
	initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
           printf("Enter radius of circle: ");
	scanf("%d", &r);
 
	printf("Enter co-ordinates of center(x and y): ");
	scanf("%d%d", &x, &y);
	drawcircle(x, y, r);
	getch();
}

Output

Computer Graphics Programs

Write a program to draw Bezier curve.

#include 
#include 
#include 
#include 

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, WHITE);
    }
  for (i=0; i<4; i++)
	putpixel (x[i], y[i], YELLOW);

    getch();
    closegraph();
    return;
}

void main()
{
    int x[4], y[4];
    int i;
    printf ("Enter the x- and y-coordinates of the four control points.\n");
    for (i=0; i<4; i++)
   scanf ("%d%d", &x[i], &y[i]);
    bezier (x, y);
 }         

Output

Computer Graphics Programs
Computer Graphics Programs

Program to rotate a rectangle about its midpoint

#include<iostream.h>
#include <stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#define pi 3.14
class arc
{
	float x[10], y[10], theta, h1, k1,r[0][10],ang;
	float p[10][10],p1[10][10],x1[10], y1[10],xm,yx;
	int i, k, j, n;
	public:
	void get();
	void cal();
	void map();
	void graph();
	void plot();
	void plot1();
};
void arc::get()
{
	cout "<<"\n ENTER ANGLE OF ROTATION ";
           cin>>ang;
           n = 4;
           cout<<"\n ENTER";
         for (i=0; i<n; i++)
         {
               cout<<"\n x["<i<<"] and y["<<i<<"]";
                cin>>x[i]>>y[i];
         }
         h1=x [0] + (([1]-x[0])/2);
         k1=y[0]+(([3]-y[0])/2);
         cout<<"\n MIDPOINT OF RECTANGLE IS--"<<h1<<"\t"<<k1;
         theta=(ang*pi)/180?;
         r[0][0]=cos (theta);
         r[0][1]= -sin?(theta);
         r[0][2]=(-h1*cos(theta))+(k1*sin(theta)+h1;
         r[1][0]=sin (theta);
         r [1][1]=cos (theta);
         r [1][2]=(-h1*sin(theta))+(-k1*cos(theta)+k1;
         r[2][0]=0;
         r[2][1]=0;
         r[2][2]=1;
}
void arc ::cal()
{
	for(i=0;i<n;i++)
	{
		p[0][i]=x[i];
		p[1][i]=y[i];
		p[2][i]=1;
	}
	for(i=0;i<3;i++)
	{
		for(j=0;j<n;j++)
		{
			p1[i][j]=0;
		           for (k=0;k<3;k++)
			{
				p1[i][j]+=r[i][k]*p[k][j];
			}
		}
	}
	for(i=0;i<n;i++)
	{
		x1[i]=p1[0][i];
		y1[i]=p[1][i];
	}
}
void arc::map()
{
	int gd=DETECT, gm;
	initgraph (&gd, &gm, "");
	int errorcode = graphresult();
	/*an error occurred */
           if (errorcode!=grOK)
	{
		printf("Graphics error: %s \n",grapherrormsg (errorcode));
                      printf("Press and key to halt: ");
                      getch();
                      exit(1); /* terminate with an error code */
            }
}
void arc::graph()
{
	xm=getmaxx()/2;
	ym=getmaxy()/2;
	line (xm, 0, xm, 2 * ym);
	line (0, ym, 2 * xm, ym);
}
void arc :: plot 1()
{
	for (i=0;i<n-1;i++)
	{
		circle (x1[i]+xm, (-y1[i]+ym), 2);
		line (x1[i]+xm, (-y1[i]+ym), x[i+1] xm, (-y1[0]+ym));
	}
	line (x1[n-1] +xm, (-y1[n-1]+ym), x1[0]+xm, (-y1[0]+ym));
	getch();
}
void arc :: plot ()
{
	for (i=0;i<n-1;i++)
	{
		circle (x[i]+xm, (-y[i]+ym), 2);
		line (x[i]+xm, (-y[i]+ym), x[i+1] xm, (-y[i+1]+ym));
	}
	circle (x[n-1]+xm, (-y[n-1]+ym),2);
	line (x[n-1]+xm, (-y[n-1]+ym), x[0]+xm, (-y[0]+ym));
	getch();
}
void main ()
{
	class arc a;
	clrscr();
	a.map();
	a.graph();
	a.get();
	a.cal();
	a.plot();
	a.plot1();
	getch();
}

Output

Computer Graphics Programs

Program to clip a line using Liang Barsky Method

#include
#include 
#include
#include
#include
class liang
{
	float x1,x2,y1,y2,u1,u2,dx,dy,xm,ym;
	float xmin, ymin, xmax, ymax,p[4],q[4],r[5],r[5];
	int gd, gm, test, s, k;
	public:
	void clipliang();
	void get();
	void map();
	void graph();
};
void liang :: map()
{
	gd=DETECT;
	initgraph (&gd, &gm, "");
	int errorcode = graphresult();
	/*an error occurred */
           if (errorcode!=grOK)
	{
		printf("Graphics error: %s \n",grapherrormsg (errorcode));
                      printf("Press and key to halt: ");
                      getch();
                      exit(1); /* terminate with an error code */
            }
}
void liang::graph()
{
	xm=getmaxx()/2;
	ym=getmaxy()/2;
	line (xm, 0, xm, 2 * ym);
	line (0, ym, 2 * xm, ym);
}
void liang :: get()
{
	cout<<"\n ENTER WINDOW COORDINATES xwmin, ywmin, xwmax, ywmax";
	cin>>xmin>>ymin>>xmax>>ymax>>;
	rectangle (xmin+xm,-ymin+ym,xmax+xm, -ymax+ym);
       //  rectangle (320+xwmin, 240-ywmax, 320+xwmax, 240-ywmin);
           cout<<"\n ENTER END POINTS OF LINE (x1, y1)(x2, y2) ";
           cin>>x1>>y1>>x2>>y2;
	line (x1+xm,-y1+ym, x2+xm,-y2+ym);
	getch();
}
       // line (x1, y1, x2, y2);
void liang :: clipliang()
{
	float x=0, y=1;
	dx=x2-x1;
	dy=y2-y1;
	p[0]=-dx;
	p[1]=dx;
	p[2]=-dy;
	p[3]=dy;
	q[0]=x1-xmin;
	q[1]=xmax-x1;
	q[2]=y1-ymin;
	q[3]=ymax-y1;
	for (k=0;k<4;k++)
	{
		if (p[k]==0&&q[k]<0)
		{
			cout<<"\n OUTSIDE";
                                 getch();
                                 exit(0);
                      }
                       if (p[k]<0)
	           {
			r[k]=q[k]/p[k];
			if (r1[k]u2)
		{
			cout<<"\n COMPLETE OUTSIDE WINDOW";
                                 getch();
                                 exit(0);
                        }
void main ()
{
	liang1;
	clrscr();
	l.map();
	l.graph();
	l.get();
	l.clipliang();
	getch();
}

Output

Computer Graphics Programs

Program to implement Standard Perspective Projection in 3-Dimensions.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#define pi 3.14
class per
{
	float x[10], y[10], z [10], d;
	float x1 [10], y1[10], xm, ym, z1 [10];
	int i, k, j, n;
	public:
	void get();
	void perpro();
	void map();
	void graph();
	void plot1();
}
void per::get()
{
	cout<< "\n ENTER OF VERTICES";
            cin>>n;
	cout<<"\n ENTER";
            for (i=0;i<n;i++)
	{
		cout<<"\n x["?i<<"], y["<<i<<"] and z ["<<i<<"]";
		cin>>x[i]>>y[i]>>z[i];
	}
		cout<<"\n ENTER WIDTH FOR Z-AXIS-d";
		cin>>d;
}
void per:: perpro()
{
	for(i=0;i<n;i++)
	{
		x1[i]=(d*x[i])/(z[i]+d);
		y1[i]=(d*y[i])/(z[i]+d);
		z1[i]=0;
	}
	cout<< "\n ENTER PROJECTION, VERTICES OF POLYGON ARE--";
            for(i=0;i<n;i++)
	{
		cout<<"x?["<<i<<"]="<<x1[i]<< "\t";
		cout<<"y?["<<i<<"]="<<y1[i]<< "\t";
		cout<<"z?["<<i<<"]="<<z1[i]<< "\t";
		cout<<"\n";
	}
}
void per :: map()
{
	int gd=DETECT;
	initgraph (&gd, &gm, "");
	int errorcode = graphresult();
	/*an error occurred */
           if (errorcode!=grOK)
	{
		printf("Graphics error: %s \n",grapherrormsg (errorcode));
                      printf("Press and key to halt: ");
                      getch();
                      exit(1); /* terminate with an error code */
            }
}
void per::graph()
{
	xm= get maxx()/2;
	ym=get maxy()/2
	line (xm,0, xm, 2 * ym);
	line (0, ym, 2 * xm, ym);
}
void per::plot1()
{
	for(i=0;i<n-1;i++)
	{
		line(x1[i]*100+xm,(-y1[i]*100+ym),x1[i+1]*100+xm,(-y1[i+1]*100+ym));
	}
line(x1[n-1]*100+xm,(-y1[n-1]*100+ym),x1[0]*100+xm,(-y1[0]*100+ym));
	getch();
}
void main()
{
	class per a;
	clrscr();
	a.map();
	a.graph();
	a.get();
	a.perpro();
	a.graph();
	a.plot1();
	getch();
}

Output

Computer Graphics Programs

Program to implement Parallel Projection in 3-Dimensions.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#define pi 3.14
class per
{
	float x[10], y[10], z [10], d, m[10], q1[10], r1[10], m1[10];
	float x1 [10], y1[10], xm, ym, z1 [10], cav[4] [4], p[10] [10];
	float p1[10] [10];
	float a, b, c, ang, theta, f;
	int i, k, j, n;
	public:
	void get();
	void par_v();
	void par_vnr0();
	void map();
	void graph();
	void project(float m[],float q[], float r[]);
};
void per:: perproject(float m[], float q[], float r[])
{
	cout<<"\n ENTER WIDTH FOR Z-AXIS d";
	cin>>d;
	cout<< "\n AFTER PROJECTION, VERTICES OF POLYGON ARE--";
            for(i=0;i<n;i++)
	{
		cout<<"x?["<<i<<"]="<<x1[i]<< "\t";
		cout<<"y?["<<i<<"]="<<y1[i]<< "\t";
		cout<<"z?["<<i<<"]="<<z1[i]<< "\t";
		cout<<"\n";
	}
	getch();
	clearviewport();
	graph();
	for(i=0;i<n-1;i++)
	{
		line(m[i]*100+xm,(-q[i]*100+ym),m[i+1]*100+xm,(-n1[i+1]*100+ym));

            }
	line(m[n-1]*100+xm,(-q[n-1]*100+ym),m[0]*100+xm,(-q[0]*100+ym));
getch();
}
void per::get()
{
	cout<< "\n ENTER OF VERTICES";
            cin>>n;
	cout<<"\n ENTER";
            for (i=0;i<n;i++)
	{
		cout<<"\n x["?i<<"], y["<<i<<"] and z ["<<i<<"]";
		cin>>x[i]>>y[i]>>z[i];
	}
		cout<<"\n ENTER WIDTH FOR Z-AXIS-d";
		cin>>d;
		int ch=100;
		do
		{
			cout<< "\n 1->orthographic projection";
                                cout<<"\n 2->oblique projection";
                                cout<<"\n 0-> exit";
			cout<<"\n enter ch";
			cin>>ch;
			switch (ch)
			{
				case1:
					int ch1=100;
				do
				{
					cout<<"\n 1->for xy viewplane";
					cout<<"\n 2->for yz viewplane";
					cout"\n 3->for zx viewplane";
					cout<<"\n 0->exit";
					cout<<"\n enter ch1";
					cin>>ch1;
					switch(ch1)
					{
						case1:
							x1[i]=x[i];
							y1[i]=y[i];
							z1[i]=0;
							project(x1,y1,z1);
							break;
						case2:
							x1[i]=0;
							y1[i]=0;
							z1[i]=z[i];
							project(x1,y1,z1);
							break;
						case3:
							x1[i]=x[i];
							y1[i]=0;
							z1[i]=z[i];
							project(x1,y1,z1);
							break;
					}
				}
			while (ch!=0)
			     break;
		           case2:
				ch1=100;
				do
				{
					cout<<"\n 1->on xy plane";
					cout<<"\n 2->cavalier";
					cout<<"\n 0->exit";
					cout<<"\n enter ch1";
					cin>>ch1;
					switch(ch1)
					{
						case1:
							par_v();
							break;
						case2:
							par_vnr0();
							break();
					}
					while(ch!=0);
					break;
				}
				while(ch!=0)
			}
	void per::par_v()
	{
		cout<<"\n enter v (a, b, c) "	;
		cin>>a>>b>>c;
		for(i=0; i<n;i++)
		{
			x1[i]=x[i]-((a*z[i]/c);
			y1[i]=y[i]-((b*z[i]/c);
			project(x1,y1, z1);
		}
         }
        void per::par_vnr0()
        {
		cout<<"\n enter f";
		cin>>f;
		cout<<"\n enter angle";
		cin>>angle;
		theta= ((pi/180)* ang);
		for(i=0;i<4;i++)
		{
			for(j=0;j<4;j++)
			{
				if(i==j)
				cav[i] [j]=1;
				else
					cav[i] [j]=0;
			}
		}
		cav[0] [2]=f*cos(theta);
		cav[1][2]=f*sin (theta);
		cav[2][2]=0;
		for(i=0;i<n;i++)
		{
			p[0][i]=x[i];
			p[1][i]=y[i];
			p[2][i]=z[i];
			p[3][i]=1;
		}
		for(i=0;i<4;i++)
		{
			for(j=0;j<n;j++)
			{
				p1[i][j]=0;
				for(k=0;k<4;k++)
				{
					p1[i][j]+=cav[i][k]*p[k][j];
				}
			}
		}
		for(i=0;i<n;i++)
		{
			x1[i]=p1[0][i];
			y1[i]=p1[1][i];
			z1[i]=p1[i];
		}
		project(x1,y1,z1);
        }
void per :: map()
{
	int gd=DETECT;
	initgraph (&gd, &gm, "");
	int errorcode = graphresult();
	/*an error occurred */
           if (errorcode!=grOK)
	{
		printf("Graphics error: %s \n",grapherrormsg (errorcode));
                      printf("Press and key to halt: ");
                      getch();
                      exit(1); /* terminate with an error code */
            }
}
void per::graph()
{
	xm= get maxx()/2;
	ym=get maxy()/2
	line (xm,0, xm, 2 * ym);
	line (0, ym, 2 * xm, ym);
}
void main()
{
	class per a;
	clrscr();
	a.map();
	a.get();
	getch();
}

Output

Computer Graphics Programs

Program to draw Sine, Cosine and Tangent Curves

#include
#include
#include
#include
#include
#include
#define pi 3.14
class arc
{
	float x, y, a, xm, ym;
	int i, ch;
	public:
	void get();
	void map();
	void sine();
	void cosine();
	void tangent();
};
void arc::get()
{
	cout<<"\n ENTER THE MAXIMUM VALUE FOR Y";
           cin>>y;
           cout<<"\n MENU IS---------";
	cout<<"\n 1-> SINE CURVE";
           cout<<"\n 2->COSINE CURVE";
	cout<<"\n 3-> TANGENT CURVE";
	cout<<"\n 4-> EXIT";
	cout<<"\n ENTER YOUR CHOICE";
           cin>>ch;
           switch(ch)
           {
                     case1:
			sine();
			break();
		case2:
			cosine();
		case3:
			tangent();
		case4:
			exit(0);
	}
}
void arc::sine()
{
	cleardevice();
	xm=getmaxx()/2;
	ym=getmaxy()/2;
	line(xm, 0, xm, 2*ym);
	line(0, ym, 2 *xm, ym);
	outtextxy(0, ym, "X-AXIS");
           outtextxy(xm, 0, "Y-AXIS");
	for(x=-300;x<=300;x=x+0.5)
	{
		y=a*sin((pi*x)/180);
		putpixel(x+(320),-y+240,RED);
	}
}
void arc::cosine()
{
	cleardevice();
	xm=getmaxx()/2;
	ym=getmaxy()/2;
	line(xm, 0, xm, 2*ym);
	line(0, ym, 2 *xm, ym);
	outtextxy(0, ym, "X-AXIS");
           outtextxy(xm, 0, "Y-AXIS");
	for(x=-300;x<=300;x=x+0.5)
	{
		y=a*cos((pi*x)/180);
		putpixel(x+(320),-y+240,RED);
	}
}
void arc :: map()
{
	int gd=DETECT,gm;
	initgraph (&gd, &gm, "");
	int errorcode = graphresult();
	/*an error occurred */
           if (errorcode!=grOK)
	{
		printf("Graphics error: %s \n",grapherrormsg (errorcode));
                      printf("Press and key to halt: ");
                      getch();
                      exit(1); /* terminate with an error code */
            }
}
void main()
{
	class arc a;
	clrscr();
	a.map();
	a.get();
	getch();
}

Output

Computer Graphics Programs
Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf