C++ Programming Tutorial

 
 
 

Graphics Programming

Quadratic Surfaces

Draw a Sphere using Ellipses Draw a Sphere using Parametric Equations Draw an Ellipsoid using Parametric Equations

Character Generation

Urdu Alphabets using Stroke Method Urdu Alphabets using Matrix Method

Arc

Circular Arc using Trigo. Method Elliptical Arc using Trigo. Method

General Programs

C-Curve of nth order K-Curve of nth order Cubic Bezier Curve Bezier Curve of nth degree Scanfill algorithm Boundary Fill - 8 Connected Point Flood fill algorithm Rotate About Origin Rotate about reference point Scaling about origin Scaling about reference point Polyline translation Reflection in x axis Reflection in y Axis Reflection on any line Midpoint Circle Drawing Bresenhams Line Algorithm (BLA) Generate a pattern Draw a Chess Board Draw a Luddo Board Deterministic Finite Automation for identifier Kurskals algo - Minimum Cost Spanning Tree

Windows Programs

Checkbox like windows Simple windows & buttons Moving message box like windows Text box Graphical Rep. of tower of hanoi Graphical menu - operate it using arrow keys Text animation

Line

Line using Parametric equations Line-Cartesian Slope-Intercept Equation simple imp Line using Cartesian Slope-Intercept Equation Line - BLA - slopes negative and greater than 1 Line - BLA - slopes negative and less than 1 Line - BLA - slopes positive and greater than 1 Line - BLA - slopes positive and less than 1 DDA line drawing algorithm Bresenham line drawing algorithm Cohen sutherland Line clipping algo.

Line Styles

Different kinds of Dashed Lines Different kinds of Thick Lines

Polygons

Draw a Polygon Draw a Triangle Draw a Rectangle Sutherland-Hodgeman Polygon Clipping Algo

Circle

Circle using Trigo. Method Circle using Polynomial Method Circle using Bresenhams Circle algo. Circle using MidPoint Circle algo.

Ellipse

Ellipse using Polynomial Method Ellipse using Trigo. Method Ellipse using MidPoint Ellipse algo.

2D Transformations

Translation Transformation Scaling Transformation Scaling Trans along a Fixed Point Scaling Trans along Arbitrary Direction Rotation Transformation Rotation Trans along a Pivot Point Reflection tran of x-axix, y-axis and w.r.t origin Reflection tran of line y=x and y=-x X-Direction Shear Transformation Y-Direction Shear Transformation

2D Viewing - Clipping

Window-to-Viewport Coordinate Tran Point Clipping Algorithm Cohen-Sutherland Line Clipping Algo Cohen-Sutherland MidPoint Subdivision Line Nicol Lee Nicol algo. for Line Clipping Liang-Barsky Line Clipping Algo Window-to-Viewport Transformaton None-or-All String Clipping Strategy None-or-All Character Clipping Strategy

3D Object Representations

3D object using Polygon-Mesh Rep. 3D object - Translational Sweep Representatiom 3D object - Rotational Sweep Rep.

3D Transformations

3D Rotation Trans along x-axis 3D Rotation Trans along y-axis 3D Rotation Trans along z-axis 3D Reflection Trans along xy-plane 3D Reflection Trans along yz-plane 3D Reflection Trans along zx-plane 3D Shearing Trans along x-axis 3D Shearing Trans along y-axis 3D Shearing Trans along z-axis

Bezier Curves - Surfaces

3D Cubic Bezier Curve 3D Bezier Curve of nth degree 3D Piece-Wise Bezier Curve of nth degree 3D Bezier Surface for MxN control points

Projection

3D objects - Standard Perspective Projection 3D obj - Arbitrary Plane and Center of Projection 3D objects using General Perspective Projection 3D obj-Orthographics Proje Parallel onto xy-plane 3D obj-Cavalier Oblique Parallel prj-xy-plane 3D obj-Cabinet Oblique Parallel prj - xy-plane

Fill Algorithm or Area Filling

