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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
class IScalable
{
public:
virtual void scale(float factor)=0;
};
class Object
{
public:
private:
float d;
public:
Object(float n) : d(n){}
struct PointType
{
float x2;
float y2;
PointType( float x1, float y1) :x2(x1),y2(y1){}
PointType(){}
float x()
{
return x2;
}
float y()
{
return y2;
}
PointType center()
{
return *this;
}
};
class Point :public Object
{
private:
PointType mpoint;
public:
Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}
};
class Circle : public Point, public IScalable
{
private:
Object::PointType m_pt;
float r;
public:
Circle(const PointType pts, float rad, float dep)
: m_pt(pts),r(rad),Point(m_pt,dep) {}
float radius()
{
return r;
}
Circle center()
{
return *this;
};
float x()
{
return m_pt.x2;
}
float y()
{
return m_pt.y2;
}
void scale(float scale) override
{
r*=scale;
}
};
class Square: public Circle
private:
Object::PointType s_pt;
Object::VectorType v_pt;
float a;
public:
Square(const PointType spts,const VectorType vpts,float depth) :
s_pt(spts),v_pt(vpts),Circle(spts,a,depth){}
float radius()
{
float rad= sqrt(v_pt.tx * v_pt.tx + v_pt.ty * v_pt.ty);
a=rad;
return a;
}
float x()
{
return v_pt.tx/radius();
}
float y()
{
return v_pt.ty/radius();
}
void scale(float factor) override
{
a*=factor;
}
};
#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;
float x = frand();
float y = frand();
float sx = frand();
float sy = frand();
float depth = frand();
Square square(Square::PointType(x, y), Square::VectorType(sx, sy),
depth);
float scale = frand();
float radius = std::sqrt(sx * sx + sy * sy);
Square square3(square);
square3.scale(scale);
if (is_near(square3.radius(), scale * radius))
{
++count;
}
else
{
std::cout << " - Square::scale test failed" << std::endl;
}
std::cout << square3.radius()<< " " << scale* radius<<std::endl;
++max_count;
}
|