0

I've looked up some formulas relating to finding the distance a point and a line. On this page, I used example 14

http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html

I have a method that has turned into this:

+(bool) checkPointNearBetweenPointsWithPointA:(CGPoint)pointA withPointB:(CGPoint)pointB withPointC:(CGPoint)pointC withLimit:(float)limit {

  float A = pointB.x - pointA.x;
  float B = pointA.y - pointC.y;
  float C = pointA.x - pointC.x;
  float D = pointB.y - pointA.y;

  float dividend = fabs( A * B ) - ( C * D );

  float divisor = sqrt(pow(A,2) + pow(D,2));

  float distanceBetweenPointAndLine = dividend / divisor;

  if(distanceBetweenPointAndLine < limit){

    NSLog(@"distanceBetweenPointAndLine = %f",distanceBetweenPointAndLine);

    return YES;

  }


  return NO;
}

The problem is that it still returns YES if I'm passed point B, if the line segment is drawn like B----A. Other screwed up things happen to depending on which angle the line is drawn. Just wondering if I need to consider anything else if testing to see if a point is near a finite line. Most examples I see online deal with lines of infinite length.

Chewie The Chorkie
  • 4,896
  • 9
  • 46
  • 90
  • A, B, and C: which ones do you think form the line segment and which one is the one you are trying to get the distance from? – matt Mar 26 '14 at 15:24
  • A and B form the line segment. I'm trying to get the distance from C to line. – Chewie The Chorkie Mar 26 '14 at 16:18
  • 1
    Well, the formula that you are using uses the two points A and B to determine a line (not a line segment) and _drops a perpendicular_ to that line and gives the length of that. If that isn't what you want, then don't do that. – matt Mar 26 '14 at 16:42

1 Answers1

0

try my code below. line is considered to exist between points A & B (regardless of how you draw it B->A or A->B ) and point C is the point in consideration to measure the distance.

+ (bool) checkPointNearBetweenPointsWithPointA:(CGPoint)pointA
                                   withPointB:(CGPoint)pointB
                                   withPointC:(CGPoint)pointC
                                    withLimit:(float)limit
{

    CGFloat slopeLine = atan((pointB.y-pointA.y)/(pointB.x-pointA.x) );
    CGFloat slopePointToPointA = -1 *atan((pointC.y-pointA.y)/(pointC.x-pointA.x));

    CGFloat innerAngle = slopeLine + slopePointToPointA;
    CGFloat distanceAC = sqrtf(pow(pointC.y-pointA.y,2) + pow(pointC.x-pointA.x,2));

    CGFloat distanceBetweenPointAndLine = fabs(distanceAC * sin(innerAngle));

    NSLog(@"distanceBetweenPointAndLine = %f",distanceBetweenPointAndLine);
    NSLog(@"is exceeding limit ? %@",distanceBetweenPointAndLine > limit ? @"YES":@"NO");

    if(distanceBetweenPointAndLine < limit)
    {
        return YES;
    }
    return NO;
}
nsuinteger
  • 1,503
  • 12
  • 21
  • For some reason that is behaving as if the line is infinite. – Chewie The Chorkie Mar 26 '14 at 16:25
  • can you post some test data samples you're using or a test case you face this problem ? perhaps it might help to understand your question better. – nsuinteger Mar 26 '14 at 16:26
  • I need to check if the point is near a line segment, not an infinite line. I found answers that worked here: http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment – Chewie The Chorkie Mar 26 '14 at 17:04