Distance from a point to a line/segment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lauramaria
    New Member
    • Aug 2012
    • 4

    Distance from a point to a line/segment

    I have to compute the distance from a point to a line (check if it is line or a line segment). I am not sure that the bool function IsSegment is working properly. Can i have some suggestions? Thank you.
    Code:
    double Distance_From_Line_to_Point(int *a, int *b, int *c, bool IsSegment)
    {
    	double distance;
    	int dot1;
    	int dot2;
    	distance=Cross_Product(a,b,c) / Distance(a,b);
    	if(IsSegment(a,b,c)==true)
    	{
    		dot1=Dot_Product(a,b,c);
    		if(dot1>0)
    		{
    			return Distance(b,c);
    		}
    		dot2=Dot_Product(b,a,c);
    		if(dot2>0)
    		{
    			return Distance(a,c);
    		}
    	}
    	return fabs(distance);
    }
    bool IsSegment(int *a, int *b, int *c)
    {
    	double angle1;
    	double angle2;
    	angle1= atan(double(b[1]-a[1])/(b[0]-a[0]));
    	angle2= atan(double(c[1]-b[1])/(c[0]-b[0]));
    	if((angle2-angle1)*(180/PI) >90)
    	{
    		return false;
    	}
    	return true;
    }
  • sukumarst
    New Member
    • Aug 2012
    • 1

    #2
    I can understand the meaning of distance between 2 points. What exactly do you mean by distance between a point and a line? You probably need to rephrase your question!

    Comment

    • lauramaria
      New Member
      • Aug 2012
      • 4

      #3


      Line point Distance

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        One obvious problem with IsSegment is that lines 26 or 27 could involve division by zero. Look into using atan2 instead of atan.

        What are you trying to accomplish with IsSegment?

        Comment

        Working...