Distance between two lines?

I'm trying to write a function that can return the distance between two lines. I have functions written already to check if the line is parallel, horizontal, and vertical. I'm just having trouble getting my head around to code this.

I know that if the lines are not parallel, I'm supposed to return 0 since they intersect. And I think then I should check if the lines are horizontal or vertical? If vertical, subtract their x coordinates. If horizontal, subtract their y coordinates.

I just am struggling to translate my thought process into code.
How are your lines defined?

Using point-slope form is easiest here:

    y-y1 = m(x-x1)

Negate the slope. (This turns the line 90 degrees.)

Compute the intersection of the two lines. (Here's an easy walkthrough I googled for you.)
http://www.mathopenref.com/coordintersection.html

Now you only need to find the distance between the first line's point and the intersection point.

Hope this helps.
its all inside of a big class. this is what i tried to write, but its not working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  double getDistance(Line line2){
    if(getSlope() - line2.getSlope() < DDIFF && getSlope() - line2.getSlope() > -DDIFF){
      if(isVertical()){
        double distance = a.x_coord - b.x_coord;
        return distance;
      }else if(isHorizontal()){
        double distance = a.y_coord - b.y_coord;
        return distance;
      }
    }else if(isVertical() && line2.isVertical()){
      if(isVertical()){
        double distance = a.x_coord - b.x_coord;
        return distance;
      }else if(isHorizontal()){
        double distance = a.y_coord - b.y_coord;
        return distance;
      }
    }else{
      return 0.0;
    }
  }


it's supposed to check if the lines are parallel, and then if they are, find the distance between their points. if they arent parallel, then its supposed to return 0.0
The only thing that matters in the calculation is if the lines are vertical (since the slope is then undefined). Knowing nothing more about your implementation than the code snippet you posted:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  bool isParallelTo( Line line2 ) const
  {
    return std::abs( getSlope() - line2.getSlope() ) < EPSILON;
  }

  double getDistance( Line line2 ) const
  {
    // If the lines are not parallel
    if (!isParallelTo( line2 )) return 0.0;

    // Vertical lines
    if (isVertical()) return std::abs( line2.a.x_coord - a.x_coord );

    // Horizontal lines
    if (isHorizontal()) return std::abs( line2.a.y_coord - a.y_coord );

    // All other lines
    Line line3( a, Point( a.x_coord, a.y_coord + a.y_coord - b.y_coord ) );
    Point p = line3.getIntersection( line2 );
    return p.getDistance( a );
  }

Notice the other methods to help out:
Line 13: we create a copy of line1 rotated 90° at point a
Line 14: we get the point of intersection between the temporary line and line2
Line 15: we return the distance between the two points (line1.a, point of intersection)

You don't have to do line 14 in a separate method, but it'll make your life easier if you do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  Point getIntersection( Line line2 )
  {
    // Lines must not be parallel
    if (isParallel( line2 )) return ???;
    
    // If either is vertical the slope becomes useless, so we need special cases
    if (isVertical()) return Point(
      1/line2.getSlope() * (a.y_coord - line2.y_coord) + line2.x_coord,
        line2.getSlope() * (a.x_coord - line2.x_coord) + line2.y_coord );

    if (line2.isVertical()) return line2.getIntersection( *this );

    // A little algebra gives us the x coordinate of our intersection:
    double x1 = a.x_coord;
    double y1 = a.y_coord;
    double m1 = getSlope();

    double x2 = line2.a.x_coord;
    double y2 = line2.a.y_coord;
    double m2 = line2.getSlope();

    double x = (y2 - m2*x2 - y1 + m1*x1) / (m1-m2);

    // Now we can substitute x in to get y:
    double y = m1*(x-x1) + y1;

    return Point( x, y );
  }

Hope this helps.
Question, have you considered two parallel lines at 30 degrees from the x-axis? Subtracting x or y won't be enough to get the distance for those. You'll want to use the distance formula to find the distance between two points (sqrt((x1-x2)^2 + (y1-y2)^2)).

You may not be given any points where two points are the minimum distance from each other. If the lines are vertical, just subtract the x-coordinates. If the lines are not vertical, you can find the slope-intercept form (y = mx + b, where m is the slope and b is the intercept) of one of the lines, select a point on the other line that remains constant, and loop through a bunch of points on the first line until you find one with the minimum distance between that point and your constant.
Topic archived. No new replies allowed.