Bläddra i källkod

Added Figures

Vsevolod Levitan 11 månader sedan
förälder
incheckning
288aac5f6e

+ 31 - 0
Figures/Figures.sln

@@ -0,0 +1,31 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.11.34909.67
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Figures", "Figures\Figures.vcxproj", "{804D0309-46F7-472A-9E59-EA8EB2BA433D}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|x64 = Debug|x64
+		Debug|x86 = Debug|x86
+		Release|x64 = Release|x64
+		Release|x86 = Release|x86
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{804D0309-46F7-472A-9E59-EA8EB2BA433D}.Debug|x64.ActiveCfg = Debug|x64
+		{804D0309-46F7-472A-9E59-EA8EB2BA433D}.Debug|x64.Build.0 = Debug|x64
+		{804D0309-46F7-472A-9E59-EA8EB2BA433D}.Debug|x86.ActiveCfg = Debug|Win32
+		{804D0309-46F7-472A-9E59-EA8EB2BA433D}.Debug|x86.Build.0 = Debug|Win32
+		{804D0309-46F7-472A-9E59-EA8EB2BA433D}.Release|x64.ActiveCfg = Release|x64
+		{804D0309-46F7-472A-9E59-EA8EB2BA433D}.Release|x64.Build.0 = Release|x64
+		{804D0309-46F7-472A-9E59-EA8EB2BA433D}.Release|x86.ActiveCfg = Release|Win32
+		{804D0309-46F7-472A-9E59-EA8EB2BA433D}.Release|x86.Build.0 = Release|Win32
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {C2B6DF7D-1A30-4EC1-94BD-D702696A66DC}
+	EndGlobalSection
+EndGlobal

+ 24 - 0
Figures/Figures/Figures.cpp

@@ -0,0 +1,24 @@
+#include <iostream>
+#include "Figures.h"
+
+int main()
+{
+    std::vector <GeometricFigure<double>*> figures;
+
+    Circle<double> circle(Point<double>(0, 0), 5.0);
+    Ellipse<double> ellipse(Point<double>(0, 0), 3.0, 2.0);
+    Triangle<double> triangle(Point<double>(0, 0), Point<double>(3, 0), Point<double>(0, 4));
+    Rectangle<double> rectangle(Point<double>(0, 0), Point<double>(3, 0), Point<double>(3, 4), Point<double>(0, 4));
+    Polygon<double> polygon("bounds.txt");
+    figures.push_back(&circle);
+    figures.push_back(&ellipse);
+    figures.push_back(&triangle);
+    figures.push_back(&rectangle);
+    figures.push_back(&polygon);
+
+    for (const auto& figure : figures) {
+        figure->name();
+        std::cout << "Area: " << figure->calc_area() << std::endl;
+        std::cout << "Perimeter: " << figure->calc_perimeter() << std::endl << "\n";
+    }
+}

+ 275 - 0
Figures/Figures/Figures.h

