Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am coding a program in C++ that generates a fleet of ships and then produces a

ID: 3797981 • Letter: I

Question

I am coding a program in C++ that generates a fleet of ships and then produces a .png image using the cordinates provided in a data file. There are 7 ships, 6 of the ships have a Z:axis value of 0 since they don't go underwater and 1 ship is a submarine so it can have a non-zero Z value (either -1,-2,-3,-4).  The part of my program (not included) to generate the data points and put them into a data file is working fine but I can't seem to produce the resulting .png using gnuplot. The .dat file looks like this:

20 0 0

20 10 0

20 -10 0

10 0 0

12.9289   7.07107   0

12.9289   -7.07107 0

10 20 -3

where each line represents a different ship and the first data value is the X coordinate, the second value is the Y coordinate and the third value is the Z coordinate...essentially I want to have a 3d plot with all the ships and have the plotted points either a different color or shape (I don't really care which) so I am able to distinguish them apart...I have looked online and can't find much help with plotting a simple 3d graph...below is how far I have gotten

#include <iostream>
#include <random>
#include <ctime>
#include <list>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;
#include "ships.h"

string to_string(int i)
{
    stringstream ss;
    ss<<i;
    return ss.str();
}

int main()
{
    string fname, outfile;
    fstream fout, fout2;
    fout2.open("command.txt",ios::out);
    fout2<<"set yrange [-25:25]"<<endl;
    fout2<<"set xrange [-25:25]"<<endl;
    fout2<<"set zrange [-4:0]"<<endl;
    fout2<<"set xlabel "X Axis""<<endl;
    fout2<<"set ylabel "Y Axis""<<endl;
    fout2<<"set zlabel "Z Axis""<<endl;
    fout2<<"set terminal png"<<endl;

   for(int i = 0; i < 10; i++)

{

    fname="position_"+ to_string(t)+".dat";
    outfile="position_"+ to_string(t)+".png";
    fout2<<"set output '"<<outfile<<"'"<<endl;
    fout2<<"plot ""<<fname<<"" with circles linecolor rgb "#9ACD32" fill solid noborder;"<<endl;

}


    fout2.close();
    system("gnuplot command.txt");

    return 0;

}

Any help would be greatly appreciated

Explanation / Answer

Rather than using

system("gnuplot command.txt");

use

system("gnuplot -e "plot 'command.txt'"");

Explanation:

There is slight syntax change, as you are calling gnuplot inside a c++ program hence system call is used which is correct. When using system call we need to pass subcommands in a special manner, plot in this case. gnuplot only calls the interactive session but does not tells what to do i.e. subcommand, so –e (execute) options is called which tells gnuplot to plot the graph using instructions in command.txt