-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Качалов Михаил - Лабораторная Работа №2 (#408)
* Create lab2 * Change lab2 * Change lab2 * Change lab2 * Change lab2 * Change lab 2
- Loading branch information
Showing
7 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
modules/kachalov_m_calc_polygon/include/polygon_area_calculator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
modules/kachalov_m_calc_polygon/src/polygon_area_calculator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
147
modules/kachalov_m_calc_polygon/test/test_polygon_area.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |