-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88c1655
commit b08db55
Showing
1 changed file
with
67 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,69 @@ | ||
#include "figures.hpp" | ||
#include <cmath> | ||
|
||
static constexpr double PI = 3.14; | ||
Rect::Rect(double width, double height) : width(width), height(height) {} | ||
|
||
FigureType Rect::Type() const { | ||
return FigureType::RECTANGLE; | ||
} | ||
|
||
double Rect::Perimeter() const { | ||
return 2 * (width + height); | ||
} | ||
|
||
double Rect::Area() const { | ||
return width * height; | ||
} | ||
|
||
Triangle::Triangle(double sideA, double sideB, double sideC) | ||
: sideA(sideA), sideB(sideB), sideC(sideC) {} | ||
|
||
FigureType Triangle::Type() const { | ||
return FigureType::TRIANGLE; | ||
} | ||
|
||
double Triangle::Perimeter() const { | ||
return sideA + sideB + sideC; | ||
} | ||
|
||
double Triangle::Area() const { | ||
double semiPerimeter = Perimeter() / 2; | ||
return std::sqrt(semiPerimeter * (semiPerimeter - sideA) * (semiPerimeter - sideB) * (semiPerimeter - sideC)); | ||
} | ||
|
||
Circle::Circle(double radius) : radius(radius) {} | ||
|
||
FigureType Circle::Type() const { | ||
return FigureType::CIRCLE; | ||
} | ||
|
||
double Circle::Perimeter() const { | ||
return 2 * PI * radius; | ||
} | ||
|
||
double Circle::Area() const { | ||
return PI * radius * radius; | ||
} | ||
|
||
std::unique_ptr<Figure> make_figure(FigureType type, double paramA, double paramB, double paramC) { | ||
if (paramA < 0 || paramB < 0 || paramC < 0) { | ||
throw LessThanZeroParam("One or more parameters are less than zero"); | ||
} | ||
|
||
switch (type) { | ||
case FigureType::RECTANGLE: | ||
return std::make_unique<Rect>(paramA, paramB); | ||
|
||
case FigureType::TRIANGLE: | ||
if (paramA + paramB <= paramC || paramB + paramC <= paramA || paramA + paramC <= paramB) { | ||
throw WrongTriangle("Invalid triangle sides"); | ||
} | ||
return std::make_unique<Triangle>(paramA, paramB, paramC); | ||
|
||
case FigureType::CIRCLE: | ||
return std::make_unique<Circle>(paramA); | ||
|
||
default: | ||
return nullptr; | ||
} | ||
} |