-1

I'm using c++ in visual studio and i have an algorithm that creates an image for the n-body simulation problem. the algorithm itself works however i need to store the image to be able to prove that it works. i have tried a lot of different ways (which have been commented out in the code), to try to achieve this. i am not very familiar with c++ and the code was given to me so any help or advice at all would be greatly appreciated.

the function where i try to store the image is the PrintParticle() function:

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <windows.h>

using namespace std;
#define N 100
#define G 6.673e-11
#define TIMESTAMP 1e11
struct Particle {
    double rx, ry;//position components
    double vx, vy;//velocity components
    double fx, fy;//force components
    double mass;//mass of the particle

};
Particle Update(Particle p, double timestamp)
{
    p.vx += timestamp*p.fx / p.mass;
    p.vy += timestamp*p.fy / p.mass;
    p.rx += timestamp*p.vx;
    p.ry += timestamp*p.vy;
    return p;
}
void PrintParticle(Particle p)
{
    ofstream fout("particle.jpg");
    fout << ("rx == %f ry == %f vx == %f vy == %f mass == %f\n", p.rx, p.ry, p.vx, p.vy, p.mass)<< endl;
    fout.close();

    //FILE *f = fopen("particle.ppm", "w");         // Write image to PPM file. 
    //fprintf(f, "rx == %f ry == %f vx == %f vy == %f mass == %f\n", p.rx, p.ry, p.vx, p.vy, p.mass);
    //string filename = "/Users/Tasha/Documents/Visual Studio 2015/Projects/n-bodySim/n-bodySim";
    //("rx == %f ry == %f vx == %f vy == %f mass == %f\n", p.rx, p.ry, p.vx, p.vy, p.mass)->WriteImage(filename);
}
//Reset the forces on particle
Particle ResetForce(Particle p)
{
    p.fx = 0.0;
    p.fy = 0.0;
    return p;
}
//Add force to particle a by particle b
Particle AddForce(Particle a, Particle b)
{
    double EPS = 3E4;      // softening parameter (just to avoid infinities)
    double dx = b.rx - a.rx;
    double dy = b.ry - a.ry;
    double dist = sqrt(dx*dx + dy*dy);
    double F = (G * a.mass * b.mass) / (dist*dist + EPS*EPS);
    a.fx += F * dx / dist;
    a.fy += F * dy / dist;
    return a;

}

int main()
{
    Particle particles[N];
    srand(time(NULL));
    //randomly generating N Particles
    for (int i = 0; i < N; i++) {
        double rx = 1e18*exp(-1.8)*(.5 - rand());
        particles[i].rx = rx;
        double ry = 1e18*exp(-1.8)*(.5 - rand());
        particles[i].ry = ry;
        double vx = 1e18*exp(-1.8)*(.5 - rand());
        particles[i].vx = vx;
        double vy = 1e18*exp(-1.8)*(.5 - rand());
        particles[i].vy = vy;
        double mass = 1.98892e30*rand() * 10 + 1e20;
        particles[i].mass = mass;

    }

    int numberofiterations = 10;
    int count = 0;
    while (count < numberofiterations) {
        for (int i = 0; i < N; i++)
        {
            particles[i] = ResetForce(particles[i]);
            for (int j = 0; j < N; j++)
            {
                if (i != j)
                {
                    particles[i] = AddForce(particles[i], particles[j]);
                }

            }
        }
        //loop again to update the time stamp here
        for (int i = 0; i < N; i++)
        {
            particles[i] = Update(particles[i], TIMESTAMP);
        }
        for (int i = 0; i < N; i++)
        {
            PrintParticle(particles[i]);
        }
        count++;
    }
    return 0;
}
TMC
  • 1
  • 5
  • What do you want to be in the image file? A plot of the particle positions? At the moment you are writing text to an image file, which is why it's not working. – samgak Nov 26 '16 at 20:00
  • Did you ever hear of the discrete cosine transform (DCT)? No? Then you can not directly generate JPEG images. Did you ever look at the specification of the PPM and PBM formats (portable pixmap and bitmap)? Here https://en.wikipedia.org/wiki/Netpbm_format you can find it. It is an easy format, esp. the ASCII option, but requires some effort. – Lutz Lehmann Nov 26 '16 at 20:04
  • 2
    If you are just going to plot a few points, I recommend using the SVG vector image format. You can just output the points in a text-based format similar to how you are now, but with a bit of extra formatting. It's much simpler than using a bitmap-based format like JPEG because you don't have to rasterize your image to an array of pixels. – samgak Nov 26 '16 at 20:12
  • To conveniently produce graphic files one can use the Cairo library, step-by-step explained in [this tutorial](http://zetcode.com/gfx/cairo/). – Lutz Lehmann Nov 26 '16 at 20:42
  • You should probably start [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Nov 26 '16 at 20:56

1 Answers1

3

JPEG requires a special file-format, which your code doesn't implement. I'd suggest you start off by searching for a library for working on this image-format, or implementing your own codec.

Simply writing text to a file and calling it ".jpg" won't work.