Geometric shapes using Boundary Geometric shapes - Boundary - Linked List Geometric shapes using Flood Geometric shapes - Flood - Linked-List Polygon using Scan Line Polygon Rectangle using Scan-Line Rectangle Circle using Scan-Line Circle Circle - Scan-Line Circle - Polar Coordinates
 
 
 # include <iostream.h>
 # include <graphics.h>
 # include    <conio.h>
 # include     <math.h>

 # define  f                 0.3
 # define  projection_angle   45


 void show_screen( );

 void Sphere(const int,const int,const int,const int);

 void get_projected_point(double&,double&,double&);
 void multiply_matrices(const double[4],const double[4][4],double[4]);


 int main( )
    {
       int driver=VGA;
       int mode=VGAHI;

       int x=0;
       int y=0;
       int z=0;
       int r=0;

       do
      {
         show_screen( );

         gotoxy(8,10);
         cout<<\"Coordinates of Central Point - (x,y,z) :\";

         gotoxy(8,11);
         cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\";

         gotoxy(12,13);
         cout<<\"Enter the value of x = \";
         cin>>x;

         gotoxy(12,15);
         cout<<\"Enter the value of y = \";
         cin>>y;

         gotoxy(12,17);
         cout<<\"Enter the value of z = \";
         cin>>z;

         gotoxy(8,21);
         cout<<\"Radius of the Sphere - r :\";

         gotoxy(8,22);
         cout<<\"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\";

         gotoxy(12,24);
         cout<<\"Enter the value of r = \";
         cin>>r;

         initgraph(&driver,&mode,\"..\\\\Bgi\");

         setcolor(15);
           Sphere(x,y,z,r);

         setcolor(15);
           outtextxy(110,460,\"Press <Enter> to continue or any other key to exit.\");

         int key=int(getch( ));

         if(key!=13)
        break;
      }
       while(1);

       return 0;
    }



 //-----------------------------  Sphere( )  -----------------------------//


 void Sphere(const int _x,const int _y,const int _z,const int r)
    {
       double x;
       double y;
       double z;

       for(float u=0;u<=1;u+=0.0005)
      {
         for(float v=0;v<=1;v+=0.05)
        {
           x=((r*cosl((u*M_PI))*cosl((v*2*M_PI)))+_x);
           y=((r*cosl((u*M_PI))*sinl((v*2*M_PI)))+_y);
           z=((r*sinl((u*M_PI)))+_z);

           get_projected_point(x,y,z);

           putpixel(int(x+0.5),int(y+0.5),15);
        }
      }

       for(float v=0;v<=1;v+=0.0005)
      {
         for(float u=0;u<=1;u+=0.05)
        {
           x=((r*cosl((u*M_PI))*cosl((v*2*M_PI)))+_x);
           y=((r*cosl((u*M_PI))*sinl((v*2*M_PI)))+_y);
           z=((r*sinl((u*M_PI)))+_z);

           get_projected_point(x,y,z);

           putpixel(int(x+0.5),int(y+0.5),15);
        }
      }
    }

 /************************************************************************/
 //---------------------  get_projected_point( )  -----------------------//
 /************************************************************************/

 void get_projected_point(double& x,double& y,double& z)
    {
       double fcos0=(f*cos(projection_angle*(M_PI/180)));
       double fsin0=(f*sin(projection_angle*(M_PI/180)));

       double Par_v[4][4]={
                {1,0,0,0},
                {0,1,0,0},
                {fcos0,fsin0,0,0},
                {0,0,0,1}
             };

       double xy[4]={x,y,z,1};
       double new_xy[4]={0};

       multiply_matrices(xy,Par_v,new_xy);

       x=new_xy[0];
       y=new_xy[1];
       z=new_xy[2];
    }

 /************************************************************************/
 //----------------------  multiply_matrices( )  ------------------------//
 /************************************************************************/

 void multiply_matrices(const double matrix_1[4],
                 const double matrix_2[4][4],double matrix_3[4])
    {
       for(int count_1=0;count_1<4;count_1++)
      {
         for(int count_2=0;count_2<4;count_2++)
        matrix_3[count_1]+=
               (matrix_1[count_2]*matrix_2[count_2][count_1]);
      }
    }


 //--------------------------  show_screen( )  ---------------------------//


 void show_screen( )
    {
       restorecrtmode( );
       clrscr( );
       textmode(C4350);

       cprintf(\"\\n********************************************************************************\");
       cprintf(\"*-********************************-          -********************************-*\");
       cprintf(\"*---------------------------------- \");

       textbackground(1);
       cprintf(\" Sphere \");
       textbackground(8);

       cprintf(\" ----------------------------------*\");
       cprintf(\"*-********************************-          -********************************-*\");
       cprintf(\"*-****************************************************************************-*\");

       for(int count=0;count<42;count++)
      cprintf(\"*-*                                                                          *-*\");

       gotoxy(1,46);
       cprintf(\"*-****************************************************************************-*\");
       cprintf(\"*------------------------------------------------------------------------------*\");
       cprintf(\"********************************************************************************\");

       gotoxy(1,2);
    }

    Related Post:
  1. Program of calculator

  2. Program to show an example of Hashing

  3. Program of nesting member functions, private member functions and array of objects

  4. Implement a Matrix class representing 2-dimensional matrix with the following functionalities i.e. operators + , - , * , == , += &lt;&lt; , &gt;&gt; etc

  5. Program to illustrate the use of operator * and &

  6. Program to get possible prime numbers like number 6158 contains the substrings 6, 1, 5, 8, 61, 15, 58, 615, 158, and 6158.Find the largest substring..

  7. Program that provides an example of dynamic memory allocation

  8. Program to implement the Binary search Algorithm

  9. Program to illustrate over-riding of base class member function in a derived class

  10. Program to illustrate the use of default argument function

  11. Program to show the projection of 3D objects using Standard Perspective Projection w.r.t. an Arbitrary Plane and Center of Projection...

  12. Program to fill different types of geometric shapes using Boundary Fill Algorithm (Using Linked-List)

  13. Program that maintains library shop stock using link list

  14. Program to solve the Towers of Hanoi Problem (using Recursive Algorithm)

  15. Program that provides an example of destructors

  16. Program to compute and print out the decimal notation for the reciprocal of each input integer

  17. Program that prints odd numbers form 0 to 50 (Using for Loop)

  18. Program that provides an example to return an object from a function

  19. Program that list of letters should be an alphabetical list of upper case letters followed by an alphabetical list of lower case letters

  20. Program that displays graphical representation of tower of hanoi

 
 
Didn't find what you were looking for? Find more on Program to draw a Sphere using Parametric Equations