/
polytech_kolomna
/
polygon2d
Обзор
Документация
Войти
/
polytech_kolomna
/
polygon2d
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
math
tests/tests_math/tests_parabola.cpp
397 строк
11 KB
Aleksandr Mahrov
небольшые изменения
20 янв 2026, 16:30
20 янв 2026, 16:30
08567bd
Код
Авторство
О чём код?
#define _USE_MATH_DEFINES #include <gtest/gtest.h> #include <MathUtils.h> #include <parabola2d.h> #include <iostream> #include <Matrix3.h> #include <vector> #include <cmath> static std::vector<Point2D> set_of_points(const Parabola2D& parabola, int b1, int b2) { std::vector<Point2D> points; for (int t = b1; t <= b2; ++t) { points.push_back(parabola.getPoint(static_cast<double>(t))); } return points; } /////////////////////////////////////////////////////////// //Тесты метода GetPoint /////////////////////////////////////////////////////////// //при параметре t = 0 должен вернуть координаты вершины TEST(GetPoint, ZeroParamReturnsVertex) { double a = 1.0, h = 5.0, k = -5.0; Parabola2D parabola = Parabola2D::VertexForm(a, h, k); Point2D expected = Point2D{ h, k }; Point2D actual = parabola.getPoint(0.0); ASSERT_EQ(expected, actual); } //параметр t = 3 TEST(GetPoint, PositiveParam) { double a = 1.0, h = 5.0, k = -5.0; Parabola2D parabola = Parabola2D::VertexForm(a, h, k); double t = 3.0; Vector2D axis = (a >= 0) ? Vector2D(0, 1) : Vector2D(0, -1); Vector2D axisPerp(-axis.y, axis.x); double p = 1 / (4 * a); Point2D expected = Point2D(h, k) + axisPerp * t + axis * (t * t / (4 * p)); Point2D actual = parabola.getPoint(t); ASSERT_EQ(expected, actual); } //параметр t = -3 TEST(GetPoint, NegativeParam) { double a = 1.0, h = 5.0, k = -5.0; Parabola2D parabola = Parabola2D::VertexForm(a, h, k); double t = -3.0; Vector2D axis = (a >= 0) ? Vector2D(0, 1) : Vector2D(0, -1); Vector2D axisPerp(-axis.y, axis.x); double p = 1 / (4 * a); Point2D expected = Point2D(h, k) + axisPerp * t + axis * (t * t / (4 * p)); Point2D actual = parabola.getPoint(t); ASSERT_EQ(expected, actual); } //при противоположных T y одинаковые ,а x разные TEST(GetPoint,Symmetry) { double a = 1.0, h = 5.0, k = -5.0; Parabola2D parabola = Parabola2D::VertexForm(a, h, k); double t = -3.0; //при T double pointY= parabola.getPoint(t).y; //при -T double pointY1 = parabola.getPoint(t).y; ASSERT_EQ(pointY, pointY1); } //точка близкая к вершине TEST(GetPoint, NearVertex) { double a = 1.0, h = 5.0, k = -5.0; Parabola2D parabola = Parabola2D::VertexForm(a, h, k); double t = 0.001; Vector2D axis = (a >= 0) ? Vector2D(0, 1) : Vector2D(0, -1); Vector2D axisPerp(-axis.y, axis.x); double p = 1 / (4 * a); Point2D expected = Point2D(h, k) + axisPerp * t + axis * (t * t / (4 * p)); Point2D actual = parabola.getPoint(t); ASSERT_EQ(expected, actual); } TEST(GetPoint, LargeTValue) { double a = 1.0, h = 5.0, k = -5.0; Parabola2D parabola = Parabola2D::VertexForm(a, h, k); double t = 1000.0; Vector2D axis = (a >= 0) ? Vector2D(0, 1) : Vector2D(0, -1); Vector2D axisPerp(-axis.y, axis.x); double p = 1 / (4 * a); Point2D expected = Point2D(h, k) + axisPerp * t + axis * (t * t / (4 * p)); Point2D actual = parabola.getPoint(t); ASSERT_EQ(expected, actual); } TEST(GetPoint, TinyTValue) { double a = 1.0, h = 5.0, k = -5.0; Parabola2D parabola = Parabola2D::VertexForm(a, h, k); double t = MathConstants::TOLERANCE_DOUBLE; Vector2D axis = (a >= 0) ? Vector2D(0, 1) : Vector2D(0, -1); Vector2D axisPerp(-axis.y, axis.x); double p = 1 / (4 * a); Point2D expected = Point2D(h, k) + axisPerp * t + axis * (t * t / (4 * p)); Point2D actual = parabola.getPoint(t); ASSERT_EQ(expected, actual); } TEST(GetPoint, LargeNegativeTValue) { double a = 1.0, h = 5.0, k = -5.0; Parabola2D parabola = Parabola2D::VertexForm(a, h, k); double t = -1000.0; Vector2D axis = (a >= 0) ? Vector2D(0, 1) : Vector2D(0, -1); Vector2D axisPerp(-axis.y, axis.x); double p = 1 / (4 * a); Point2D expected = Point2D(h, k) + axisPerp * t + axis * (t * t / (4 * p)); Point2D actual = parabola.getPoint(t); ASSERT_EQ(expected, actual); } /////////////////////////////////////////////////////////// //Сравнение форм задания параболы /////////////////////////////////////////////////////////// TEST(comparison_of_three_forms, param_vertex_general) { Parabola2D p = Parabola2D::ParamForm({ 5,-5 }, 0.25); Parabola2D p1 = Parabola2D::VertexForm(1, 5, -5); Parabola2D p2 = Parabola2D::GeneralForm(1,-10,20); std::vector<Point2D> points_p = set_of_points(p, -2, 2); std::vector<Point2D> points_p1 = set_of_points(p1, -2, 2); std::vector<Point2D> points_p2 = set_of_points(p2, -2, 2); ASSERT_EQ(points_p, points_p1); ASSERT_EQ(points_p, points_p2); } /////////////////////////////////////////////////////////// //Тесты метода Transform /////////////////////////////////////////////////////////// //Translation //Проверяет смещение параболы в положительном направлении TEST(Transform, PositiveTranslation) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; Vector2D v({ 3,6 }); Matrix3 M = Matrix3::translation(v); parabola.transform(M); std::vector<Point2D> expected = { orig.getPoint(-2)*M, orig.getPoint(-1)*M, orig.getPoint(0)*M, orig.getPoint(1)*M, orig.getPoint(2)*M }; std::vector<Point2D> actual = set_of_points(parabola, -2, 2); ASSERT_EQ(expected, actual); } //Проверяет смещение параболы в отрицательном направлении TEST(Transform, NegativeTranslation) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; Vector2D v({ -1,-1 }); Matrix3 M = Matrix3::translation(v); parabola.transform(M); std::vector<Point2D> expected = { orig.getPoint(-2) * M, orig.getPoint(-1) * M, orig.getPoint(0) * M, orig.getPoint(1) * M, orig.getPoint(2) * M }; std::vector<Point2D> actual = set_of_points(parabola, -2, 2); ASSERT_EQ(expected, actual); } //Scale TEST(Transform, ScaleZero) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; Matrix3 M = Matrix3::scale(0, 0); parabola.transform(M); std::vector<Point2D> expected = { orig.getPoint(-2)*M, orig.getPoint(-1) * M, orig.getPoint(0) * M, orig.getPoint(1) * M, orig.getPoint(2) * M}; std::vector<Point2D> actual = set_of_points(parabola, -2, 2); ASSERT_EQ(expected, actual); } TEST(Transform, PositiveScale) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; Matrix3 M = Matrix3::scale(2, 2); parabola.transform(M); std::vector<Point2D> expected = { orig.getPoint(-2) * M, orig.getPoint(-1) * M, orig.getPoint(0) * M, orig.getPoint(1) * M, orig.getPoint(2) * M }; std::vector<Point2D> actual = set_of_points(parabola, -2, 2); ASSERT_EQ(expected, actual); } TEST(Transform, DifferentScaleXY) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; parabola.transform(Matrix3::scale(2, 3)); std::vector<Point2D> actual = set_of_points(parabola, -2, 2); std::vector<Point2D> expected; for (int t = -2; t <= 2; ++t) { Point2D origPoint = orig.getPoint(t); // Масштабирование относительно (0,0): x' = 2*x, y' = 3*y expected.push_back(Point2D(2 * origPoint.x, 3 * origPoint.y)); } ASSERT_EQ(expected, actual); } TEST(Transform, FractionalScale) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; parabola.transform(Matrix3::scale(0.5, 0.25)); std::vector<Point2D> actual = set_of_points(parabola, -2, 2); std::vector<Point2D> expected; for (int t = -2; t <= 2; ++t) { Point2D origPoint = orig.getPoint(t); expected.push_back(Point2D(0.5 * origPoint.x, 0.25 * origPoint.y)); } ASSERT_EQ(expected, actual); } TEST(Transform, ZeroXScale) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; parabola.transform(Matrix3::scale(0, 2)); std::vector<Point2D> actual = set_of_points(parabola, -2, 2); std::vector<Point2D> expected; for (int t = -2; t <= 2; ++t) { Point2D origPoint = orig.getPoint(t); // Все x становятся 0, y масштабируется в 2 раза expected.push_back(Point2D(0, 2 * origPoint.y)); } ASSERT_EQ(expected, actual); } TEST(Transform, ZeroYScale) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; parabola.transform(Matrix3::scale(2, 0)); std::vector<Point2D> actual = set_of_points(parabola, -2, 2); std::vector<Point2D> expected; for (int t = -2; t <= 2; ++t) { Point2D origPoint = orig.getPoint(t); // Все y становятся 0, x масштабируется в 2 раза expected.push_back(Point2D(2 * origPoint.x, 0)); } ASSERT_EQ(expected, actual); } TEST(Transform, NegativeScaleX) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; parabola.transform(Matrix3::scale(-1, 1)); std::vector<Point2D> actual = set_of_points(parabola, -2, 2); std::vector<Point2D> expected; for (int t = -2; t <= 2; ++t) { Point2D origPoint = orig.getPoint(t); // Отражение относительно оси Y: x' = -x, y' = y expected.push_back(Point2D(-origPoint.x, origPoint.y)); } ASSERT_EQ(expected, actual); } TEST(Transform, NegativeScaleY) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; parabola.transform(Matrix3::scale(1, -1)); std::vector<Point2D> actual = set_of_points(parabola, -2, 2); std::vector<Point2D> expected; for (int t = -2; t <= 2; ++t) { Point2D origPoint = orig.getPoint(t); // Отражение относительно оси X: x' = x, y' = -y expected.push_back(Point2D(origPoint.x, -origPoint.y)); } ASSERT_EQ(expected, actual); } TEST(Transform, NegativeScaleBoth) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; parabola.transform(Matrix3::scale(-2, -3)); std::vector<Point2D> actual = set_of_points(parabola, -2, 2); std::vector<Point2D> expected; for (int t = -2; t <= 2; ++t) { Point2D origPoint = orig.getPoint(t); // Отражение и масштабирование: x' = -2*x, y' = -3*y expected.push_back(Point2D(-2 * origPoint.x, -3 * origPoint.y)); } ASSERT_EQ(expected, actual); } //rotate TEST(Transform, Rotation75Degrees) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); Parabola2D parabola = orig; Matrix3 M = Matrix3::rotation(75.0 * M_PI / 180.0); parabola.transform(M); std::vector<Point2D> expected = { orig.getPoint(-2) * M, orig.getPoint(-1) * M, orig.getPoint(0) * M, orig.getPoint(1) * M, orig.getPoint(2) * M }; std::vector<Point2D> actual = set_of_points(parabola, -2, 2); ASSERT_EQ(expected, actual); } TEST(Transform, Rotation360Degrees) { Parabola2D orig = Parabola2D::VertexForm(1, 5, -5); std::vector<Point2D> before = set_of_points(orig, -2, 2); Parabola2D parabola = orig; Matrix3 M = Matrix3::rotation(2 * M_PI); parabola.transform(M); std::vector<Point2D> after = set_of_points(parabola, -2, 2); EXPECT_EQ(before.size(), after.size()); for (size_t i = 0; i < before.size(); ++i) ASSERT_EQ(before[i], after[i]); }