#include <stdio.h>
#include <math.h>
int getTarget(float radars[3][3], float radarValues[3], float **target);
float getDistance(float pointA[3], float pointB[3]);
float triangleCalculation(float r1, float r2, float l);
void proectionCoordinates(float pointA[3], float pointB[3], float length, float distance, float *pointC);
void normalVector(float pointA[3], float pointB[3], float pointC[3], float *vector);
int main()
{
//init radars array
int i,j;
float radars[3][3]={{0,0,0},{10,2,0},{10,-2,0}};
float targets[3][2][3], **target;
//set radars values
float radarValues[3][3]={{11.5,2.5,2.5},{12,3,3},{13,4,4}};
float radarValue[3];
printf("Hello, world\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
radarValue[j]=radarValues[i][j];
}
if(!getTarget(radars, radarValue, **target)){
return 0;
}
}
return 0;
}
/*Function calculates coordinates of target*/
int getTarget(float radars[3][3], float radarValues[3], float **target)
{
float l1, l2, x1, x2, h1, h2, nvl=0;
float point1[3], point2[3], point3[3], vn[3], t1[3], t2[3], r1[3], r2[3], r3[3];
int i,j;
printf("Radar coordinats are:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%.2f",radars[i][j]);
if(j==2)
{
printf(" Distance are %.2f\n",radarValues[i]);
}
else
{
printf(", ");
}
}
}
for(i=0;i<3;i++)
{
r1[i]=radars[0][i];
r2[i]=radars[1][i];
r3[i]=radars[2][i];
}
//get l1
l1 = getDistance(r2,r3);
printf("l1 is %.2f\n",l1);
if(radarValues[1]+radarValues[2]<l1)
{
printf("Incorrect values\n");
return 0;
}
//get first proection length
x1=triangleCalculation(radarValues[1], radarValues[2], l1);
printf("x1 is %.2f\n",x1);
//get first proection coords
proectionCoordinates(r2, r3, x1, l1, point1);
printf("point1 is %.2f,%.2f,%.2f\n",point1[0],point1[1],point1[2]);
//calculate height for proection
h1=sqrt(pow(radarValues[1],2)-pow(x1,2));
printf("h1 is %.2f\n",h1);
//do the same for second proection
l2=getDistance(r1, point1);
printf("l2 is %.2f\n",l2);
if(radarValues[0]+h1<l2)
{
printf("Incorrect values\n");
return 0;
}
//get first proection length
x2=triangleCalculation(radarValues[0], h1, l2);
printf("x2 is %.2f\n",x2);
//get first proection coords
proectionCoordinates(r1, point1, x2, l2, point2);
printf("point2 is %.2f,%.2f,%.2f\n",point2[0],point2[1],point2[2]);
//calculate height for proection
h2=sqrt(pow(radarValues[0],2)-pow(x2,2));
printf("h2 is %.2f\n",h2);
//find normal vector for plane
normalVector(r1, r2, r3, vn);
printf("vn is %.2f,%.2f,%.2f\n",vn[0],vn[1],vn[2]);
//find coords of target
for(i=0;i<3;i++)
{
point3[i]=point2[i]+vn[i];
nvl+=pow(vn[i],2);
}
proectionCoordinates(point2, point2, h2, nvl, t1);
printf("t1 is %.2f,%.2f,%.2f\n",t1[0],t1[1],t1[2]);
proectionCoordinates(point2, point2, -h2, nvl, t2);
printf("t2 is %.2f,%.2f,%.2f\n",t2[0],t2[1],t2[2]);
for(i=0;i<3;i++)
{
target[0][i]=t1[i];
target[1][i]=t2[i];
}
printf("target[0] is %.2f,%.2f,%.2f\n",target[0][0],target[0][1],target[0][2]);
printf("Target coordinates are:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%.2f",target[i][j]);
if(j==2)
{
printf("\n");
}
else
{
printf(", ");
}
}
}
return 1;
}
/*Function calculates distance between two points*/
float getDistance(float pointA[3], float pointB[3])
{
float suareSum=0;
int i;
for(i=0;i<3;i++)
{
suareSum+=pow(pointA[i]-pointB[i],2);
}
return fabs(sqrt(suareSum));
}
/*Function calculates length of proection*/
float triangleCalculation(float r1, float r2, float l)
{
float r12,r22,l2;
r12=pow(r1,2);
r22=pow(r2,2);
l2=pow(l,2);
if(r12>r22+l2)
{
return l+(r12-r22-l2)/2/l;
}
else if(r22>r12+l2)
{
return -(r22-r12-l2)/2/l;
}
else
{
return (r12-r22+l2)/2/l;
}
}
/*Function calculates coordinates of proection*/
void proectionCoordinates(float pointA[3], float pointB[3], float length, float distance, float *pointC)
{
int i;
float k;
k=length/distance;
for(i=0;i<3;i++)
{
pointC[i]=k*(pointB[i]-pointA[i])+pointA[i];
}
}
/*Function calculates normal verctor for plane*/
void normalVector(float pointA[3], float pointB[3], float pointC[3], float *vector)
{
float v1[3],v2[3];
int i;
for(i=0;i<3;i++)
{
v1[i]=pointC[i]-pointA[i];
v2[i]=pointB[i]-pointA[i];
}
vector[0]=v1[1]*v2[2]-v2[1]*v1[2];
vector[1]=v1[2]*v2[0]-v2[2]*v1[0];
vector[2]=v1[0]*v2[1]-v2[0]*v1[1];
}