Pregunta

(Full question is listed at the bottom)

I have an assignment that requires me to write text to a postscript file that allows me to draw "Gosper" curves using recursion. However, the test driver (GosperDriver.cpp) my professor has given us resembles the following:

#include "Gosper.h"
#include <iostream>
using namespace std;

int main( )
{   
// test right hexagonal Gosper curve at level 4
Gosper gosper1( 100, 100, 0 );
gosper1.rightCurve( 4, 4 ); 

// test left hexagonal Gosper curver at level 4
Gosper gosper2( 500, 100, 0 );
gosper2.leftCurve( 4, 4 );

// test right hexagonal Gosper curve at level 3
Gosper gosper3( 100, 400, 0 );
gosper3.rightCurve( 3, 6 );

// test left hexagonal Gosper curver at level 3
Gosper gosper4( 500, 400, 0 );
gosper4.leftCurve( 3, 6 );

// test right hexagonal Gosper curve at level 2
Gosper gosper5( 100, 600, 0 );
gosper5.rightCurve( 2, 8 );

// test left hexagonal Gosper curver at level 2
Gosper gosper6( 500, 600, 0 );
gosper6.leftCurve( 2, 8 );

// test right hexagonal Gosper curve at level 1
Gosper gosper7( 100, 700, 0 );
gosper7.rightCurve( 1, 10 );

// test left hexagonal Gosper curver at level 1
Gosper gosper8( 500, 700, 0 );
gosper8.leftCurve( 1, 10 );
}

Gosper.h includes Turtle.h, which contains the "draw" functions which are vital to the project.

Here are my Gosper.h, Gosper.cpp, Turtle.h, and Turtle.cpp files, in that order (I'll cut out the unnecessary code, which controls drawing):

Gosper.h:

// Sierpinski Class 
#ifndef GOSPER_H
#define GOSPER_H

#include "Turtle.h"
#include <iostream>
#include <fstream>


using namespace std;

class Gosper : Turtle 
{

public:

    Gosper(float initX=0.0, float initY=0.0, float initA=0.0);

    void leftCurve( int l, float d );  // Draw level l left curve with dist d
    void rightCurve( int l, float d ); // Draw level l right curve with dist d

};

#endif

Gosper.cpp:

#include <iostream>
#include <string>
#include "Gosper.h"

// Initialization and such.
Gosper::Gosper(float initX, float initY, float initA)
{

}


void Gosper::leftCurve(int level, float d)
{
    // Code that uses draw() function of Turtle.h and Turtle.cpp
}


void Gosper::rightCurve(int level, float d)
{
    // Same as above
}

Turtle.h:

#ifndef TURTLE_H
#define TURTLE_H

#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

const float PI = 3.1459265;

class Turtle {

public:

    Turtle(float initX = 0.0, float initY = 0.0, float initA = 0.0);
    ~Turtle();

    void draw( float d );   // draw line by distance d
    void move( float d );   // simply move by distance d
    void turn( float a );   // turn by angle a

private:

    ofstream out;
    float angle;    // current angle

};

Turtle.cpp:

#include "Turtle.h"
#include <iostream>
#include <fstream>

Turtle::Turtle(float initX, float initY, float initA)
{

out.open("output.ps");

out << "%!PS-Adobe-2.0" << endl;
out << initX << "\t" << initY << "\tmoveto" << endl;

angle = initA;

}

Turtle::~Turtle() 
{
out << "stroke" << endl;
out << "showpage" << endl;
}

void Turtle::turn(float a)
{
angle += a;
}

void Turtle::draw(float d)
{
float dX, dY;

dX = d * cos(PI * angle / 180);
dY = d * sin(PI * angle / 180);
out << dX << "\t" << dY << "\trlineto" << endl;
}

void Turtle::move(float d)
{
float dX, dY;

dX = d * cos(PI * angle / 180);
dY = d * sin(PI * angle / 180);
out << dX << "\t" << dY << "\trmoveto" << endl;
}
#endif

Okay, so now that you've seen my code, here's my problem:

I want to write the text for every Gosper class object in GosperDriver.cpp into one postscript file. As it is right now, any attempt to do that will result in the previous block of text in the designated output.ps to be overwritten. At the moment, I can only write the text necessary for ONE Gosper class object. I have had to comment out every Gosper object declaration in Gosperdriver.cpp but one, in order to test if my program is working correctly.

In short, I need to write the text necessary to output.ps for every Gosper object in GosperDriver.cpp, but it isn't working because it will only let me write for one at a time. What do I do?


Bonus question about inheritance: right now, my "starting point" for each Gosper drawing keeps being set at x = 0 and y = 0. As seen by the Gosper object declarations, none of the parameters contain 0 for x or y. Something's gone wonky. What's happening?

Thanks in advance to anyone who can answer one or both of these questions! :)

¿Fue útil?

Solución

You can use

out.open("output.ps", std::fstream::in | std::fstream::out | std::fstream::app);

to open the file in append mode. Meaning old content will not be overwritten. You will however need to add something to detect if the header out << "%!PS-Adobe-2.0" << endl; has already been written. (I assume you need that exactly once per file.)

To avoid opening and closing the file all the time you could also create a separate class that will open the file, initialize it (write the header) and then use this class to write all your contents and close the file afterwards.

For bonus points use RAII to make the class automatically take care of the file.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top