Shape testing.
This commit is contained in:
parent
b736074066
commit
b7cb695840
157
main.cpp
157
main.cpp
@ -4,6 +4,7 @@
|
||||
#include <map>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
using string = std::string;
|
||||
using slist = std::list<string>;
|
||||
@ -20,83 +21,76 @@ void printlm(T m){
|
||||
printm("\n");
|
||||
}
|
||||
|
||||
class Heap {
|
||||
protected:
|
||||
int pos {0};
|
||||
int length {0};
|
||||
Heap() {}
|
||||
private:
|
||||
int size;
|
||||
int* storage;
|
||||
class Point {
|
||||
public:
|
||||
Heap(int sz) {
|
||||
size = sz;
|
||||
storage = new int[sz];
|
||||
}
|
||||
virtual ~Heap() {
|
||||
delete[] storage;
|
||||
}
|
||||
bool isEmpty() {
|
||||
return length == 0;
|
||||
}
|
||||
virtual void add(int item) {
|
||||
if (length + 1 > size) return;
|
||||
pos = length;
|
||||
while (pos > 0 && storage[(pos-1)/2] < item) {
|
||||
storage[pos] = storage[(pos-1)/2];
|
||||
pos = (pos-1)/2;
|
||||
}
|
||||
storage[pos] = item;
|
||||
++length;
|
||||
}
|
||||
virtual int extractMax() {
|
||||
int max {storage[0]};
|
||||
storage[0] = storage[--length];
|
||||
int first {0};
|
||||
while ((2*first)+1 <= length - 1) {
|
||||
int largest {(2*first)+1};
|
||||
if ((2*first)+2 <= length - 1 && storage[largest] < storage[(2*first)+2]) largest = (2*first)+2;
|
||||
if (storage[first] >= storage[largest]) break;
|
||||
int swap = storage[first];
|
||||
storage[first] = storage[largest];
|
||||
storage[largest] = swap;
|
||||
first = largest;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
const double x;
|
||||
const double y;
|
||||
Point(double a, double b) : x(a), y(b) {}
|
||||
};
|
||||
|
||||
class HeapInf : public Heap {
|
||||
std::vector<int> storage;
|
||||
double dist(Point a, Point b) {
|
||||
const auto dx = a.x - b.x;
|
||||
const auto dy = a.y - b.y;
|
||||
return sqrt(dx*dx + dy*dy);
|
||||
}
|
||||
|
||||
std::string printp(const Point &p) {
|
||||
return "( " + std::to_string(p.x) + " , " + std::to_string(p.y) + " )";
|
||||
}
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
HeapInf() : Heap() {}
|
||||
virtual ~HeapInf() {}
|
||||
void add(int item) {
|
||||
if (length+1>storage.size()) storage.push_back(0);
|
||||
pos = length;
|
||||
while (pos > 0 && storage[(pos-1)/2] < item) {
|
||||
storage[pos] = storage[(pos-1)/2];
|
||||
pos = (pos-1)/2;
|
||||
}
|
||||
storage[pos] = item;
|
||||
++length;
|
||||
}
|
||||
int extractMax() {
|
||||
int max {(storage.size() > 0) ? storage[0] : 0};
|
||||
storage[0] = storage[--length];
|
||||
int first {0};
|
||||
while ((2*first)+1 <= length - 1) {
|
||||
int largest {(2*first)+1};
|
||||
if ((2*first)+2 <= length - 1 && storage[largest] < storage[(2*first)+2]) largest = (2*first)+2;
|
||||
if (storage[first] >= storage[largest]) break;
|
||||
int swap = storage[first];
|
||||
storage[first] = storage[largest];
|
||||
storage[largest] = swap;
|
||||
first = largest;
|
||||
}
|
||||
storage.pop_back();
|
||||
return max;
|
||||
}
|
||||
virtual bool contains(Point p) const = 0;
|
||||
virtual std::string description() const = 0;
|
||||
virtual void scale(double s) = 0;
|
||||
virtual ~Shape() {}
|
||||
};
|
||||
class Circle : public Shape {
|
||||
public:
|
||||
Point center;
|
||||
double radius;
|
||||
Circle(Point c, double r) : center(c), radius(r) {}
|
||||
virtual bool contains(Point p) const override {
|
||||
return dist(center, p) <= radius;
|
||||
}
|
||||
virtual std::string description() const override {
|
||||
return "Circle ; " + printp(center) + " ; " + std::to_string(radius);
|
||||
}
|
||||
virtual void scale(double s) override {
|
||||
radius *= s;
|
||||
}
|
||||
};
|
||||
class Rectangle : public Shape {
|
||||
public:
|
||||
Point topleft;
|
||||
double width, height;
|
||||
Rectangle(Point tl, double w, double h) : topleft(tl), width(w), height(h) {}
|
||||
virtual bool contains(Point p) const override {
|
||||
return topleft.x <= p.x && topleft.x + width >= p.x && topleft.y <= p.y && topleft.y + height >= p.y;
|
||||
}
|
||||
virtual std::string description() const override {
|
||||
return "Rectangle ; " + printp(topleft) + " ; [ " + std::to_string(width) + " , " + std::to_string(height) + " ]";
|
||||
}
|
||||
virtual void scale(double s) override {
|
||||
width *= s;
|
||||
height *= s;
|
||||
}
|
||||
};
|
||||
class CompositeShape : public Shape {
|
||||
public:
|
||||
Shape* shape1;
|
||||
Shape* shape2;
|
||||
CompositeShape(Shape* s1, Shape* s2) : shape1(s1), shape2(s2) {}
|
||||
virtual bool contains(Point p) const override {
|
||||
return shape1->contains(p) || shape2->contains(p);
|
||||
}
|
||||
virtual std::string description() const override {
|
||||
return "Composite ; " + shape1->description() + " ; " + shape2->description();
|
||||
}
|
||||
virtual void scale(double s) override {
|
||||
shape1->scale(s);
|
||||
shape2->scale(s);
|
||||
}
|
||||
};
|
||||
|
||||
int prompti(string,bool*);
|
||||
@ -104,15 +98,14 @@ double promptd(string,bool*);
|
||||
string prompts(string,bool*);
|
||||
|
||||
int main() {
|
||||
printlm("Create Heap:");
|
||||
HeapInf h{};
|
||||
bool s{true};
|
||||
while (s) {
|
||||
auto x {prompti("Enter integer (Finish by entering invalid): ",&s)};
|
||||
if (s) {h.add(x);}
|
||||
}
|
||||
printlm("Output Heap:");
|
||||
while (!h.isEmpty()) printlm(h.extractMax());
|
||||
std::vector<Shape*> s {};
|
||||
printlm("Creating Cirlces...");
|
||||
for (int i = 0; i <= 10; ++i) s.push_back(new Circle(Point(0, i), 10));
|
||||
printlm("Circles with the point (5, 12) within them:");
|
||||
for (int i = 0; i < s.size(); i++) if (s[i]->contains(Point(5, 12))) printlm(i);
|
||||
printlm("Destroying Circles...");
|
||||
while (s.size() > 0) {delete s.back(); s.pop_back();}
|
||||
Circle c {Poi}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user