Saturday, June 2, 2012
BACK FACE DETECTION CODE IN GRAPHICS IN C++ BUILDER IDE
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "math.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
int ang;
struct point
{
int x;
int y;
int z;
};
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);
}
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);
}
void bhm(int x1,int y1,int x2,int y2,int col)
{
int dx,dy,p,kx,ky;
dx=abs(x1-x2);
dy=abs(y1-y2);
p=2*dy-dx;
Form1->Canvas->Pixels[x1][y1]=col;
//DRAWING VERTICAL LINES
if(dx==0)
{
//TO DETERMINE THE +VE OR -VE SLOPE
ky=dy/(y2-y1);
for(int i=0;i<=dy;i++)
{
Form1->Canvas->Pixels[x1][y1]=col;
y1=y1+ky;
}
return;
}
//DRAWING HORIZONTAL LINES
if(dy==0)
{
//TO DETERMINE THE +VE OR -VE SLOPE
kx=dx/(x2-x1);
for(int i=0;i<=dx;i++)
{
Form1->Canvas->Pixels[x1][y1]=col;
x1=x1+kx;
}
return;
}
//BRESENHAM ALGORITHM FOR LINE DRAWING FOR m<1
if(dx>dy)
{
//TO DETERMINE THE +VE OR -VE SLOPE
kx=dx/(x2-x1);
ky=dy/(y2-y1);
for(int i=0;i<=dx;i++)
{
x1=x1+kx;
if(p<0)p=p+2*dy;
else
{
y1=y1+ky;
p=p+2*dy-2*dx;
}
Form1->Canvas->Pixels[x1][y1]=col;
}
return;
}
//BRESENHAM ALGORITHM FOR LINE DRAWING FOR m>1
if(dx<dy)
{
p=2*dx-dy;
//TO DETERMINE THE +VE OR -VE SLOPE
kx=dx/(x2-x1);
ky=dy/(y2-y1);
for(int i=0;i<=dy;i++)
{
y1=y1+ky;
if(p<0)p=p+2*dx;
else
{
x1=x1+kx;
p=p-2*dy+2*dx;
}
Form1->Canvas->Pixels[x1][y1]=col;
}
}
if(dx==dy)
{
for(int i=0;i<=dy;i++)
{
//TO DETERMINE THE +VE OR -VE SLOPE
kx=dx/(x2-x1);
ky=dy/(y2-y1);
y1=y1+ky;
x1=x1+kx;
Form1->Canvas->Pixels[x1][y1]=col;
}
return;
}
}
void __fastcall TForm1::zbufferClick(TObject *Sender)
{
Form1->Refresh();
ang+=10;
point p1,p2,p3,p4,p5,p6,p7,p8;
float ak1,ak2,ai1,ai2,aj1,aj2,bk1,bk2,bi1,bi2,bj1,bj2,ck1,ck2,ci1,ci2,cj1,cj2,dk1,dk2,di1,di2,dj1,dj2;
int diffx,diffy,x1,x2,x3,y1,y2,y3,z1,z2,z3;float checkx,checky,checkz;
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=500;//{500,200,500};
p3.y=200;
p3.z=500;
p4.x=500;//{500,500,500};
p4.y=500;
p4.z=500;
p5.x=500;//{500,200,200};
p5.y=200;
p5.z=200;
p6.x=500;//{500,500,200};
p6.y=500;
p6.z=200;
p7.x=200;//{200,500,200};
p7.y=500;
p7.z=200;
p8.x=200;//{200,500,500};
p8.y=500;
p8.z=500;
p1=yrotate(p1,ang);
p2=yrotate(p2,ang);
p3=yrotate(p3,ang);
p4=yrotate(p4,ang);
p5=yrotate(p5,ang);
p6=yrotate(p6,ang);
p7=yrotate(p7,ang);
p8=yrotate(p8,ang);
p1=projection(0,90,20,p1);//oblique projection
p2=projection(0,90,20,p2);
p3=projection(0,90,20,p3);
p4=projection(0,90,20,p4);
p5=projection(0,90,20,p5);
p6=projection(0,90,20,p6);
p7=projection(0,90,20,p7);
p8=projection(0,90,20,p8);
diffx=p1.x-200;//translation for rotation about fixed point
diffy=p1.y-200;
p1.x=200;p1.y=200;
p2.x=p2.x-diffx;p2.y=p2.y-diffy;
p3.x=p3.x-diffx;p3.y=p3.y-diffy;
p4.x=p4.x-diffx;p4.y=p4.y-diffy;
p5.x=p5.x-diffx;p5.y=p5.y-diffy;
p6.x=p6.x-diffx;p6.y=p6.y-diffy;
p7.x=p7.x-diffx;p7.y=p7.y-diffy;
p8.x=p8.x-diffx;p8.y=p8.y-diffy;
x1=p1.x;y1=p1.y;z1=p1.z;
x2=p3.x;y2=p3.y;z2=p3.z;
x3=p4.x;y3=p4.y;z3=p4.z;
ak1=(float)y1*(z2-z3)+y2*(z3-z1)+y3*(z1-z2);
bk1=(float)z1*(x2-x3)+z2*(x3-x1)+z3*(x1-x2);
ck1=(float)x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);
dk1=(float)-x1*(y2*z3-y3*z2)-x2*(y3*z1-y1*z3)-x3*(y1*z2-y2*z1);
x1=p6.x;y1=p6.y;z1=p6.z;
x2=p7.x;y2=p7.y;z2=p7.z;
x3=p2.x;y3=p2.y;z3=p2.z;
ak2=(float)y1*(z2-z3)+y2*(z3-z1)+y3*(z1-z2);
bk2=(float)z1*(x2-x3)+z2*(x3-x1)+z3*(x1-x2);
ck2=(float)x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);
dk2=(float)-x1*(y2*z3-y3*z2)-x2*(y3*z1-y1*z3)-x3*(y1*z2-y2*z1);
x1=p1.x;y1=p1.y;z1=p1.z;
x2=p3.x;y2=p3.y;z2=p3.z;
x3=p5.x;y3=p5.y;z3=p5.z;
aj1=(float)y1*(z2-z3)+y2*(z3-z1)+y3*(z1-z2);
bj1=(float)z1*(x2-x3)+z2*(x3-x1)+z3*(x1-x2);
cj1=(float)x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);
dj1=(float)-x1*(y2*z3-y3*z2)-x2*(y3*z1-y1*z3)-x3*(y1*z2-y2*z1);
x1=p8.x;y1=p8.y;z1=p8.z;
x2=p4.x;y2=p4.y;z2=p4.z;
x3=p6.x;y3=p6.y;z3=p6.z;
aj2=(float)y1*(z2-z3)+y2*(z3-z1)+y3*(z1-z2);
bj2=(float)z1*(x2-x3)+z2*(x3-x1)+z3*(x1-x2);
cj2=(float)x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);
dj2=(float)-x1*(y2*z3-y3*z2)-x2*(y3*z1-y1*z3)-x3*(y1*z2-y2*z1);
x1=p3.x;y1=p3.y;z1=p3.z;
x2=p5.x;y2=p5.y;z2=p5.z;
x3=p6.x;y3=p6.y;z3=p6.z;
ai1=(float)y1*(z2-z3)+y2*(z3-z1)+y3*(z1-z2);
bi1=(float)z1*(x2-x3)+z2*(x3-x1)+z3*(x1-x2);
ci1=(float)x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);
di1=(float)-x1*(y2*z3-y3*z2)-x2*(y3*z1-y1*z3)-x3*(y1*z2-y2*z1);
x1=p7.x;y1=p7.y;z1=p7.z;
x2=p8.x;y2=p8.y;z2=p8.z;
x3=p1.x;y1=p1.y;z1=p1.z;
ai2=(float)y1*(z2-z3)+y2*(z3-z1)+y3*(z1-z2);
bi2=(float)z1*(x2-x3)+z2*(x3-x1)+z3*(x1-x2);
ci2=(float)x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);
di2=(float)-x1*(y2*z3-y3*z2)-x2*(y3*z1-y1*z3)-x3*(y1*z2-y2*z1);
if(ai1==0)ai1=0.00000001;
if(ai2==0)ai2=0.00000001;
if(ck2==0)ck2=0.00000001;
if(ck1==0)ck1=0.00000001;
checkz=((ak2/ck2)-(ak1/ck1))*200+((bk2/ck2)-(bk1/ck1))*200-(dk1/ck1)+(dk2/ck2);
checky=((aj2/bj2)-(aj1/bj1))*200+((cj2/bj2)-(cj1/bj1))*200-(dj1/bj1)+(dj2/bj2);
checkx=((ci2/ai2)-(ci1/ai1))*200+((bi2/ai2)-(bi1/ai1))*200-(di1/ai1)+(di2/ai2);
//Form1->Caption=checkx;
if(checkz>0)
{
bhm(p1.x,p1.y,p3.x,p3.y,clBlue);
bhm(p3.x,p3.y,p4.x,p4.y,clBlue);
bhm(p4.x,p4.y,p8.x,p8.y,clBlue);
bhm(p8.x,p8.y,p1.x,p1.y,clBlue);
int diffi=p8.y-p1.y;
for(int i=0;i<=diffi;i++)
{
bhm(p1.x,p1.y+i,p3.x,p3.y+i,clGreen);
}
}
if(checkx<0&&(int)checkx!=-1461&& (int)checkx!=-635&&(int)checkx!=-352&&(int)checkx!=-217)
{
bhm(p3.x,p3.y,p5.x,p5.y,clBlue);
bhm(p5.x,p5.y,p6.x,p6.y,clBlue);
bhm(p6.x,p6.y,p4.x,p4.y,clBlue);
bhm(p4.x,p4.y,p3.x,p3.y,clBlue);
int diffi=p4.y-p3.y;
for(int i=0;i<=diffi;i++)
{
bhm(p3.x,p3.y+i,p5.x,p5.y+i,clBlue);
}
}
if(checky>0)
{
bhm(p8.x,p8.y,p4.x,p4.y,clBlue);
bhm(p4.x,p4.y,p6.x,p6.y,clBlue);
bhm(p6.x,p6.y,p7.x,p7.y,clBlue);
bhm(p7.x,p7.y,p8.x,p8.y,clBlue);
int diffi=p8.x-p7.x;
for(int i=0;i<=20;i++)
{
bhm(p8.x-i,p8.y,p1.x-i,p1.y,clRed);
}
}
if(checkz<0)
{
bhm(p6.x,p6.y,p7.x,p7.y,clBlue);
bhm(p7.x,p7.y,p2.x,p2.y,clBlue);
bhm(p2.x,p2.y,p5.x,p5.y,clBlue);
bhm(p5.x,p5.y,p6.x,p6.y,clBlue);
int diffi=p6.y-p5.y;
for(int i=0;i<=diffi;i++)
{
bhm(p5.x,p5.y+i,p2.x,p2.y+i,clGreen);
}
}
if(checkx>0&&checkx!=426971993473024&&checkx!=428400002990080&& (int)checkx!=764&&(int)checkx!=405&&(int)checkx!=273&&(int)checkx!=199)
{
bhm(p7.x,p7.y,p8.x,p8.y,clBlue);
bhm(p8.x,p8.y,p1.x,p1.y,clBlue);
bhm(p1.x,p1.y,p2.x,p2.y,clBlue);
bhm(p2.x,p2.y,p7.x,p7.y,clBlue);
int diffi=p7.y-p2.y;
for(int i=0;i<=diffi;i++)
{
bhm(p2.x,p2.y+i,p1.x,p1.y+i,clBlue);
}
}
if(checkx==426971993473024||checkx==428400002990080|| (int)checkx==764||(int)checkx==405||(int)checkx==273||(int)checkx==199)
{
bhm(p3.x,p3.y,p5.x,p5.y,clBlue);
bhm(p5.x,p5.y,p6.x,p6.y,clBlue);
bhm(p6.x,p6.y,p4.x,p4.y,clBlue);
bhm(p4.x,p4.y,p3.x,p3.y,clBlue);
int diffi=p4.y-p3.y;
for(int i=0;i<=diffi;i++)
{
bhm(p3.x,p3.y+i,p5.x,p5.y+i,clBlue);
}
}
if((int)checkx==-1461|| (int)checkx==-635||(int)checkx==-352||(int)checkx==-217)
{
bhm(p7.x,p7.y,p8.x,p8.y,clBlue);
bhm(p8.x,p8.y,p1.x,p1.y,clBlue);
bhm(p1.x,p1.y,p2.x,p2.y,clBlue);
bhm(p2.x,p2.y,p7.x,p7.y,clBlue);
int diffi=p7.y-p2.y;
for(int i=0;i<=diffi;i++)
{
bhm(p2.x,p2.y+i,p1.x,p1.y+i,clBlue);
}
}
if(checky<0)
{
Form1->Caption=p1.z;
;
bhm(p1.x,p1.y,p3.x,p3.y,clBlue);
bhm(p3.x,p3.y,p5.x,p5.y,clBlue);
bhm(p5.x,p5.y,p2.x,p2.y,clBlue);
bhm(p2.x,p2.y,p1.x,p1.y,clBlue);
Form1->Canvas->MoveTo(p1.x,p1.y);
Form1->Canvas->LineTo(p5.x,p5.y);
Form1->Canvas->MoveTo(p2.x,p2.y);
Form1->Canvas->LineTo(p3.x,p3.y);
Form1->Canvas->MoveTo((p1.x+p3.x)/2,(p1.y+p3.y)/2);
Form1->Canvas->LineTo((p2.x+p5.x)/2,(p2.y+p5.y)/2);
Form1->Canvas->MoveTo((p2.x+p1.x)/2,(p2.y+p1.y)/2);
Form1->Canvas->LineTo((p3.x+p5.x)/2,(p3.y+p5.y)/2);
}
}
Subscribe to:
Post Comments (Atom)
Well, interesting post and amazing stuff here. I enjoyed reading your blog. Thanks for sharing....
ReplyDeletepackers and movers in hyderabad