Skip to content

Commit

Permalink
Качалов Михаил - Лабораторная Работа №2 (#408)
Browse files Browse the repository at this point in the history
* Create lab2

* Change lab2

* Change lab2

* Change lab2

* Change lab2

* Change lab 2
  • Loading branch information
KachalovM authored Nov 12, 2024
1 parent 9277d49 commit f26b2b6
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 0 deletions.
10 changes: 10 additions & 0 deletions modules/kachalov_m_calc_polygon/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Declare variables for binaries' names
get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
set(MODULE "${DIR_NAME}")
set(LIBRARY "lib_${MODULE}")
set(TESTS "test_${MODULE}")

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(src)
add_subdirectory(test)
20 changes: 20 additions & 0 deletions modules/kachalov_m_calc_polygon/include/polygon_area_calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 Kachalov Mikhail

#ifndef MODULES_KACHALOV_M_CALC_POLYGON_INCLUDE_POLYGON_AREA_CALCULATOR_H_
#define MODULES_KACHALOV_M_CALC_POLYGON_INCLUDE_POLYGON_AREA_CALCULATOR_H_

#include <vector>
#include <utility>

class PolygonAreaCalculator {
public:
void addVertex(double x, double y);

double calculateArea() const;
void clearVertices();

private:
std::vector<std::pair<double, double>> vertices;
};

#endif // MODULES_KACHALOV_M_CALC_POLYGON_INCLUDE_POLYGON_AREA_CALCULATOR_H_
18 changes: 18 additions & 0 deletions modules/kachalov_m_calc_polygon/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set(target ${LIBRARY})

file(GLOB srcs "*.cpp")
file(GLOB hdrs "../include/*.h")
set_source_files_properties(${srcs} ${hdrs} PROPERTIES
LABELS "${MODULE};Library")

add_library(${target} STATIC ${srcs} ${hdrs})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME ${MODULE}
LABELS "${MODULE};Library")

if (UNIX)
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT})
endif (UNIX)
target_link_libraries(${target} ${LIBRARY_DEPS})

set(LIBRARY_DEPS "${LIBRARY_DEPS};${target}" PARENT_SCOPE)
30 changes: 30 additions & 0 deletions modules/kachalov_m_calc_polygon/src/polygon_area_calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 Kachalov Mikhail
#include "include/polygon_area_calculator.h"

#include <cmath>
#include <stdexcept>

void PolygonAreaCalculator::addVertex(double x, double y) {
vertices.emplace_back(x, y);
}

void PolygonAreaCalculator::clearVertices() {
vertices.clear();
}

double PolygonAreaCalculator::calculateArea() const {
if (vertices.size() < 3) {
throw std::logic_error("A polygon must have at least 3 vertices");
}

double area = 0.0;
int n = vertices.size();

for (int i = 0; i < n; ++i) {
int j = (i + 1) % n;
area += (vertices[i].first * vertices[j].second
- vertices[i].second * vertices[j].first);
}

return std::abs(area) * 0.5;
}
27 changes: 27 additions & 0 deletions modules/kachalov_m_calc_polygon/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
set(target ${TESTS})

file(GLOB srcs "*.cpp")
set_source_files_properties(${srcs} PROPERTIES
LABELS "${MODULE};Test")

add_executable(${target} ${srcs} ${hdrs})
set_target_properties(${target} PROPERTIES
LABELS "${MODULE};Test")

if (UNIX)
target_link_libraries(${target} gtest ${CMAKE_THREAD_LIBS_INIT} pthread)
endif (UNIX)
target_link_libraries(${target} gtest ${LIBRARY})

# VS2012 doesn't support correctly the tuples yet,
# see http://code.google.com/p/googletest/issues/detail?id=412
if(MSVC)
target_compile_definitions(${target} PUBLIC _VARIADIC_MAX=10)
endif()

add_test(
NAME ${MODULE}_gtest
COMMAND ${target}
)
set_tests_properties (${MODULE}_gtest PROPERTIES
LABELS "${MODULE}")
8 changes: 8 additions & 0 deletions modules/kachalov_m_calc_polygon/test/test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2024 Kachalov Mikhail

#include <gtest/gtest.h>

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
147 changes: 147 additions & 0 deletions modules/kachalov_m_calc_polygon/test/test_polygon_area.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright 2024 Kachalov Mikhail

#include <gtest/gtest.h>

#include <cmath>

#include "include/polygon_area_calculator.h"

TEST(Kachalov_PolygonAreaCalculator, CalculatesTriangleArea) {
PolygonAreaCalculator areaCalculator;
areaCalculator.addVertex(0.0, 0.0);
areaCalculator.addVertex(4.0, 0.0);
areaCalculator.addVertex(0.0, 3.0);

double calculatedArea = areaCalculator.calculateArea();

double expectedArea = 6.0;
EXPECT_NEAR(calculatedArea, expectedArea, 1e-5);
}

TEST(Kachalov_PolygonAreaCalculator, CalculatesRectangleArea) {
PolygonAreaCalculator areaCalculator;
areaCalculator.addVertex(0.0, 0.0);
areaCalculator.addVertex(4.0, 0.0);
areaCalculator.addVertex(4.0, 3.0);
areaCalculator.addVertex(0.0, 3.0);

double calculatedArea = areaCalculator.calculateArea();

double expectedArea = 12.0;
EXPECT_NEAR(calculatedArea, expectedArea, 1e-5);
}

