Create a class called XYData that maintains a list of 2D points stored as vector
ID: 3687281 • Letter: C
Question
Create a class called XYData that maintains a list of 2D points stored as vector of Vec objects that we have seen in previous labs. Your class should also implement a method add() that will add points to its list of points, and a method max() that will return a Vec object with the maximum x and y coordinates. Add a virtual method generate(float xini, float xend, float inc) to your XYData class. This method should generate data with x coordinates from xini to xend by inc increments. The y coordinate will be computed according to each derived class. Now create two derived classes as follows: class QuadraticCurve will have a constructor receiving 3 floats a, b and c and will override method generate() in order to evaluate points on the quadratic curve a x^2 + b x + c; where ^ denotes exponentiation. Similarly, class CubicCurve will have a constructor receiving 4 floats a, b, c and d and will override method generate() in order to evaluate points on the cubic curve a x^3 + b x^2 + c x + d.
Explanation / Answer
main.cpp
#include <iostream>
#include "GenerateCurves.h"
using namespace std;
int main(int argc, const char * argv[])
{
XYData* d2 = new QuadraticCurve ( 1, 0, 0 );
XYData* d3 = new CubicCurve ( 1, 0, 0, 0 );
d2->generate ( 0, 2, 1 );
d3->generate ( 0, 2, 1 );
cout<<"Q Max: "<<d2->max().y<<endl;
cout<<"C Max: "<<d3->max().y<<endl;
return 0;
}
GenerateCurves.h
#ifndef __CURVES_H__
#define __CURVES_H__
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
class Vec
{
public:
float x;
float y;
static Vec null;
Vec() : x(0.f), y(0.f)
{
}
Vec(float x, float y) : x(x), y(y)
{
}
Vec& add(const Vec& other)
{
x += other.x;
y += other.y;
return *this;
}
void print()
{
std::cout << "(" << x << ", " << y << ")" << std::endl;
}
};
Vec Vec::null;
class XYData
{
static bool xcheck(Vec a, Vec b) {return a.x < b.x;}
static bool ycheck(Vec a, Vec b) {return a.y < b.y;}
protected:
std::vector<Vec> points;
public:
void add(Vec p)
{
points.push_back(p);
}
Vec max()
{
Vec max;
max.x = std::max_element(points.begin(), points.end(), xcheck)->x;
max.y = std::max_element(points.begin(), points.end(), ycheck)->y;
return max;
}
virtual void generate(float xini, float xend, float inc)
{
points.resize(0);
for(float i = xini; i <= xend; i += inc)
add(Vec(i,0));
}
};
class QuadraticCurve : public XYData
{
float a;
float b;
float c;
public:
QuadraticCurve(float a, float b, float c)
: a(a), b(b), c(c)
{
}
void generate(float xini, float xend, float inc)
{
XYData::generate(xini, xend, inc);
for(std::vector<Vec>::iterator i = points.begin(); i != points.end(); i++)
i->y = a*std::pow(i->x, 2) + b*i->x + c;
}
};
class CubicCurve : public XYData
{
float a, b, c, d;
public:
CubicCurve(float a, float b, float c, float d)
: a(a), b(b), c(c), d(d)
{
}
void generate(float xini, float xend, float inc)
{
XYData::generate(xini, xend, inc);
for(std::vector<Vec>::iterator i = points.begin(); i != points.end(); i++)
i->y = a*std::pow(i->x, 3) + b*std::pow(i->x, 2) + c*i->x + d;
}
};
#endif
sample output
Q Max: 4
C Max: 8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.