Hi guys!
I have started learning geometry and wrote a code to calculate the distance between a line(or a segment) and a point.
Here is my code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int dot_pdt(int a[],int b[],int c[])
{
int ab[2],bc[2];
ab[0]=b[0]-a[0];
ab[1]=b[1]-a[1];
bc[0]=c[0]-b[0];
bc[1]=c[1]-b[1];
// printf("%d\n",(ab[0]*bc[0]+ab[1]*bc[1]));
return (ab[0]*bc[0]+ab[1]*bc[1]);
}
int cross_pdt(int a[],int b[],int c[])
{
int ab[2],ac[2];
ab[0]=b[0]-a[0];
ab[1]=b[1]-a[1];
ac[0]=c[0]-a[0];
ac[1]=c[1]-a[1];
//printf("%d\n",(abs)(ab[0]*ac[1]-ab[1]*ac[0]));
return (abs)(ab[0]*ac[1]-ab[1]*ac[0]);
}
double distance(int a[],int b[])
{
int d1=b[0]-a[0];
int d2=b[1]-a[1];
//printf("%f\n",sqrt(d1*d1+d2*d2));
return (abs)(sqrt(d1*d1+d2*d2));
}
double linepointdist(int a[],int b[],int c[],int isSegment)
{
double dist=cross_pdt(a,b,c)/distance(a,b);
printf("%f\n",dist);
int d1,d2;
//If the problem is not a line but a segment.
if(isSegment)
{
d1=dot_pdt(a,b,c);
if(d1>0){printf("1\n"); return distance(b,c);}
d2=dot_pdt(b,a,c);
if(d2>0){printf("2\n"); return distance(a,c);}
}
return dist;
}
int main()
{
int a[2],b[2],c[2];
scanf("%d %d %d %d %d %d",&a[0],&a[1],&b[0],&b[1],&c[0],&c[1]);
printf("%f",linepointdist(a,b,c,1));
return 0;
}
the problem is what I get is always an integer not a float value.
I have a input of
0 0 1 2 0 3
I get
1.000000
instead of
1.500000
The bug might be silly but its killing me.:'(
PLZ HELP.