TEST(Kachalov_PolygonAreaCalculator, CalculatesPentagonArea) {
PolygonAreaCalculator areaCalculator;
areaCalculator.addVertex(0.0, 0.0);
areaCalculator.addVertex(4.0, 0.0);
areaCalculator.addVertex(5.0, 2.0);
areaCalculator.addVertex(2.0, 4.0);
areaCalculator.addVertex(-1.0, 2.0);

double calculatedArea = areaCalculator.calculateArea();

double expectedArea = 16;
EXPECT_NEAR(calculatedArea, expectedArea, 1e-5);
}

TEST(Kachalov_PolygonAreaCalculator, CalculatesHexagonArea) {
PolygonAreaCalculator areaCalculator;
areaCalculator.addVertex(2.0, 0.0);
areaCalculator.addVertex(1.0, std::sqrt(3.0));
areaCalculator.addVertex(-1.0, std::sqrt(3.0));
areaCalculator.addVertex(-2.0, 0.0);
areaCalculator.addVertex(-1.0, -std::sqrt(3.0));
areaCalculator.addVertex(1.0, -std::sqrt(3.0));

double calculatedArea = areaCalculator.calculateArea();

double expectedArea = 10.3923;
EXPECT_NEAR(calculatedArea, expectedArea, 1e-4);
}

TEST(Kachalov_PolygonAreaCalculator, CalculatesOctagonArea) {
PolygonAreaCalculator areaCalculator;
areaCalculator.addVertex(2.0, 0.0);
areaCalculator.addVertex(std::sqrt(2.0), std::sqrt(2.0));
areaCalculator.addVertex(0.0, 2.0);
areaCalculator.addVertex(-std::sqrt(2.0), std::sqrt(2.0));
areaCalculator.addVertex(-2.0, 0.0);
areaCalculator.addVertex(-std::sqrt(2.0), -std::sqrt(2.0));
areaCalculator.addVertex(0.0, -2.0);
areaCalculator.addVertex(std::sqrt(2.0), -std::sqrt(2.0));

double calculatedArea = areaCalculator.calculateArea();

double expectedArea = 11.3137;
EXPECT_NEAR(calculatedArea, expectedArea, 1e-4);
}

TEST(Kachalov_PolygonAreaCalculator, CalculatesArbitraryPolygonArea) {
PolygonAreaCalculator areaCalculator;
areaCalculator.addVertex(1.0, 1.0);
areaCalculator.addVertex(4.0, 0.0);
areaCalculator.addVertex(5.0, 3.0);
areaCalculator.addVertex(3.0, 4.0);
areaCalculator.addVertex(0.0, 4.0);
areaCalculator.addVertex(0.0, 2.0);

double calculatedArea = areaCalculator.calculateArea();

double expectedArea = 14.5;
EXPECT_NEAR(calculatedArea, expectedArea, 1e-4);
}

TEST(Kachalov_PolygonAreaCalculator, CalculatesDegeneratePolygonArea) {
PolygonAreaCalculator areaCalculator;
areaCalculator.addVertex(0.0, 0.0);
areaCalculator.addVertex(0.0, 0.0);
areaCalculator.addVertex(0.0, 0.0);

double calculatedArea = areaCalculator.calculateArea();

double expectedArea = 0.0;
EXPECT_NEAR(calculatedArea, expectedArea, 1e-5);
}

TEST(Kachalov_PolygonAreaCalculator, CalculatesNonConvexPolygonArea) {
PolygonAreaCalculator areaCalculator;
areaCalculator.addVertex(0.0, 0.0);
areaCalculator.addVertex(2.0, 2.0);
areaCalculator.addVertex(1.0, 1.0);
areaCalculator.addVertex(2.0, 0.0);
areaCalculator.addVertex(0.0, 2.0);

double calculatedArea = areaCalculator.calculateArea();

double expectedArea = 1.0;
EXPECT_NEAR(calculatedArea, expectedArea, 1e-4);
}

TEST(Kachalov_PolygonAreaCalculator, ThrowsExceptionForInsufficientVertices) {
PolygonAreaCalculator areaCalculator;
areaCalculator.addVertex(0.0, 0.0);
areaCalculator.addVertex(1.0, 1.0);

EXPECT_THROW(areaCalculator.calculateArea(), std::logic_error);
}

TEST(Kachalov_PolygonAreaCalculator, ClearsVertices) {
PolygonAreaCalculator areaCalculator;
areaCalculator.addVertex(0.0, 0.0);
areaCalculator.addVertex(4.0, 0.0);
areaCalculator.addVertex(0.0, 3.0);

double calculatedArea = areaCalculator.calculateArea();
double expectedArea = 6.0;
EXPECT_NEAR(calculatedArea, expectedArea, 1e-5);

areaCalculator.clearVertices();
areaCalculator.addVertex(0.0, 0.0);
areaCalculator.addVertex(1.0, 0.0);
areaCalculator.addVertex(0.0, 1.0);

calculatedArea = areaCalculator.calculateArea();
expectedArea = 0.5;
EXPECT_NEAR(calculatedArea, expectedArea, 1e-5);
}

0 comments on commit f26b2b6

Please sign in to comment.