【发布时间】:2016-11-26 19:56:01
【问题描述】:
我在 Visual Studio 中使用 c++,并且我有一个算法可以为 n 体模拟问题创建图像。该算法本身有效,但是我需要存储图像才能证明它有效。我尝试了很多不同的方法(已在代码中注释掉),试图实现这一点。我对 c++ 不是很熟悉,代码是给我的,所以任何帮助或建议都将不胜感激。
我尝试存储图像的函数是 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;
}
【问题讨论】:
-
你想在图像文件中是什么?粒子位置图?目前您正在将文本写入图像文件,这就是它不起作用的原因。
-
您听说过离散余弦变换 (DCT) 吗?不?那么就不能直接生成JPEG图像了。您是否看过 PPM 和 PBM 格式(便携式像素图和位图)的规范?在这里en.wikipedia.org/wiki/Netpbm_format 你可以找到它。这是一种简单的格式,尤其是。 ASCII 选项,但需要一些努力。
-
如果您只是要绘制几个点,我建议使用 SVG 矢量图像格式。您可以只以类似于您现在的方式的基于文本的格式输出点,但需要一些额外的格式。它比使用基于位图的格式(如 JPEG)要简单得多,因为您不必将图像光栅化为像素数组。
-
为了方便地生成图形文件,可以使用 Cairo 库,在this tutorial 中逐步解释。
-
你应该开始here。
标签: c++ algorithm visual-studio physics particles