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
|
class Object
{
public:
private:
float d;
public:
Object(float n) : d(n){}
struct PointType
{
float x;
float y;
PointType( float x1, float y1) :x(x1),y(y1){}
PointType(){}
};
};
class Point :public Object
{
private:
PointType mpoint;
public:
Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}
Point center()
{
return *this;
}
float x()
{
return mpoint.x;
}
float y()
{
return mpoint.y;
}
};
class Polygon : public Point
{
private:
Object::PointType *mpt;
int msize;
public:
const PointType& operator[](int index)
{
return mpt[index];
}
Polygon(PointType* points, int npoints, float depth)
: msize(npoints), mpt{new Object::PointType[npoints]}, Point(*mpt,depth){
for(int i = 0; i < msize; ++i)
{
mpt[i] = points[i];
}
}
float x()
{
return mpt-> x;
}
float y()
{
return mpt-> y;
}
};
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
const float EPSILON = 1e-5f;
bool is_near(float x, float y)
{
return std::abs(x - y) < EPSILON;
}
float frand()
{
return 10.0f * float(rand()) / float(RAND_MAX);
}
int main()
{
srand(unsigned(time(0)));
int count = 0;
int max_count = 0;
int max_index = 3 + rand() % 8;
float* xs = new float[max_index];
float* ys = new float[max_index];
float depth = frand();
float x = 0;
float y = 0;
Polygon::PointType* vertices = new Polygon::PointType[max_index];
for (int index = 0; index < max_index; ++index)
{
xs[index] = frand();
ys[index] = frand();
vertices[index] = Polygon::PointType(xs[index], ys[index]);
x += xs[index];
y += ys[index];
y += ys[index];
}
x /= static_cast<float>(max_index);
y /= static_cast<float>(max_index);
Polygon polygon(vertices, max_index, depth);
delete[] vertices;
int index = 0;
while ((index < max_index) &&
is_near(polygon[index].x(), xs[index]) &&
is_near(polygon[index].y(), ys[index]))
{
++index;
}
if (index == max_index)
{
++count;
}
else
{
std::cout << " - Polygon::operator[] test failed" << std::endl;
}
++max_count;
|