What is wrong with my intersection check algorithm?

I know that there are many sites that explain how to check the intersection of two lines, but I am very bored just copying and pasting the code for such a simple math problem. The more I get disappointed, the more I can't get my code to work. I know questions with "What's wrong with my code?" are stupid, but I don’t know what the hell is wrong with my math / code, also my code is well documented (except, in truth, the wrong naming of variables), so I think there should be someone who interested in math behind her

bool segment::checkforIntersection(QPointF a, QPointF b) { //line 1: a+bx, line 2: c+dx, note that a and c are called offset and bx and dx are called gradients in this code
QPointF bx = b-a;
double firstGradient = bx.y() / bx.x(); //gradient of line 1
//now we have to calculate the offset of line 1: we have b from a+bx. Since QPointF a is on that line, it is:
//a + b * a.x = a.y with a as free variable, which yields a = a.y - b*a.x.
//One could also use the second point b for this calculation.
double firstOffset = a.y() - firstGradient * a.x();
double secondGradient, secondOffset;
for (int i = 0; i < poscount-3; i++) { //we dont check with the last line, because that could be the same line, as the one that emited intersection checking
    QPointF c = pos[i];
    QPointF d = pos[i+1];
    QPointF dx = d-c;
    secondGradient = dx.y() / dx.x(); //same formula as above
    secondOffset = c.y() - secondGradient * c.x();
    //a+bx=c+dx <=> a-c = (d-b)x <=> (a-c)/(d-b) = x
    double x = (firstOffset - secondOffset) / (secondGradient - firstGradient);
    //we have to check, if those lines intersect with a x \in [a.x,b.x] and x \in [c.x,d.x]. If this is the case, we have a collision
    if (x >= a.x() && x <= b.x() && x >= c.x() && x <= d.x()) {
        return true;
    }
}
return false;
}

, , 4 a, b, c, d ( 1: a - b, 2: c - d) ( for), x y, , deltay/deltax. , , a ( c ) . , 4 a + bx, ax of 0 , (a/c), ax of 1 , (b/). ( ). , x. , . - ?

, . Positives (, , ), (, ). , , , , , , .

, , ( -), , , - .:)

: , Qt, ++:

#include <iostream>
#include <math.h>

using namespace std;
class Point {
private:
    double xval, yval;
public:
    // Constructor uses default arguments to allow calling with zero, one,
    // or two values.
    Point(double x = 0.0, double y = 0.0) {
        xval = x;
        yval = y;
    }

    // Extractors.
    double x() { return xval; }
    double y() { return yval; }

    Point sub(Point b)
    {
        return Point(xval - b.xval, yval - b.yval);
    }
};

bool checkforIntersection(Point a, Point b, Point c, Point d) { //line 1: a+bx, line 2: c+dx, note that a and c are called offset and bx and dx are called gradients in this code
    Point bx = b.sub(a);
    double firstGradient = bx.y() / bx.x(); //gradient of line 1
    //now we have to calculate the offset of line 1: we have b from a+bx. Since Point a is on that line, it is:
    //a + b * a.x = a.y with a as free variable, which yields a = a.y - b*a.x.
    //One could also use the second point b for this calculation.
    double firstOffset = a.y() - firstGradient * a.x();
    double secondGradient, secondOffset;
    Point dx = d.sub(c);
    secondGradient = dx.y() / dx.x(); //same formula as above
    secondOffset = c.y() - secondGradient * c.x();
    //a+bx=c+dx <=> a-c = (d-b)x <=> (a-c)/(d-b) = x
    double x = (firstOffset - secondOffset) / (secondGradient - firstGradient);
    //we have to check, if those lines intersect with a x \in [a.x,b.x] and x \in [c.x,d.x]. If this is the case, we have a collision
    if (x >= a.x() && x <= b.x() && x >= c.x() && x <= d.x()) {
        return true;
    }
    return false;
}

int main(int argc, char const *argv[]) {
    if (checkforIntersection(Point(310.374,835.171),Point(290.434,802.354), Point(333.847,807.232), Point(301.03,827.172)) == true) {
        cout << "These lines do intersect so I should be printed out\n";
    } else {
        cout << "The algorithm does not work, so instead I do get printed out\n";
    }
    return 0;
}

, ~ (310,835) - (290,802) (333,807) - (301,827). :

\documentclass[crop,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=.1cm,y=.1cm]
\draw (310,835) -- (290,802);
\draw (333,807) -- (301,827);
\end{tikzpicture}
\end{document}

++ ,

+4
2

( , )

, , , , [0,1].

[ a, b],

{x, y}(t) = {(1-t)*ax+t*bx, (1-t)*ay+t*by} t [0,1]

- t=0 a, t=1 b, t, .

, (a, b) (c, d) :

// for some t1 and t2, the x coordinate is the same
(1-t1)*ax+t*bx=(1-t2)*cx+t2*dx; 
(1-t1)*ay+t*by=(1-t2)*cy+t2*dy; // and so is the y coordinate

t1 t2. t1 [0,1], a b, t2 c d.

, :

  • -
  • . , (, )
+1

, deltay/deltax.

, deltax ?

, , - 0.0, .

:

  • , .
  • , .

(a,b) x (c,d) = (ax-bx)*(cy-dy)-(ay-by)*(cx-dx) - , ( , ).

, :

  • " ?" . , ( - , a==b / c==d)
  • " " , - 1 , , ( "" to zero ", ?)
    , ... (, ? ). mmm.. , - hypot(ax-bx, ay-by)*hypot(cx-dx,cy-dy) ( , ?)
  • , , " " -? , , (, {a, b, c, d}), ... - ( ) sin(angle-between-versors), " 1 , , ? ? 1 ??
+1

Source: https://habr.com/ru/post/1654208/


All Articles