@@ -0,0 +1,275 @@
+#pragma once
+#include <iostream>
+#include <cmath>
+#include <vector>
+#include <fstream>
+#define PI 3.14159265358979323846
+
+
+template<class T>
+class Point {
+public:
+    T x, y;
+    Point(T x = 0, T y = 0) : x(x), y(y) {}
+};
+
+/// <summary>
+/// Represents a geometric figure
+/// </summary>
+/// <typeparam name="T">Number value type</typeparam>
+template<class T>
+class GeometricFigure {
+public:
+    /// <summary>
+    /// Calculate area of the figure
+    /// </summary>
+    /// <returns>The internal area of the figure</returns>
+    virtual T calc_area() = 0;
+
+    /// <summary>
+    /// Calculate perimeter of the figure
+    /// </summary>
+    /// <returns>The external perimeter of the figure</returns>
+    virtual T calc_perimeter() = 0;
+    
+    /// <summary>
+    /// Get name of the figure
+    /// </summary>
+    virtual void name() = 0;
+
+    friend std::ostream& operator << (const std::ostream& out, const GeometricFigure& figure);
+};
+
+template<class T>
+std::ostream& operator << (const std::ostream& out, const GeometricFigure<T>& figure)
+{
+    out << figure.name << " (perimeter: " << figure.calc_perimeter() << ", area: " << figure.calc_area();
+    return out;
+}
+
+/// <summary>
+/// Represents a circle
+/// </summary>
+/// <typeparam name="T">Number value type</typeparam>
+template<class T> class Circle : public GeometricFigure<T> {
+private:
+    Point<T> center;
+    T radius;
+
+public:
+    /// <summary>
+    /// Create a circle by its center point and radius
+    /// </summary>
+    /// <param name="c">Center point of the circle</param>
+    /// <param name="r">Radius of the circle</param>
+    Circle(Point<T> c, T r) : center(c), radius(r) {}
+
+    T calc_area() override;
+    T calc_perimeter() override;
+    void name() override;
+};
+
+template<class T>
+T Circle<T>::calc_area() {
+    return PI * radius * radius;
+}
+
+template<class T>
+T Circle<T>::calc_perimeter() {
+    return 2 * PI * radius;
+}
+
+template<class T>
+void Circle<T>::name() {
+    std::cout << "Circle" << std::endl;
+}
+
+/// <summary>
+/// Represents an ellipse
+/// </summary>
+/// <typeparam name="T">Number value type</typeparam>
+template<class T>
+class Ellipse : public GeometricFigure<T> {
+private:
+    Point<T> center;
+    T a, b;
+
+public:
+    /// <summary>
+    /// Create an ellipse by its center point and two dimension
+    /// </summary>
+    /// <param name="c">Center of the ellipse</param>
+    /// <param name="a">First dimension</param>
+    /// <param name="b">Second dimension</param>
+    Ellipse(Point<T> c, T a, T b) : center(c), a(a), b(b) {}
+
+    T calc_area() override;
+    T calc_perimeter() override;
+    void name() override;
+};
+
+template<class T>
+T Ellipse<T>::calc_area() {
+    return PI * a * b;
+}
+
+template<class T>
+T Ellipse<T>::calc_perimeter() {
+    return PI * (3 * (a + b) - sqrt((3 * a + b) * (a + 3 * b)));
+}
+
+template<class T>
+void Ellipse<T>::name() {
+    std::cout << "Ellipse" << std::endl;
+}
+
+/// <summary>
+/// Represents a triangle
+/// </summary>
+/// <typeparam name="T">Number value type</typeparam>
+template<class T>
+class Triangle : public GeometricFigure<T> {
+private:
+    Point<T> vertices[3];
+
+public:
+    /// <summary>
+    /// Create a triangle by three points
+    /// </summary>
+    /// <param name="a">Point A</param>
+    /// <param name="b">Point B</param>
+    /// <param name="c">Point C</param>
+    Triangle(Point<T> a, Point<T> b, Point<T> c) : vertices{ a, b, c } {}
+
+    T calc_area() override;
+    T calc_perimeter() override;
+    void name() override;
+};
+
+template<class T>
+T Triangle<T>::calc_area() {
+    T a = sqrt((vertices[1].x - vertices[0].x) * (vertices[1].x - vertices[0].x) + (vertices[1].y - vertices[0].y) * (vertices[1].y - vertices[0].y));
+    T b = sqrt((vertices[2].x - vertices[1].x) * (vertices[2].x - vertices[1].x) + (vertices[2].y - vertices[1].y) * (vertices[2].y - vertices[1].y));
+    T c = sqrt((vertices[0].x - vertices[2].x) * (vertices[0].x - vertices[2].x) + (vertices[0].y - vertices[2].y) * (vertices[0].y - vertices[2].y));
+    T s = (a + b + c) / 2;
+    return sqrt(s * (s - a) * (s - b) * (s - c));
+}
+
+template<class T>
+T Triangle<T>::calc_perimeter() {
+    T a = sqrt((vertices[1].x - vertices[0].x) * (vertices[1].x - vertices[0].x) + (vertices[1].y - vertices[0].y) * (vertices[1].y - vertices[0].y));
+    T b = sqrt((vertices[2].x - vertices[1].x) * (vertices[2].x - vertices[1].x) + (vertices[2].y - vertices[1].y) * (vertices[2].y - vertices[1].y));
+    T c = sqrt((vertices[0].x - vertices[2].x) * (vertices[0].x - vertices[2].x) + (vertices[0].y - vertices[2].y) * (vertices[0].y - vertices[2].y));
+    return a + b + c;
+}
+
+template<class T>
+void Triangle<T>::name() {
+    std::cout << "Triangle" << std::endl;
+}
+
+/// <summary>
+/// Represents a rectangle
+/// </summary>
+/// <typeparam name="T">Number value type</typeparam>
+template<class T>
+class Rectangle : public GeometricFigure<T> {
+private:
+    Point<T> vertices[4];
+
+public:
+    /// <summary>
+    /// Create a rectangle by 4 points
+    /// </summary>
+    /// <param name="a">Top-left point</param>
+    /// <param name="b">Bottom-left point</param>
+    /// <param name="c">Bottom-right point</param>
+    /// <param name="d">Top-right point</param>
+    Rectangle(Point<T> a, Point<T> b, Point<T> c, Point<T> d) : vertices{ a, b, c, d } {}
+
+    T calc_area() override;
+    T calc_perimeter() override;
+    void name() override;
+};
+
+template<class T>
+T Rectangle<T>::calc_area() {
+    T width = sqrt((vertices[1].x - vertices[0].x) * (vertices[1].x - vertices[0].x) + (vertices[1].y - vertices[0].y) * (vertices[1].y - vertices[0].y));
+    T height = sqrt((vertices[2].x - vertices[1].x) * (vertices[2].x - vertices[1].x) + (vertices[2].y - vertices[1].y) * (vertices[2].y - vertices[1].y));
+    return width * height;
+}
+
+template<class T>
+T Rectangle<T>::calc_perimeter() {
+    T width = sqrt((vertices[1].x - vertices[0].x) * (vertices[1].x - vertices[0].x) + (vertices[1].y - vertices[0].y) * (vertices[1].y - vertices[0].y));
+    T height = sqrt((vertices[2].x - vertices[1].x) * (vertices[2].x - vertices[1].x) + (vertices[2].y - vertices[1].y) * (vertices[2].y - vertices[1].y));
+    return 2 * (width + height);
+}
+
+template<class T>
+void Rectangle<T>::name() {
+    std::cout << "Rectangle" << std::endl;
+}
+
+/// <summary>
+/// Represents an abstract polygonal figure
+/// </summary>
+/// <typeparam name="T">Number value type</typeparam>
+template<class T>
+class Polygon : public GeometricFigure<T> {
+private:
+    std::vector<Point<T>> vertices;
+
+public:
+    /// <summary>
+    /// Read polygon from file
+    /// </summary>
+    /// <param name="filename">Path to the file</param>
+    Polygon(const std::string& filename);
+
+    T calc_area() override;
+    T calc_perimeter() override;
+    void name() override;
+};
+
+template<class T>
+Polygon<T>::Polygon(const std::string& filename) {
+    std::ifstream file(filename);
+    if (!file.is_open()) {
+        std::cerr << "Error opening file" << std::endl;
+        return;
+    }
+    int numVertices;
+    file >> numVertices;
+    for (int i = 0; i < numVertices; ++i) {
+        T x, y;
+        file >> x >> y;
+        vertices.emplace_back(x, y);
+    }
+    file.close();
+}
+
+template<class T>
+T Polygon<T>::calc_area() {
+    T area = 0.0;
+    int n = vertices.size();
+    for (int i = 0; i < n; ++i) {
+        area += (vertices[i].x * vertices[(i + 1) % n].y) - (vertices[(i + 1) % n].x * vertices[i].y);
+    }
+    return 0.5 * std::abs(area);
+}
+
+template<class T>
+T Polygon<T>::calc_perimeter() {
+    T perimeter = 0.0;
+    int n = vertices.size();
+    for (int i = 0; i < n; ++i) {
+        perimeter += sqrt((vertices[(i + 1) % n].x - vertices[i].x) * (vertices[(i + 1) % n].x - vertices[i].x) + (vertices[(i + 1) % n].y - vertices[i].y) * (vertices[(i + 1) % n].y - vertices[i].y));
+    }
+    return perimeter;
+}
+
+template<class T>
+void Polygon<T>::name() {
+    std::cout << "Polygon" << std::endl;
+}

