2016-11-26 46 views
-1

我在Visual Studio中使用c + +,我有一個算法,爲n體仿真問題創建一個圖像。該算法本身的作品,但我需要存儲的圖像能夠證明它的作品。我嘗試了很多不同的方式(已經在代碼中註釋掉了),試圖實現這一點。我不是很熟悉C++,代碼給了我,所以任何幫助或建議都將不勝感激。C++:試圖保存一個圖像創建到一個.jpg文件,但圖像不會打開

在那裏我嘗試存儲圖像的功能是PrintParticle()函數:

#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; 
} 
+0

你想在圖像文件中做什麼?粒子位置圖?目前您正在將文本寫入圖像文件,這就是爲什麼它不起作用。 – samgak

+0

你有沒有聽說過離散餘弦變換(DCT)?沒有?那麼你不能直接生成JPEG圖像。你有沒有看過PPM和PBM格式(便攜式像素圖和位圖)的規格?這裏https://en.wikipedia.org/wiki/Netpbm_format你可以找到它。這是一個簡單的格式,尤其是。 ASCII選項,但需要一些努力。 – LutzL

+2

如果您只是要繪製幾點,我推薦使用SVG矢量圖像格式。您可以使用與您現在的方式相似的基於文本的格式輸出點,但只需要一些額外的格式。這比使用JPEG等基於位圖的格式簡單得多,因爲您不必將圖像光柵化爲像素數組。 – samgak

回答

3

JPEG需要一種特殊的文件格式,你的代碼沒有實現。我建議你首先尋找一個庫來處理這個圖像格式,或者實現你自己的編解碼器。

只需將文本寫入文件並將其命名爲「.jpg」將不起作用。