As I'm new to C++, I am stuck on a problem whereby I am trying to use polygon.center() to return the first point in the list of points. But unfortunately I tried many ways and still failed. Does Anyone know how I can go about achieving that? Center function is in class polygon. NOTE: Do NOT modify the main() Thanks!
Output:
Polygon::center test failed
5.5195 4.90362
8.57219 10.1605
class Object:
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:
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.x2;
}
float y()
{
return mpoint.y2;
}
};
class polygon:
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)
: msize(npoints), mpt{new Object::PointType[npoints]},
Point(*mpt,depth){
for(int i = 0; i < msize; ++i)
{
mpt[i] = points[i];
}
}
Polygon center() //center function
{
return *this;
}
float x()
{
return mpt-> x2;
}
float y()
{
return mpt-> y2;
}
};
Main:
#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;
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;
}
Aucun commentaire:
Enregistrer un commentaire