I have been trying to get my Class Square functions: float radius() and void scale() to work. However, it just does not, and I can't figure out why. The scale function and radius function is required to get the output for the test. Can someone show me the way? Note: main() cannot be modified in any way. Thanks!
Output:
Square::scale test failed
6.27346 60.4114
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
class IScalable
{
public:
virtual void scale(float factor)=0;
};
class Object
{
private:
float d;
public:
Object(float n) : d(n){}
float depth() const
{
return d;
}
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;
}
};
struct VectorType
{
float tx;
float ty;
VectorType( float tx1, float ty1) :tx(tx1),ty(ty1){}
VectorType( ){}
};
};
class Point :public Object
{
private:
PointType mpoint;
public:
Point(const PointType& pt, float& y1) : Object(y1), mpoint(pt){}
Point center()
{
return *this;
}
float x()
{
return mpoint.x2;
}
float y()
{
return mpoint.y2;
}
};
class Circle : public Point, public IScalable
{
private:
Object::PointType m_pt;
float r;
public:
Circle(const PointType pts, float rad, float dep)
: Point(m_pt,dep), m_pt(pts),r(rad){}
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) :
Circle(spts,a,depth),s_pt(spts),v_pt(vpts){}
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;
}
};
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;
}
Aucun commentaire:
Enregistrer un commentaire