article

THE THREE DIMENSIONS

Email
Submitted on: 1/3/2015 4:22:00 PM
By: Gaurav Creations (from psc cd)  
Level: Beginner
User Rating: By 7 Users
Compatibility: C, C++ (general)
Views: 499
 
     Teach you how to write programs for creating 3-D objects on the screen using the smallest possible code

This article has accompanying files
 
				



The 3 Dimensions

By Ė Gaurav Dhup

www.gauravcreations.cjb.net

 

 

Hello and welcome to the first tutorial I have ever written in my life. This tutorial will teach you how to create 3-D objects through C++ although you can use the 3-D functions in other languages too but I am taking up C++ here.

 

Before I start I assume that you have some little knowledge of the basic graphics functions in Turbo C++ v3.0 i.e. drawing a line, circle etc . . . . Please note that I am using the TC++ v3.0 compiler here and have not tested it on any other C++ compiler.

 

Now letís start with a program. It will be better if I take up a program and explain it in detail instead of giving all the boring theoretical information.

 

The program which we will develop is ROTATING 3-D PYRAMID. Donít be nervous by the name itís a very simple program with only a 50 line code! I have even attached the code along with this tutorial

 

OK Letís Start!

 

/* This is just including the header files */

 

#include<graphics.h>

#include<conio.h>

#include<dos.h>

#include<math.h>

 

/* End of Header Files */

 

# define pi 3.141592736; //Declaring a constant named pi

 

float cosa,cosb,sina,sinb; // Declaring variables

 

/* Till now it was simple you do that in any C++ program. From now onwards starts the CORE */

 

/* We use the function below for rotation in both horizontal and vertical direction All we have to do is pass the angle in degrees to this function and we will get the corresponding values */

 

void ini(float cos1,float cos2)

{

 cosa=cos(cos1*pi/180);

 cosb=cos(cos2*pi/180);

 sina=sin(cos1*pi/180);

 sinb=sin(cos2*pi/180);

}

 

/* The function below takes 3 values i.e the values for the 3 Co-ordinates (x,y,z) and returns us the values which we actually plot on the screen i.e it takes the 3-D values and converts them in to a 2-D value which when plotted on the screen seems 3-D */

 

void plot(int x,int y,int z,int &x1,int &y1)

{

 x1=x*cosa-y*sina;

 y1=x*sina*sinb+y*cosa*sinb+z*cosb;

 x1=320+int(x1);          

 y1=240-int(y1);

}

 

/* Above we add 320 and 240 respectively to x1 and y1 to shift the co-ordinate system to the centre of the screen */

 

/* The above 2 functions are what you will only need to create any 3-D object */

 

void main()

{

 

  /* Here we are initializing the graphics */

 

int gd=DETECT,gm;

  initgraph(&gd,&gm,"c:\\tc\\bgi"); /* Please enter the path of your bgi directory.In my case it is ďc:\\tc\\bgiĒ.It may be different in your case */

 

  int p=0; /* Variable passed to the ini function above i.e. it contains the angle in degrees*/

 

while(!kbhit())

 {

ini(p,p); /* Here we are calling the ini function which is responsible for the rotation.You can try with different parameters here

                  ini(p,0) Ė Horizontal Rotation

                  ini(0,p) Ė Vertical Rotation

                  ini(p,p) Ė Both Veretical and Horizontal Rotation */

 

int x[5],y[5];  /*Declaration of array used for storing the converted x,y,z values by the plot function */

 

          

            /* Below 4 lines are responsible for plotting the co-ordinates of the base of the Pyramid */

 

plot(5,5,1,x[0],y[0]);

           plot(45,5,1,x[1],y[1]);

       plot(5,45,1,x[3],y[3]);

       plot(45,45,1,x[4],y[4]);

      

       setcolor(RED); // Sets the color of the lines as RED

 

/* Below 4 lines are responsible for actually drawing the base of the pyramid from the values x & y returned by the plot function. Different co-ordinates of the base(4 pts.) are stored in the array declared above*/

 

       

line(x[3],y[3],x[4],y[4]);

line(x[1],y[1],x[4],y[4]);

       line(x[0],y[0],x[3],y[3]);

      line(x[0],y[0],x[1],y[1]);

 

  /* Below we plot the top of the pyramid and from there using the for loop

      join the peak to the 4 co-ordinates of the base hence completing the pyramid*/   

 

       plot(20,20,140,x[2],y[2]);

 

       for(int i=0;i<5;i++)

          {

            setcolor(BROWN);

            line(x[2],y[2],x[i],y[i]);

                }

 

p+=3; /* Incrementing the angle for rotation by 3 degrees.This also alters the speed of rotation try increasing or decreasing this value */

           

delay(20);

            cleardevice(); /* If we donít use cleardevice here then the pyramid will leave marks all around itís rotation path. */

  }

}

 

 

This is it you have made your first 3-D program in just 50 lines!

Easy isnít it. If you want to have a look at some complex 3-D programs which I have created Please have a look at www.gauravcreations.cjb.net  .The programs are available for download with the source code.

 

You are free to ask my anything about the above tutorial!

 

Please Since this is my first tutorial do give a FEEDBACK and VOTE for it too. It will encourage me to write more tutorials J

 

 

HAPPY CODING!!!

 

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Other 10 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.