fork download
  1. #define _USE_MATH_DEFINES
  2. #include <math.h>
  3. #include "figures.h"
  4. #include <iostream>
  5. #include <vector>
  6. #include <ostream>
  7.  
  8. //Длина отрезка ab
  9. double length(Figure::Point a, Figure::Point b){
  10. return sqrt(double((a.x-b.x) * (a.x-b.x) + (a.y-b.y)*(a.y-b.y)));
  11. }
  12.  
  13. Circle::Circle(Point center, int rad, int color):
  14. Figure(color), //вызываем конструктор базового класса
  15. center(center), rad(rad) //инициализация полей
  16. {}
  17.  
  18. double Circle::place() const {
  19. #ifdef _DEBUG
  20. std::cerr << "In Circle::place()\n";
  21. #endif
  22. return M_PI * rad * rad;
  23. }
  24.  
  25. double Circle::perimetr() const {
  26. #ifdef _DEBUG
  27. std::cerr << "In Circle::perimetr()\n";
  28. #endif
  29. return 2 * M_PI * rad;
  30. }
  31.  
  32. void Circle::output() const{
  33. std::cout << "<" << "Cricle: " << "x=" << center.x << ", " << "y=" << center.y << ", " << "r=" << rad << ", " << "color=" << color << ">" << std::endl;
  34. }
  35.  
  36.  
  37. //===================================================
  38.  
  39. Triangle::Triangle(Point p1, Point p2, Point p3, int color):
  40. Figure(color),
  41. p1(p1), p2(p2), p3(p3)
  42. {}
  43.  
  44. double Triangle::place() const {
  45. #ifdef _DEBUG
  46. std::cerr << "In Triangle::place()\n";
  47. #endif
  48. double ans = 0.5 * ( (p1.x-p3.x)*(p2.y-p3.y)-(p2.x-p3.x)*(p1.y-p3.y) );
  49. if (ans < 0) {
  50. ans = -ans;
  51. }
  52. return ans;
  53. }
  54.  
  55. double Triangle::perimetr() const {
  56. #ifdef _DEBUG
  57. std::cerr << "In Triangle::perimetr()\n";
  58. #endif
  59. return length(p1,p2) + length(p2,p3) + length(p1,p3);
  60. }
  61.  
  62. void Triangle::output() const{
  63. std::cout << "<" << "Triangle:" << "x1="<< p1.x << ", " <<"y1=" << p1.y << ", " << "x2="<< p2.x << ", " <<"y2=" << p2.y << ", " << "x3="<< p3.x << ", " <<"y3=" << p3.y << ">" << std::endl;
  64.  
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71. //===================================================
  72. Polygon::Polygon(std::vector<Point> vPolygon, int color):
  73. Figure(color),
  74. vPolygon(vPolygon)
  75. {}
  76.  
  77. double Polygon::place() const {
  78. #ifdef _DEBUG
  79. std::cerr << "In Polygon::place()\n";
  80. #endif
  81. double s = 0.0;
  82. for (int i = 0; i<vPolygon.size()-1; i++)
  83. {
  84. s += (vPolygon[i+1].y + vPolygon[i].y)*(vPolygon[i+1].x-vPolygon[i].x)/2;
  85. }
  86. if (s<0) {
  87. s = -s;
  88. }
  89. return s;
  90. }
  91.  
  92. double Polygon::perimetr() const {
  93. #ifdef _DEBUG
  94. std::cerr << "In Polygon::perimetr()\n";
  95. #endif
  96. double p = 0.0;
  97. for (int i = 0; i<vPolygon.size()-1; i++){
  98. p += length(vPolygon[i+1], vPolygon[i]);
  99. }
  100. return p;
  101.  
  102. }
  103.  
  104. void Polygon::output() const{
  105. std::cout<<"Polygon:";
  106. for (int i=0; i < vPolygon.size()-1;i++){
  107. std::cout<<"x"<<i+1<<"="<<vPolygon[i].x<<", "<<"y"<<i+1<<"="<<vPolygon[i].y<<", ";
  108. }
  109. std::cout<<"color="<<color<<">"<<std::endl;
  110.  
  111. }
  112.  
  113. std::ostream & operator << (std::ostream & os, const Figure &f){
  114. f.output();
  115. return os;
  116. }
  117.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:21: fatal error: figures.h: No such file or directory
 #include "figures.h"
                     ^
compilation terminated.
stdout
Standard output is empty