/
polytech_kolomna
/
aipackaging_math
Обзор
Документация
Войти
/
polytech_kolomna
/
aipackaging_math
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
intersections
src/geometry/intersections.cpp
42 строки
2 KB
Matvey
пересечение двух отрезков
17 дек 2025, 15:29
17 дек 2025, 15:29
ac94ffb
Код
Авторство
О чём код?
#include "intersections.h" #include "boundedcurve2d.h" #include "mathutils.h" #include "point2d.h" #include <memory> #include <cmath> namespace { double Determinant( double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { return (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3); } } // bool BoundedCurveIntersection( const std::shared_ptr<BoundedCurve2D>& bounded1, const std::shared_ptr<BoundedCurve2D>& bounded2 ){ Point2D A = bounded1->getPoint(bounded1->getMinT()); //минимальный параметр первого отрезка Point2D B = bounded1->getPoint(bounded1->getMaxT()); //максимальный параметр первого отрезка Point2D C = bounded2->getPoint(bounded2->getMinT()); //минимальный параметр второго отрезка Point2D D = bounded2->getPoint(bounded2->getMaxT()); ////максимальный параметр второго отрезка // вычисляем определитель double det = Determinant( A.x, A.y, B.x, B.y, // отрезок AB C.x, C.y, D.x, D.y // отрезок CD ); // определитель близок к нулю - линии параллельны if (std::fabs(det) < MathConstants::TOLERANCE_DOUBLE) { return false; } // вычисляем параметр t на первой линии и u double t = ((C.x - A.x) * (D.y - C.y) - (C.y - A.y) * (D.x - C.x)) / det; double u = ((C.x - A.x) * (B.y - A.y) - (C.y - A.y) * (B.x - A.x)) / det; // параметры должны быть в пределах от [0,1] return (t >= -MathConstants::TOLERANCE_DOUBLE && t <= 1.0 + MathConstants::TOLERANCE_DOUBLE && u >= -MathConstants::TOLERANCE_DOUBLE && u <= 1.0 + MathConstants::TOLERANCE_DOUBLE); }