-1
Visual Studio에서 C++을 사용하고 있으며, n-body 시뮬레이션 문제에 대한 이미지를 생성하는 알고리즘을 가지고 있습니다. 알고리즘 자체가 작동하지만 이미지가 작동하는지 증명할 수 있어야합니다. 나는 이것을 달성하기 위해 (코드에서 주석 처리 된) 많은 다른 방법을 시도했다. 나는 C++에 익숙하지 않고 코드가 나에게 주어 지므로 어떤 도움이나 조언도 크게 감사 할 것입니다.C++ : .jpg 파일로 생성 된 이미지를 저장하려고했지만 이미지가 열리지 않습니다.
#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;
}
이미지 파일에 무엇을 포함 하시겠습니까? 입자 위치의 플롯? 현재 이미지 파일에 텍스트를 쓰고 있기 때문에 작동하지 않습니다. – samgak
이산 코사인 변환 (DCT)에 대해 들어 보셨습니까? 아니? 그렇다면 직접 JPEG 이미지를 생성 할 수 없습니다. PPM 및 PBM 형식 (portable pixmap 및 bitmap)의 사양을 본 적이 있습니까? 여기 https://en.wikipedia.org/wiki/Netpbm_format에서 찾을 수 있습니다. 그것은 쉬운 형식입니다, 특히. ASCII 옵션이지만 약간의 노력이 필요합니다. – LutzL
몇 가지 점을 그려보기 만하면 SVG 벡터 이미지 형식을 사용하는 것이 좋습니다. 당신은 현재와 비슷한 텍스트 기반 형식으로 포인트를 출력 할 수 있지만 약간의 추가 형식이 있습니다. 이미지를 픽셀 배열로 래스터화할 필요가 없으므로 JPEG와 같은 비트 맵 기반 형식을 사용하는 것보다 훨씬 간단합니다. – samgak