1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
//--- Vertex.h
#ifndef VERTEX_H
#define VERTEX_H
class Vertex {
private:
int x, y;
public:
Vertex() : x(0), y(0) {};
Vertex(int px, int py) : x(px), y(py) {};
~Vertex();
int Xvrd() { return x; };
int Yvrd() { return y; };
friend class Polygon; // Not necesary, just in case
};
#endif
//--- Shape.h
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
#include "Vertex.h"
using namespace std;
class Shape {
protected:
Vertex PosSh;
int AntPlg, AntRec, AntCir, AntPoi;
public:
Shape(const Shape &s) : AntPlg(0), AntRec(0), AntCir(0), AntPoi(0) {};
virtual ~Shape();
virtual double area() = 0;
virtual void printShape();
};
#endif
//--- Polygon.h
#ifndef POLYGON_H
#define POLYGON_H
#include <iostream>
#include "Vertex.h"
#include "Shape.h"
using namespace std;
class Polygon : public Shape {
private:
Vertex *Pol;
int pX, pY, size;
public:
Polygon();
Polygon(int x, int y, Vertex *varr, int num);
~Polygon();
void add(Shape &s);
double area();
void printPol();
};
#endif
//--- Polygon.cpp
#include <iostream>
#include <cmath>
using namespace std;
#include "polygon.h"
#include "Vertex.h"
class Shape;
Polygon::Polygon(int x, int y, Vertex *varr, int num) : pX(x), pY(y), size(num) {
if (size > 0) {
Shape::PosSh = Vertex(pX,pY);
Pol = new Vertex[size];
for(int ix=0; ix<size; ix++)
Pol[ix] = varr[ix];
} else {
Pol=0;}
}
Polygon::~Polygon() {}
double Polygon::area() {
double s = 0.0;
double r = 0.0;
for (int ix=1; ix<size; ix++)
s += (Pol[ix-1].x * Pol[ix].y) - (Pol[ix].x * Pol[ix-1].y);
r = s / 2;
return abs(r);
}
void Polygon::printPol() {
for(int ix=0; ix<size; ix++)
cout << "(" <<Pol[ix].x << "," << Pol[ix].y << ")";
}
//--- Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include "Vertex.h"
#include "Shape.h"
using namespace std;
class Circle : public Shape{
private:
Vertex *CircPos;
int pX, pY;
double radius;
public:
Circle();
Circle(int x, int y, int radie);
~Circle();
void add(Shape &s);
virtual double area();
virtual void printCir();
};
#endif
//--- Circle.cpp
#include <iostream>
#include "Circle.h"
#include "Vertex.h"
using namespace std;
class Shape;
Circle::Circle(int x, int y, int radie) : pX(x), pY(y), radius(radie) {
Shape::AntCir++;
if (radius > 0){
CircPos = new Vertex[Shape::AntCir];
CircPos[Shape::AntCir] = Vertex(pX, pY);
} else { CircPos=0; }
}
Circle::~Circle() {}
double Circle::area() { return M_PI*radius*radius; }
void Circle::printCir(){}
| |