2

I have many parallel line segments, for example L1(P1, P2) and L2(P3, P4). The Points have each x and y coordinates. These parallel line segments have varying angles between 0-180 degrees.

How can I efficiantly find the perpendicular space between those line segments in c++? Distance between two parallel line segments

4
  • 2
    What did you try so far?
    – eerorika
    Commented Mar 2, 2015 at 13:20
  • 2
    Are you having trouble deriving the formula? Or expressing that formula in C++? Commented Mar 2, 2015 at 13:30
  • I have trouble expressing the formula in C++, maybe I just need some simple example to start from there
    – baal
    Commented Mar 2, 2015 at 13:37
  • this has helped me: youtube.com/watch?v=KUXbhlAGeok Commented Oct 11, 2020 at 15:12

2 Answers 2

5

The distance between two parallel lines will be the distance between the first (infinite) line and any point (say, P3) on the second line. Since you are working with coordinates, it's more convenient to use the vector representation of the formula than to try to express the line as an equation. Using that representation, in 2d this distance is given by |(P3 - P1) dot ( norm ( P2 - P1 ))|, where norm is a normalized perpendicular to P2 - P1:

enter image description here

Also note, in 2d, a perpendicular to a vector (x, y) is easily given by (-y, x). Thus:

class GeometryUtilities
{
public:
    GeometryUtilities();
    ~GeometryUtilities();

    static double LinePointDistance2D(double lineP1X, double lineP1Y, double lineP2X, double lineP2Y, double pointX, double pointY);

    static void Perpendicular2D(double x, double y, double &outX, double &outY);

    static double Length2D(double x, double y);
};

double GeometryUtilities::LinePointDistance2D(double lineP1X, double lineP1Y, double lineP2X, double lineP2Y, double pointX, double pointY)
{
    double vecx = lineP2X - lineP1X;
    double vecy = lineP2Y - lineP1Y;
    double lineLen = Length2D(vecx, vecy);
    if (lineLen == 0.0) // Replace with appropriate epsilon
    {
        return Length2D(pointX - lineP1X, pointY - lineP1Y);
    }

    double normx, normy;
    Perpendicular2D(vecx/lineLen, vecy / lineLen, normx, normy);
    double dot = ((pointX - lineP1X) * normx + (pointY - lineP1Y) * normy); // Compute dot product (P3 - P1) dot( norm ( P2 - P1 ))
    return abs(dot);
}

void GeometryUtilities::Perpendicular2D(double x, double y, double &outX, double &outY)
{
    outX = -y;
    outY = x;
}

double GeometryUtilities::Length2D(double x, double y)
{
    return sqrt(x*x + y*y);
}

In production you will probably want to introduce some sort of Point class which would beautify this API considerably, however since it isn't shown I wrote the code purely using doubles.

1
  • Thankk you. Haven't thought about using vectors but it was much easier to understand for me. +1 for the example code!
    – baal
    Commented Mar 3, 2015 at 14:49
1

A quick google search yields this wikipedia article. http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line

To compute the distance you need that one line's equation is expressed as ax + by + c = 0. You can then use a point of the other line to compute the distance using the formula given in the wikipedia article.

To obtain a line equation in the form ax + by + c = 0 from two points on the line, use the method described in this web page https://bobobobo.wordpress.com/2008/01/07/solving-linear-equations-ax-by-c-0/ You then obtain the values a, b and c for the line.

Once you have the formula, it is straight forward to convert it to c++.

I would discourage using a line equation in the form mx + b = y because you might find yourself with a case where m is infinite. It will then be very difficult to compute the distance. You don't have this problem when using the equation ax + by + c = 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.