/
polytech_kolomna
/
aipackaging_math
Обзор
Документация
Войти
/
polytech_kolomna
/
aipackaging_math
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
intersections
src/algorithms/mathutils.cpp
139 строк
3 KB
Olegh
lower case
28 окт 2025, 21:08
28 окт 2025, 21:08
037a99f
Код
Авторство
О чём код?
#include <cmath> #include <limits> #include <stdexcept> #include <rect2d.h> #include <vector2d.h> #include <point2d.h> #include <line2d.h> #include <circle2d.h> #include <mathutils.h> // bool doubleEQ(double arg1, double arg2, double epsilon) { return std::fabs(arg2 - arg1) < epsilon; } // double degreesToRadians(double degrees) { return degrees * (MathConstants::PI / 180.0); } // double calculateAngle(const Vector2D& v1, const Vector2D& v2) { return std::atan2(v2.y, v2.x) - std::atan2(v1.y, v1.x); } // double distance(const Point2D& p1, const Point2D& p2) { // Расстояние вычисляется как гипотенуза double distance = std::hypot(p1.x - p2.x, p1.y - p2.y); return distance; } // double distance(const Point2D& p1, const Line2D& line) { // Направляющий вектор линии Vector2D directionLine = line.getPoint(1) - line.getPoint(0); // Вектор от точки до начальной точки линии Vector2D pointVector = (p1 - line.getPoint(0)); // Псевдоскалярное произведение double crossProduct = pointVector.pseudoCrossProduct(directionLine); return std::fabs(crossProduct); } // double distance(const Point2D& p1, const Circle2D& circle) { // Находим крайние точки окружности Point2D leftPoint = circle.getPoint(0); Point2D rightPoint = circle.getPoint(MathConstants::PI); double centerX = (rightPoint.x + leftPoint.x) / 2; double centerY = (rightPoint.y + leftPoint.y) / 2; Point2D center{ centerX,centerY }; // Находим радиус double radius = (leftPoint - center).length(); // Вычисляем расстояние от данной точки до центра окружности double distanceToCenter = distance(p1, center); // Расстояние от точки до окружности return std::fabs(distanceToCenter - radius); } // bool segmentIntersectsCircle(const Point2D& p1, const Point2D& p2, const Point2D& centerCircle, double radius) { // Проверяем коллинеарность const Vector2D directionSegment = p2 - p1; const Vector2D directionToCenter = centerCircle - p1; const double lengthSquare = directionSegment.dotProduct(directionSegment); const double cross = std::abs(directionSegment.pseudoCrossProduct(directionToCenter)); const double distance = cross / std::sqrt(lengthSquare); if (distance > radius) return false; const double dotProduct = directionToCenter.dotProduct(directionSegment); return (dotProduct >= 0 && dotProduct <= lengthSquare); } // bool pointInBox(const Point2D& point, const Rect2D& box) { const Point2D topLeftBox = box.topLeft(); const Point2D bottomRightBox = box.bottomRight(); return ((point.x >= topLeftBox.x && point.x <= bottomRightBox.x && point.y <= topLeftBox.y && point.y >= bottomRightBox.y)); } // Point2D calculateCompositeCenter(const std::vector<Rect2D>& rectangles) { double totalArea = 0.0; double sumCX = 0.0; double sumCY = 0.0; for (const Rect2D& rect : rectangles) { Point2D center = rect.center(); Point2D tl = rect.topLeft(); Point2D br = rect.bottomRight(); double width = std::abs(br.x - tl.x); double height = std::abs(br.y - tl.y); double area = width * height; totalArea += area; sumCX += center.x * area; sumCY += center.y * area; } if (doubleEQ(totalArea, 0)) throw std::invalid_argument("Empty Vector"); return { sumCX / totalArea, sumCY / totalArea }; }