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