Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Easiest way to find the distance between two parallel QLineF?



  • Before delving deeper and looking at creating perpendicular lines and walking them, is there an easy solution for finding the parallel distance between 2 QLineFs?



  • From the math point of view, I guess the following should work (I'm tempted to say it's impossible to do it "simpler", but implementing the same operations using your own specialized code might be more performant):
    @QLineF normal = line1,normal();.
    QPointF crossPoint;
    if (normal.intersect(line2, &crossPoint) != QLineF::NoIntersection) {
    QLineF distanceLine(line1.p1(), crossPoint);
    qreal distance = distanceLine.length();
    }@
    Please note there is no check for actual parallelity; you can add it be comparison of the inclinations:
    @if (line1.dy() * line2.dx() == line1.dx() * line2.dy()) {
    // parallel
    } else {
    // not parallel
    }@


Log in to reply