F
franklini
hello people,
just wanted to say thanks again for the help in the past. i have a new
problem which am wondering if any body can help me with.
i have written this abtract class shape and its derived class circle,
rectangle and triangle. i have also written this method for each shape
object called 'draw_all_seq_inside2' which takes two values. i want to
call this method using for_each statement in another method but it
doesnt seem to work. this is a trim version of the code. i need to do
this using for_each statement as it is a coursework. please have a look
and get back to me (sorry for the lengthy code but i trimmed it as much
as possible).
#include <vector>
#include <list>
#include <deque>
#include <iostream>
#include <string>
#include <complex>
#include <functional>
#include <algorithm>
using namespace std;
class Shape {
protected:
string shapename;
public:
Shape(string name): shapename(name) {};
virtual void draw() const = 0;
virtual void inside_window2(complex<double> &bl, complex<double>
&tr) const = 0;
};
class Circle : public Shape {
public:
const double x1;
const double y1;
const double radius;
Circle(complex<double> first, double rad): Shape("Circle"),
x1(first.real()), y1(first.imag()), radius(rad) {}
void draw() const {
complex<double> first(x1, y1);
cout << "<Shape::" << Shape::shapename << " " << first << " "
<< radius
<< " >\n";
}
void inside_window2(complex<double> &bl, complex<double> &tr) const
{
if((x1 - radius) >= bl.real() && (y1 - radius) >= bl.imag() &&
(x1 + radius) <= tr.real() && (y1 + radius) <= tr.imag()){
draw();
}
}
};
class Rectangle : public Shape {
public:
const double x1;
const double y1;
const double x2;
const double y2;
Rectangle(complex<double> first, complex<double> second):
Shape("Rectangle"), x1(first.real()), y1(first.imag()),
x2(second.real()), y2(second.imag()) {}
void draw() const {
complex<double> first(x1, y1);
complex<double> second(x2, y2);
cout << "<Shape::" << Shape::shapename << " " << first << " "
<< second
<< " >\n";
}
void inside_window2(complex<double> &bl, complex<double> &tr) const
{
if(bl.real() <= x1 && bl.imag() <= y1 && tr.real() >= x2 &&
tr.imag() >= y2){
draw();
}
}
};
class Triangle : public Shape {
public:
const double x1;
const double y1;
const double x2;
const double y2;
const double x3;
const double y3;
Triangle(complex<double> first, complex<double> second,
complex<double> third):
Shape("Triangle"), x1(first.real()), y1(first.imag()),
x2(second.real()),
y2(second.imag()), x3(third.real()), y3(third.imag()) {}
void draw() const {
complex<double> first(x1, y1);
complex<double> second(x2, y2);
complex<double> third(x3, y3);
cout << "<Shape::" << Shape::shapename << " " << first << " "
<< second
<< " " << third << " >\n";
}
void inside_window2(complex<double> &bl, complex<double> &tr) const
{
double wx1 = bl.real();
double wy1 = bl.imag();
double wx2 = tr.real();
double wy2 = tr.imag();
if(wx1 <= x1 && wx1 <= x2 && wx1 <= x3 && wy1 <= y1 && wy1 <=
y2 && wy1 <= y3 &&
wx2 >= x1 && wx2 >= x2 && wx2 >= x3 && wy2 >= y1 && wy2 >=
y2 && wy2 >= y3){
draw();
}
}
};
template <typename Container>
void draw_all_seq_inside2(Container& c, Rectangle &w){
complex<double> first(w.x1, w.y1);
complex<double> second(w.x2, w.y2);
for_each(c.begin(), c.end(), mem_fun(&Shape::inside_window2(first,
second)));
}
int main() {
vector<Shape *> shapes;
complex<double> first(0, 0);
complex<double> secondr(1, 1);
complex<double> secondt(0, 1);
complex<double> third(1, 0);
Circle c = Circle(first, 1);
Rectangle r = Rectangle(first, secondr);
Triangle t = Triangle(first, secondt, third);
shapes.push_back(&c);
shapes.push_back(&r);
shapes.push_back(&t);
draw_all_seq_inside2(shapes, r);
return 0;
}
just wanted to say thanks again for the help in the past. i have a new
problem which am wondering if any body can help me with.
i have written this abtract class shape and its derived class circle,
rectangle and triangle. i have also written this method for each shape
object called 'draw_all_seq_inside2' which takes two values. i want to
call this method using for_each statement in another method but it
doesnt seem to work. this is a trim version of the code. i need to do
this using for_each statement as it is a coursework. please have a look
and get back to me (sorry for the lengthy code but i trimmed it as much
as possible).
#include <vector>
#include <list>
#include <deque>
#include <iostream>
#include <string>
#include <complex>
#include <functional>
#include <algorithm>
using namespace std;
class Shape {
protected:
string shapename;
public:
Shape(string name): shapename(name) {};
virtual void draw() const = 0;
virtual void inside_window2(complex<double> &bl, complex<double>
&tr) const = 0;
};
class Circle : public Shape {
public:
const double x1;
const double y1;
const double radius;
Circle(complex<double> first, double rad): Shape("Circle"),
x1(first.real()), y1(first.imag()), radius(rad) {}
void draw() const {
complex<double> first(x1, y1);
cout << "<Shape::" << Shape::shapename << " " << first << " "
<< radius
<< " >\n";
}
void inside_window2(complex<double> &bl, complex<double> &tr) const
{
if((x1 - radius) >= bl.real() && (y1 - radius) >= bl.imag() &&
(x1 + radius) <= tr.real() && (y1 + radius) <= tr.imag()){
draw();
}
}
};
class Rectangle : public Shape {
public:
const double x1;
const double y1;
const double x2;
const double y2;
Rectangle(complex<double> first, complex<double> second):
Shape("Rectangle"), x1(first.real()), y1(first.imag()),
x2(second.real()), y2(second.imag()) {}
void draw() const {
complex<double> first(x1, y1);
complex<double> second(x2, y2);
cout << "<Shape::" << Shape::shapename << " " << first << " "
<< second
<< " >\n";
}
void inside_window2(complex<double> &bl, complex<double> &tr) const
{
if(bl.real() <= x1 && bl.imag() <= y1 && tr.real() >= x2 &&
tr.imag() >= y2){
draw();
}
}
};
class Triangle : public Shape {
public:
const double x1;
const double y1;
const double x2;
const double y2;
const double x3;
const double y3;
Triangle(complex<double> first, complex<double> second,
complex<double> third):
Shape("Triangle"), x1(first.real()), y1(first.imag()),
x2(second.real()),
y2(second.imag()), x3(third.real()), y3(third.imag()) {}
void draw() const {
complex<double> first(x1, y1);
complex<double> second(x2, y2);
complex<double> third(x3, y3);
cout << "<Shape::" << Shape::shapename << " " << first << " "
<< second
<< " " << third << " >\n";
}
void inside_window2(complex<double> &bl, complex<double> &tr) const
{
double wx1 = bl.real();
double wy1 = bl.imag();
double wx2 = tr.real();
double wy2 = tr.imag();
if(wx1 <= x1 && wx1 <= x2 && wx1 <= x3 && wy1 <= y1 && wy1 <=
y2 && wy1 <= y3 &&
wx2 >= x1 && wx2 >= x2 && wx2 >= x3 && wy2 >= y1 && wy2 >=
y2 && wy2 >= y3){
draw();
}
}
};
template <typename Container>
void draw_all_seq_inside2(Container& c, Rectangle &w){
complex<double> first(w.x1, w.y1);
complex<double> second(w.x2, w.y2);
for_each(c.begin(), c.end(), mem_fun(&Shape::inside_window2(first,
second)));
}
int main() {
vector<Shape *> shapes;
complex<double> first(0, 0);
complex<double> secondr(1, 1);
complex<double> secondt(0, 1);
complex<double> third(1, 0);
Circle c = Circle(first, 1);
Rectangle r = Rectangle(first, secondr);
Triangle t = Triangle(first, secondt, third);
shapes.push_back(&c);
shapes.push_back(&r);
shapes.push_back(&t);
draw_all_seq_inside2(shapes, r);
return 0;
}