+ 138 - 0
Figures/Figures/Figures.vcxproj

@@ -0,0 +1,138 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup Label="ProjectConfigurations">
+    <ProjectConfiguration Include="Debug|Win32">
+      <Configuration>Debug</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|Win32">
+      <Configuration>Release</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Debug|x64">
+      <Configuration>Debug</Configuration>
+      <Platform>x64</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|x64">
+      <Configuration>Release</Configuration>
+      <Platform>x64</Platform>
+    </ProjectConfiguration>
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <VCProjectVersion>17.0</VCProjectVersion>
+    <Keyword>Win32Proj</Keyword>
+    <ProjectGuid>{804d0309-46f7-472a-9e59-ea8eb2ba433d}</ProjectGuid>
+    <RootNamespace>Figures</RootNamespace>
+    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <PlatformToolset>v143</PlatformToolset>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <PlatformToolset>v143</PlatformToolset>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <PlatformToolset>v143</PlatformToolset>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <PlatformToolset>v143</PlatformToolset>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+  <ImportGroup Label="ExtensionSettings">
+  </ImportGroup>
+  <ImportGroup Label="Shared">
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <PropertyGroup Label="UserMacros" />
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <SDLCheck>true</SDLCheck>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <SDLCheck>true</SDLCheck>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <SDLCheck>true</SDLCheck>
+      <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <SDLCheck>true</SDLCheck>
+      <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClCompile Include="Figures.cpp" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="Figures.h" />
+  </ItemGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>

