\brief CheckIntersection \param pt1 \param pt2 \return
| 363 | /// \return |
| 364 | /// |
| 365 | bool CheckIntersection(cv::Point2f pt1, cv::Point2f pt2) const |
| 366 | { |
| 367 | const float eps = 0.00001f; // Epsilon for equal comparing |
| 368 | |
| 369 | // First line equation |
| 370 | float a1 = 0; |
| 371 | float b1 = 0; |
| 372 | bool trivial1 = false; // Is first line is perpendicular with OX |
| 373 | |
| 374 | if (fabs(m_pt1.x - m_pt2.x) < eps) |
| 375 | { |
| 376 | trivial1 = true; |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | a1 = (m_pt2.y - m_pt1.y) / (m_pt2.x - m_pt1.x); |
| 381 | b1 = (m_pt2.x * m_pt1.y - m_pt1.x * m_pt2.y) / (m_pt2.x - m_pt1.x); |
| 382 | } |
| 383 | |
| 384 | // Second line equation |
| 385 | float a2 = 0; |
| 386 | float b2 = 0; |
| 387 | bool trivial2 = false; // Is second line is perpendicular with OX |
| 388 | |
| 389 | if (fabs(pt1.x - pt2.x) < eps) |
| 390 | { |
| 391 | trivial2 = true; |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | a2 = (pt2.y - pt1.y) / (pt2.x - pt1.x); |
| 396 | b2 = (pt2.x * pt1.y - pt1.x * pt2.y) / (pt2.x - pt1.x); |
| 397 | } |
| 398 | |
| 399 | // Intersection coords |
| 400 | cv::Point2f intersectPt; |
| 401 | |
| 402 | bool isIntersect = true; |
| 403 | if (trivial1) |
| 404 | { |
| 405 | if (trivial2) |
| 406 | isIntersect = (fabs(m_pt1.x - pt1.x) < eps); |
| 407 | else |
| 408 | intersectPt.x = m_pt1.x; |
| 409 | |
| 410 | intersectPt.y = a2 * intersectPt.x + b2; |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | if (trivial2) |
| 415 | { |
| 416 | intersectPt.x = pt1.x; |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | if (fabs(a2 - a1) > eps) |
| 421 | intersectPt.x = (b1 - b2) / (a2 - a1); |
| 422 | else |