dimanche 26 mai 2019

Numbers same but test failed(C++)

I am supposed to find the center (average) of all points with the center function in my class Polygon. However, I am facing this weird scenario where my output numbers matches exactly, but the test still fails? I do not understand this at all. Can someone help to clear out my doubts and help me to comprehend what is going on? Thanks!

Output:

Polygon::center test failed

7.01552 7.01552

4.18235 4.18235

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

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 IScalable
 {
public:
virtual void scale(float factor)=0;
 };

 class ITranslatable
{
public:
virtual void translate(const Object::VectorType& displacement)=0;
};

class Point :public Object, public ITranslatable
{

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;
}
void translate(const Object::VectorType& displacement) override
{
    this->mpoint.x2+=displacement.tx;
    this->mpoint.y2+=displacement.ty;
}

 };

  class Polygon :  public Point
  {
    private:
    Object::PointType *mpt;
    int msize;

    public:
    PointType& operator[](int index)  
   {
     return mpt[index];
   }
   Polygon(PointType* points, int npoints, float depth)
   : Point(*mpt,depth), mpt{new 
     Object::PointType[npoints]},msize(npoints){
    for(int i = 0; i < msize; ++i)
    {
     mpt[i]  = points[i];
    }
 }
Polygon center() //center function
{
     return *this;
}
float x() 
{   float avg;
    float sum;
    for(int i = 0; i < msize; i++)
    {
        sum += mpt[i].x2;
        avg = sum / msize;
    }
    return avg;
}
float y()
{
    float avg1;
    float sum1;
    for(int i = 0; i < msize; i++)
    {
        sum1 += mpt[i].y2;
        avg1 = sum1 / msize;
    }
    return avg1;
}
int size()
{
    return msize;
}
void scale(float factor)
{

    PointType mass_center = mpt->center();

    for(int i = 0; i < msize; i++)
    {   
        // this is a vector that leads from mass center to 
           current vertex
        PointType vec = PointType(mpt[i].x2 - mass_center.x2, 
                    mpt[i].y2 - mass_center.y2);
        mpt[i].x2 = mass_center.x2 + factor * vec.x2;
        mpt[i].y2 = mass_center.y2 + factor * vec.y2;
    }

}

};


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];
}
x /= static_cast<float>(max_index);
y /= static_cast<float>(max_index);
Polygon polygon(vertices, max_index, depth);
delete[] vertices;
if (polygon.size() == max_index)
{
    ++count;
}
else
{
    std::cout << "  - Polygon::size test failed" << std::endl;
}
++max_count;

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;

if (is_near(polygon.depth(), depth))
{
    ++count;
}
else
{
    std::cout << "  - Polygon::depth test failed" << std::endl;
}
++max_count;

if (is_near(polygon.center().x(), x) &&
    is_near(polygon.center().y(), y))
{
    ++count;
}
else
{
    std::cout << "  - Polygon::center test failed" << std::endl;
}
std::cout<<polygon.center().x()<< " "<<x<<std::endl;
    std::cout<<polygon.center().y()<< " "<<y<<std::endl;
++max_count;


int result = static_cast<int>(
    100.0f * static_cast<float>(count) / static_cast<float> 
    (max_count) + 0.5f
);
std::cout << "Tests passed: " << result << "%" << std::endl;
return result;
 }

Aucun commentaire:

Enregistrer un commentaire