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
|
#include <iostream>
#include <cmath>
#include "Vertex.h"
#include "Shape.h"
using namespace std;
class Polygon : public Shape {
private:
Vertex* Pol;
int storlek, pX, pY;
public:
Polygon::Polygon(int x = 0, int y = 0, Vertex* varr = 0, int num = 0) : pX(x), pY(y), storlek(num) {
Pol = new Vertex[storlek];
for (int ix=0; ix<storlek; ix++)
Pol[ix] = varr[ix];
}
virtual Polygon::~Polygon() { delete[] Pol; }
virtual Shape* Polygon::clone() const { return new Polygon(pX, pY, Pol, storlek); }
double Polygon::objArea() const {
double s = 0.0;
for (int ix=1; ix<storlek; ix++)
s += (Pol[ix -1].Xv() * Pol[ix].Yv()) - (Pol[ix].Xv() * Pol[ix -1].Yv());
return abs(s / 2);
}
void Polygon::printShape() const {
cout << "POLYGON: (" << pX << "," << pY << ") { ";
for (int ix=0; ix<storlek; ix++)
cout << "(" << Pol[ix].Xv() << ", " << Pol[ix].Yv() << ") ";
cout << "}" << endl;
}
};
class Circle: public Shape {
private:
int pX, pY;
double radius;
public:
Circle::Circle(int x, int y, int radie) : pX(x), pY(y), radius(radie) {}
virtual Circle::~Circle() {}
virtual Shape* Circle::clone() const { return new Circle(pX, pY, radius); }
double Circle::area() { return M_PI * radius * radius; }
void Circle::printShape() {
cout << "CIRCLE: (" << pX << ", " << pY << "), r=" << radius << endl;
}
};
class Rectangle : public Shape {
private:
int pX, pY, bredd, hojd;
public:
Rectangle::Rectangle(int x, int y, int width, int height) : pX(x), pY(y), bredd(width), hojd(height) {}
virtual Rectangle::~Rectangle() {}
virtual Shape* Rectangle::clone() const {
return new Rectangle(pX, pY, bredd, hojd);
}
double Rectangle::area() { return bredd * hojd; }
void Rectangle::printShape() {
cout << "RECTANGLE: (" << pX << ", " << pY << "), w=" << bredd << ", h=" << hojd << endl;
}
};
class Point : public Shape {
private:
int AntPoi, pX, pY, Langd;
public:
Point::Point(int x, int y, int size) : pX(x), pY(y), Langd(size) {}
virtual Point::~Point() {}
virtual Shape* Point::clone() const { return new Point(pX, pY, Langd); }
double Point::area() { return Langd; }
void Point::printShape() {
cout << "POINT: (" << pX << ", " << pY << "), s=" << Langd << endl;
}
};
| |