0

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.

6
Contributors
7
Replies
8
Views
9 Years
Discussion Span
Last Post by Prabakar
0

Hi guys!
I have started learning geometry and wrote a code to calculate the distance between a line(or a segment) and a point.

Rewrite your distance function like this:

double distance(int a[],int b[])
{
    double d1=b[0]-a[0];
    double d2=b[1]-a[1];    
    return sqrt(d1*d1+d2*d2);
}

General rule -- if you do something with floating point, do everything with floating point. Implicit type casts works (as usual in C) in strange inpredictable manner.

0

Thanx Tilir I found the bug at last
It was in :

double distance(int a[],int b[])
{
    double d1=b[0]-a[0];
    double d2=b[1]-a[1];
    //printf("%f\n",sqrt(d1*d1+d2*d2));
    return (abs)(sqrt(d1*d1+d2*d2));
}

I removed "abs" from

return (abs)(sqrt(d1*d1+d2*d2));

and whoa!!! it started working I think the "abs" function has some problem let me study about "abs" function first.....

0

> I think the "abs" function has some problem let me study about "abs" function first
1. Use fabs() if you want to use floats.
2. Think about it - sqrt() only returns positive numbers, so why do you need it?

0

Another general rule of thumb:

If you think one of the standard libraries has problems, look at your code again. These libraries are subject to a lot of scrutiny. I'm not saying that there is no possibility of errors in them - it's just much MUCH more likely that

a) you're not using it correctly IE putting an integer value into a function instead of a pointer.

b) it doesn't do what you think it does IE you think its returning something that it isn't.

c) you've got some other coding issue.

0

Implicit type casts works (as usual in C) in strange inpredictable manner.

Since inpredictable is not a word, I assume you mean unpredictable. The problem with this statement is that casts are completely predictable.

Can you give us an example that is strange and unpredictable?

0

Since inpredictable is not a word, I assume you mean unpredictable. The problem with this statement is that casts are completely predictable.

Can you give us an example that is strange and unpredictable?

int i = 5;
double d = i / 2; // expecting d = 2.5, but... division is integer. i / 2.0 will be correct.
printf("%f\n", d);

My lovely one (not about floating point, but...)

unsigned int a = 13;
if (a < -1)
  printf("%s", "Wow!"); // Surprise. Unsigned to signed promotion works. ((int) a < -1) will be correct

Really I'm agree with you -- any of these are completely predictable after styding the standard and some time, spend programming on C. They are unintuitive to novice, that is what I mentioned.

Anyone may read excellent book on C language by Harbison and Steele where the most of such pitfalls are collected. But show me someone who cares...

0

Why do you call this a pitfall? They are just general rules in evaluating expressions, Aren't they?
I just can't thing of any other way to evaluate these expression.
for (5 < -1u) the compiler is bound to return 1.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.