Saturday, June 2, 2012

CODE FOR DRAWING A 3d CUBE IN GRAPHICS WITH C++ BUILDER


in drawing a 2d object ,it is easy and can be done simply by joining lines.However 3d drawing is not as easy as 2d.we must be aware of projection in this case .At  first 3d object's coordinate should be given and this coordinate is projected in to 2d ,Thus we see a 3d view in a 2d plane.


HERE IS THE SOURCE CODE FOR DRAWING 3D CUBE

/////  This code is for initializing structure//////

struct point
{
int x;
int y;
int z;
};

struct point1
{
float x;
float y;
float z;
}

////  rotation of the 3d point////
point yrotate(point p1,int theta)
{
point p3; float angle;
angle=M_PI*theta/180;
p3.x=p1.x*cos(angle)+p1.z*sin(angle);
p3.y=p1.y;
p3.z=-p1.x*sin(angle)+p1.z*cos(angle);
return(p3);
}


///projectiion of 3d to 2d////
 point projection(float x_angle,float y_angle,float z_angle,point p1)
{
    float x1,y1;
    float horz_scale=0.4;
 float vert_scale=0.4;


 x_angle=M_PI*(x_angle)/180;
 y_angle=M_PI*(y_angle)/180;
 z_angle=M_PI*(z_angle)/180;

 x1=p1.x*cos(x_angle)+p1.y*cos(y_angle)+p1.z*cos(z_angle);
 y1=p1.x*sin(x_angle)+p1.y*sin(y_angle)+p1.z*sin(z_angle);

   p1.x=x1*horz_scale;
   p1.y=y1*vert_scale;
   return(p1);
   }



////// here you can form button of any name and include the following code inside it.Here i have formed  a button name drawClick ,You can give any name and include it/////

void __fastcall TForm1::drawClick(TObject *Sender)
{
point p1,p2,p3,p4,p5,p6,p7,p8,p;
float i1[700],i2[700],intensity;float color;
int deno;
p1.x=200;//{200;200;500};
p1.y=200;
p1.z=500;
p2.x=200;//{200,200,200};
p2.y=200;
p2.z=200;
p3.x=200;//{200,500+100,200};
p3.y=600;
p3.z=200;
p4.x=200;//{200,500+100,500};
p4.y=600;
p4.z=500;

p6.x=500+100;//{500+100,200,500};
p6.y=200;
p6.z=500;
p5.x=500+100;//{500+100,500+100,500};
p5.y=620;
p5.z=500;
p7.x=500+100;//{500+100,200,200};
p7.y=500;
p7.z=200;

p8.x=600;
p8.y= 200;
p8.z= 200;


//assigning intensity levels
i1[0]=1;i2[0]=.5;
i1[p4.y-p1.y]=.2;i2[p3.y-p2.y]=0.2;
for(int i=0;i<=p4.y-p1.y-1;i++)
{
i1[i+1]=i1[i]-(i1[0]-i1[p4.y-p1.y])/(p4.y-p1.y);
i2[i+1]=i2[i]-(i2[0]-i2[p3.y-p2.y])/(p3.y-p2.y);
//ListBox1->Items->Add(i2[i]);
}

for(int i=0;i<=p4.y-p1.y;i++)
{
for(int j=p1.z;j>=p2.z;j--)
{
deno=p2.z-p1.z;
intensity=(j-p2.z)*i1[i]/deno+((p1.z-j)*i2[i])/deno;
color=intensity*255;
//ListBox1->Items->Add(color);

color=abs(color);
if(color>=255)color=100;
if(color<=40)color=004;
p.x=p1.x;p.y=i+p1.y;p.z=j;
p=projection(0,90,45,p);
Canvas->Pixels[p.x][p.y]=RGB(200,color,color);
//Form1->Caption=i;
}
}

//front face

i1[0]=1;i2[0]=.5;
i1[p4.y-p1.y]=.2;i2[p5.y-p6.y]=0.2;
for(int i=0;i<=p4.y-p1.y-1;i++)
{
i1[i+1]=i1[i]-(i1[0]-i1[p4.y-p1.y])/(p4.y-p1.y);
i2[i+1]=i2[i]-(i2[0]-i2[p5.y-p6.y])/(p5.y-p6.y);
//ListBox1->Items->Add(i2[i]);
}

for(int i=0;i<=p4.y-p1.y;i++)
{
for(int j=p1.x;j<=p6.x;j++)
{
deno=p6.x-p1.x;
intensity=(j-p1.x)*i2[i]/deno+((p6.x-j)*i1[i])/deno;
color=intensity*255;
//ListBox1->Items->Add(color);

color=abs(color);
if(color>=255)color=100;
if(color<=40)color=004;
p.x=j;p.y=i+p1.y;p.z=p1.z;
p=projection(0,90,45,p);
Canvas->Pixels[p.x][p.y]=RGB(200,color,color);
//Form1->Caption=i;
}



}




//top face

i1[0]=1;i2[0]=.5;//i1[0] at p1 and i2[0] at p6
i1[p1.z-p2.z]=.2;i2[p6.z-p7.z]=0.2;
for(int i=0;i<=p1.z-p2.z-1;i++)
{
i1[i+1]=i1[i]-(i1[0]-i1[p1.z-p2.z])/(p1.z-p2.z);
i2[i+1]=i2[i]-(i2[0]-i2[p6.z-p7.z])/(p6.z-p7.z);
//ListBox1->Items->Add(i2[i]);
}

for(int i=0;i<=p1.z-p2.z;i++)
{
for(int j=p1.x;j<=p6.x;j++)
{
deno=p6.x-p1.x;
intensity=(j-p1.x)*i2[i]/deno+((p6.x-j)*i1[i])/deno;
color=intensity*255;
//ListBox1->Items->Add(color);

color=abs(color);
if(color>=255)color=100;
if(color<=40)color=004;
p.x=j;p.y=p1.y;p.z=-i+p1.z;
p=projection(0,90,45,p);
Canvas->Pixels[p.x][p.y]=RGB(200,color,color);
}
}
}

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Well, interesting post and amazing stuff here. I enjoyed reading your blog. Thanks for sharing....
    packers and movers in hyderabad

    ReplyDelete
  3. i have problem with "point yrotate(point p1,int theta)"
    Turbo C++ :Declaration syntax error
    i'm just copy so i don't know why :(

    ReplyDelete