Code:
//------define file usage------//
#include<dos.h>
#include<math.h>
#include<time.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphic.h>
//------define constants and input ports------//
#define porta 0x300 //add porta 8255
#define portb 0x301 //add portb 8255
#define portc 0x302 //add portc 8255
#define control 0x303 //add control port 8255
#define maxdistance 5100.0
#define vlight 330.0
#define X 320
#define Y 240
int main(void)
{
clrscr();
unsigned int dataA, dataB, dataC;
float D1, D2, J2, D3, J3, yp, yo, zo, xp, ya;
float rad1, rad, costeta, cospsi, sinpsi, dig1, angleteta, angle;
double t1, t2, t3, tm, xo, h, deltax, slope, a, b, c;
int B, C, y1, digit, xaxis, Yaxis, Zaxis, xpaxis, ypaxis;
char buffer[20];
//------define input and control port------//
outportb(control,0x9b); //control port
dataA=inportb(0x300); //input port bit a
dataB=inportb(0x301); //input port bit b
dataC=inportb(0x302); //input port bit c
//------calculations for psi angle------//
for(dig1=0 ; dig1<=90 ; dig1++)
{
rad1=dig1*3.1412/180.0;
//------calculations for distance D1,D2,D3 from input data------//
tm=maxdistance/vlight; //max time,tm
t1=(dataA/99)*tm; //time for x count,tn
t2=(dataB/99)*tm;
t3=(dataC/99)*tm;
D1=t1*vlight; //distance of x count,Dn
D2=t2*vlight;
D3=t3*vlight;
//------display of value of distance and input data------//
clrscr();
textcolor(BROWN);
textbackground(CYAN);
printf("\n\t******************************************");
printf("\n\t\tDISTANCE AND LOCATION OF LIGHTNING");
printf("\n\t******************************************");
printf("\n\n\tData from portA[heksadecimal] = %x",dataA);
printf("\n\tData from portB[heksadecimal] = %x",dataB);
printf("\n\tData from portC[heksadecimal] = %x",dataC);
printf("\n\n\tDistance of lightning from detector A = %.1f",D1);
printf("\n\tDistance of lightning from detector B = %.1f",D2);
printf("\n\tDistance of lightning from detector C = %.1f",D3);
//------calculation of angle towards 2nd distance, J2------//
xp=1000; //distance between detector
costeta=((D1*D1)+(xp*xp)-(D2*D2))/(2*xp*D1); //[3.3]
rad=acos(costeta); //[3.4]
angleteta=rad*180.0/3.1412; //angle in degree
h=D1*sin(rad); //[3.5]
xo=D1*cos(rad); //[3.6]
J2=sqrt(h*h+(xp-xo)*(xp-xo)); //[3.7]
//------calculation of 3rd distance comparison, J3------//
cospsi=cos(rad1);
sinpsi=sin(rad1);
yo=h*cospsi; //[3.9]
zo=h*sinpsi; //[3.10]
yp=sqrt(xp*xp-xp/2*xp/2); //[3.8]
J3=sqrt(h*h+yp*yp-h*yp*cospsi); //[3.13]
//------display of xo,yo,zo, distance of B and C from calculations and teta and psi angle------//
printf("\n\n\tDistance of detector B form calculations = %.2f",J2);
printf("\n\tDistance of detector C form calculations = %.2f",J3);
printf("\n\n\tcoordinate of xo = %.2f",xo);
printf("\n\tcoordinate of yo = %.2f",yo);
printf("\n\tcoordinate of zo = %.2f\n",zo);
printf("\n\tangle from detector A = %.2f",angleteta);
printf("\n\tangle at xplane = %.2f\n",dig1);
}
stop;
getch();
//------request auto detection------//
int gdriver=DETECT,EGA,gmode=EGAHI,errorcode;
//------start of graphic------//
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
//------xaxis------//
line(X,Y,X+250,Y);
moveto(X+260,Y);
outtext("X");
//------zaxis------//
line(X,Y,X,Y-250);
moveto(X-20,Y-240);
outtext("Z");
//------yaxis and its slope------//
a=(Y-350);
c=(X-100);
slope=a/c;
line(X,Y,100,350);
moveto(100-10,350-10);
outtext("Y");
//------scale graph of 3 dimension------//
setcolor(GREEN0;
rectangle(40,130,200,200);
moveto(50,140);
outtext("scale");
moveto(50,160);
outtext(xaxis=5:102");
moveto(50,170);
outtext(yaxis=41:850");
moveto(50,180);
outtext(xaxis=5:102");
Xaxis=(xo/5100*250);
Yaxis=(xo/5100*250);
Zaxis=(xo/5100*246);
xpaxis=(xp/5100*250);
ypaxis=(yp/5100*246);
//------actual coordinate------//
moveto(44,240);
sprintf(buffer,"(%d,%d,%d)"Xaxis,Yaxis,Zaxis);
outtext(buffer);
moveto(44,220);
outtext("coordinate");
setcolor(RED);
//------xo location------//
line(X,Y,X+Xaxis,Y);
//------zo location------//
line(X,Y,X,Y-Zaxis);
//------yo location------//
//slope=(Y-y1)/(X-(X+Yaxis))
y1=Y-slope*Yaxis;
line(X,Y,X-Yaxis,y1);
//------plot location------//
setcolor(YELLOW);
B=(X+Xaxis)-((Y-y1)/slope);
//------distance detector A------//
line(X,Y,B,Y-Zaxis);
//------distance detector B------//
line(X+xpaxis,Y,B,Y-Zaxis);
//------distance detector C------//
C=Y-slope*ypaxis;
line(B,Y-Zaxis,X+xpaxis/2-ypaxis,C);
//------graph title------//
setcolor(GREEN);
moveto(120,420);
outtext("Display 3D to detect location of lightning");
//------clenup------//
getch();
closegraph();
cleardevice();
return();
}