+ 27 - 0
Figures/Figures/Figures.vcxproj.filters

@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <Filter Include="Source Files">
+      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+      <Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+    </Filter>
+    <Filter Include="Header Files">
+      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+      <Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
+    </Filter>
+    <Filter Include="Resource Files">
+      <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
+      <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
+    </Filter>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="Figures.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="Figures.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+  </ItemGroup>
+</Project>

+ 79 - 0
Figures/Figures/bounds.txt

@@ -0,0 +1,79 @@
+77	
+19258	4120
+19268	4186
+19143	4747
+19229	4732
+19429	4733
+19426	4287
+19441	4208
+19504	4110
+19437	3814
+19480	3466
+19497	3285
+19530	2804
+19568	2166
+19573	2008
+19552	1801
+19478	1546
+19385	1349
+19442	1249
+19329	1184
+19287	1184
+19181	1003
+19146	899
+19171	678
+19304	483
+19385	313
+19411	119
+19308	-355
+19343	-511
+19482	-638
+19692	-668
+19813	-637
+20185	-218
+20185	190
+20435	190
+20507	-175
+20620	-330
+20742	-747
+20361	-924
+20232	-635
+20100	-601
+19915	-747
+19804	-800
+19547	-815
+19345	-729
+19278	-671
+19236	-618
+19186	-521
+19171	-467
+19160	-370
+19176	-248
+19260	83
+19251	243
+19192	384
+19088	525
+19027	640
+18996	766
+18788	766
+18788	524
+18368	524
+18368	1044
+18788	1044
+18788	805
+18993	805
+18996	896
+19033	1035
+19106	1169
+19165	1268
+18844	1558
+18877	1595
+19063	1428
+19262	1431
+19342	1589
+19404	1785
+19430	1973
+19420	2264
+19383	2875
+19310	3689
+19258	4120