GoralWMoro

Untitled

Jan 28th, 2020
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include "cmath"
  4. #include <vector>
  5.  
  6. using namespace std;
  7. class Line;
  8. class Shape;
  9. class Triangle;
  10. class Quadrangle;
  11. class Point{
  12. private:
  13. double x;
  14. double y;
  15.  
  16. public:
  17. Point(double x, double y){
  18. this->x = x;
  19. this->y = y;
  20. }
  21.  
  22. double getX(){
  23. return this->x;
  24. }
  25.  
  26. double getY(){
  27. return this->y;
  28. }
  29.  
  30. double getDistance(Point *punkt){
  31.  
  32. return sqrt(pow((punkt->getX() - this->getX()), 2) + pow((punkt->getY() - this->getY()), 2));
  33.  
  34. }
  35. };
  36.  
  37. class Line{
  38. private:
  39. Point* A;
  40. Point* B;
  41. public:
  42. Line(Point *A, Point *B){
  43. this->A = A;
  44. this->B = B;
  45. }
  46.  
  47. double getA(){
  48.  
  49. return (this->B->getY() - this->A->getY())/(this->B->getX() - this->A->getX());
  50.  
  51. }
  52.  
  53. double getB(){
  54. double y = (this->A->getY() - (this->A->getY() - this->B->getY())/(this->A->getX() - this->B->getX()) * this->A->getX());
  55.  
  56. return y;
  57. }
  58.  
  59.  
  60. double getLength(){
  61.  
  62. return sqrt(pow((this->B->getX() - this->A->getX()), 2) + pow((this->B->getY() - this->A->getY()), 2));
  63. }
  64.  
  65.  
  66.  
  67. };
  68.  
  69. class Shape{
  70. protected:
  71. vector<Point*> punkty;
  72. public:
  73. Shape(){
  74. };
  75.  
  76. virtual double getArea()=0;
  77.  
  78. void addPoint(Point *punkt){
  79. this->punkty.push_back(punkt);
  80. }
  81.  
  82. Point *getPoint(int i){
  83. return this->punkty[i];
  84. }
  85.  
  86. double getCircumference(){
  87. double obw = 0;
  88.  
  89. for(int i = 0; i < punkty.size(); i++){
  90. if(i == punkty.size() - 1){
  91. obw += getPoint(i)->getDistance(getPoint(0));
  92. }else {
  93. obw += getPoint(i)->getDistance(getPoint(i+1));
  94. }
  95.  
  96. }
  97. return obw;
  98. }
  99.  
  100.  
  101.  
  102. };
  103.  
  104. class Triangle : public Shape{
  105. public:
  106. Triangle(Point *punktA, Point *punktB, Point *punktC){
  107. addPoint(punktA);
  108. addPoint(punktB);
  109. addPoint(punktC);
  110. }
  111.  
  112. double getArea(){
  113.  
  114.  
  115. double a = getPoint(0)->getDistance(getPoint(1));
  116. double b = getPoint(1)->getDistance(getPoint(2));
  117. double c = getPoint(2)->getDistance(getPoint(0));
  118.  
  119. double p = 0.5*(a+b+c);
  120.  
  121. return sqrt(p *(p-a)*(p-b)*(p-c));
  122. }
  123. };
  124.  
  125. class Quadrangle : public Shape{
  126. public:
  127. Quadrangle(Point *punktA, Point *punktB, Point *punktC, Point *punktD){
  128. addPoint(punktA);
  129. addPoint(punktB);
  130. addPoint(punktC);
  131. addPoint(punktD);
  132. }
  133.  
  134. double getArea() {
  135. Triangle *trojkat1 = new Triangle(getPoint(0),getPoint(1),getPoint(2));
  136. Triangle *trojkat2 = new Triangle(getPoint(3), getPoint(0), getPoint(2));
  137.  
  138. return trojkat1->getArea() + trojkat2->getArea();
  139. }
  140.  
  141.  
  142. };
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. int main() {
  151.  
  152. return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment