Home Forums C Programming 3D Shape

Viewing 1 reply thread
  • Author
    Posts
    • #1886
      will
      Participant
      /*Author: Mohammed Ali Akbani
      Level : Intermediate
      E-mail: [email protected]
      Web Site: http://www.akbani.20m.com
      Please do not repost it in your name.
      */
      #include
      #include
      #include
      #include "svga256.h"
      const float pi = 3.142;
      struct coordinate
      {
        unsigned int x;
        unsigned int y;
      };
      struct dimension
      {
        int x;
        int y;
        int z;
      };
      class cube_3d
      {
      private:
        dimension dimension;
        coordinate ini;
        coordinate front[4];
        coordinate back [4];
      public:
        cube_3d()
      {
        get_dimensions();
        ini.x = 100;
        ini.y = 700;
        for(int i=0; i<4; i++)
        {
          front.x = ini.x;
          front.y = ini.y;
        }
        for(int j=0; j<4; j++)
        {
          back[j].x = ini.x;
          back[j].y = ini.y;
        }
      }
      void get_dimensions()
      {
        clrscr();
        cout << "Enter x dimension : ";
        cin >> dimension.x;
        cout << "Enter y dimension : ";
        cin >> dimension.y;
        cout << "Enter z dimension : ";
        cin >> dimension.z;
      }
      void calculate_points()
      {
        calculate_front();
        calculate_back();
      }
      void draw_cube()
      {
        draw_front();
        draw_back();
        draw_sides();
      }
      void calculate_front()
      {
        front[1].x += dimension.x;
        front[2].x += dimension.x;
        front[2].y -= dimension.y;
        front[3].y -= dimension.y;
      }
      void calculate_back()
      {
        back[0].y -= (int)(dimension.z * sin(pi/4));
        back[0].x += (int)(dimension.z * sin(pi/4));
        back[1].x = back[0].x + dimension.x;
        back[1].y = back[0].y;
        back[2].x = back[0].x + dimension.x;
        back[2].y = back[0].y - dimension.y;
        back[3].x = back[0].x;
        back[3].y = back[0].y - dimension.y;
      }
      void draw_front()
      {
        setcolor(RED);
        line(front[0].x, front[0].y, front[1].x, front[1].y);
        setcolor(BLUE);
        line(front[1].x, front[1].y, front[2].x, front[2].y);
        setcolor(RED);
        line(front[2].x, front[2].y, front[3].x, front[3].y);
        setcolor(BLUE);
        line(front[3].x, front[3].y, front[0].x, front[0].y);
      }
      void draw_back()
      {
        setcolor(RED);
        line(back[0].x, back[0].y, back[1].x, back[1].y);
        setcolor(BLUE);
        line(back[1].x, back[1].y, back[2].x, back[2].y);
        setcolor(RED);
        line(back[2].x, back[2].y, back[3].x, back[3].y);
        setcolor(BLUE);
        line(back[3].x, back[3].y, back[0].x, back[0].y);
      }
      void draw_sides()
      {
        setcolor(GREEN);
        line(back[0].x, back[0].y, front[0].x, front[0].y);
        setcolor(GREEN);
        line(back[1].x, back[1].y, front[1].x, front[1].y);
        setcolor(GREEN);
        line(back[2].x, back[2].y, front[2].x, front[2].y);
        setcolor(GREEN);
        line(back[3].x, back[3].y, front[3].x, front[3].y);
      }
      };
      void main()
      {
        cube_3d cube;
        SVGAinitgraph("e:\tc\bgi");
        cube.calculate_points();
        cube.draw_cube();
        getch();
      }

    • #3139
      arshi
      Participant

      Hello ! I Want to Know about more 3D- Graphics Functions

Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.