Skip to content
Snippets Groups Projects
Commit c39da79a authored by Stephen D's avatar Stephen D
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
a.out 0 → 100755
File added
main.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "qdbmp.h"
typedef struct RigidMass
{
long mass;
double vx, vy;
double x, y;
double dvx, dvy;
double tick_time;
} RigidMass_t;
double G = 6.67408E-11;
//Calculates delta V on body, due to other.
void calcGravity(RigidMass_t *body, RigidMass_t other)
{
double dx = body->x - other.x;
double dy = body->y - other.y;
double dist_squared = dx * dx + dy * dy;
double v = -G * other.mass / dist_squared * body->tick_time;
double hyp = sqrt(dist_squared);
body->dvx += v * (dx) / hyp;
body->dvy += v * (dy) / hyp;
}
void processTick(RigidMass_t *body)
{
//technically, we could just remove dvx/dvy
//and work with vx/vy directly, since they don't change anything other than when
//processing the tick here
body->vx += body->dvx;
body->vy += body->dvy;
body->dvx = 0;
body->dvy = 0;
body->x += body->vx * body->tick_time;
body->y += body->vy * body->tick_time;
}
//END PHYSICS ENGINE
//thank you, stackoverflow
/* reverse: reverse string s in place */
void reverse(char s[])
{
int i, j;
char c;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* itoa: convert n to characters in s */
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
//END SO
#define numStars 10000
int w = 1000;
int h = 1000;
double max_component_vel = 3;
RigidMass_t stars[numStars];
void generateStars()
{
RigidMass_t new;
new.mass = 500000000000;
new.dvx = 0;
new.dvy = 0;
new.tick_time = 0.05;
for(int i = 0; i < numStars; i++)
{
new.x = rand() % 250 + 375; //375 to 625
new.y = rand() % 250 + 375;
double dx = new.x - w / 2;
double dy = new.y - h / 2;
if(abs(dx) <= abs(dy))
{
new.vx = (double)rand()/(double)(RAND_MAX/max_component_vel);
new.vy = -new.vx * dx / dy;
}
else
{
new.vy = (double)rand()/(double)(RAND_MAX/max_component_vel);
new.vx = -new.vy * dy / dx;
}
if(new.y < new.x)
{
new.vy = -new.vy;
new.vx = -new.vx;
}
stars[i] = new;
}
}
void iterate()
{
#pragma omp parallel for
for(int a = 0; a < numStars; a++)
{
for(int b = 0; b < numStars; b++)
{
if(abs(stars[a].x - stars[b].x) > 0.1 && abs(stars[a].y - stars[b].y) > 0.1)
{
//printf("%d, %d \r\n", stars[a].dvx, stars[a].dvy);
calcGravity(stars + a, stars[b]);
}
}
}
//'commit' the deltaV
for(int a = 0; a < numStars; a++)
{
processTick(stars + a);
//printf("%f, %f \r\n", stars[a].x, stars[a].y);
}
}
BMP *out;
void addToImage()
{
for(int a = 0; a < numStars; a++)
{
BMP_SetPixelRGB(out, stars[a].x, stars[a].y, 0, 150, 0);
}
}
void saveImage(double frame)
{
char filename[50];
char buffer[46];
strcpy(filename, "img/");
itoa(frame, buffer);
strcat(filename, buffer);
strcat(filename, ".bmp");
BMP_WriteFile(out, filename);
BMP_Free(out);
out = BMP_Create(w, h, 24);
}
int main()
{
srand(time(NULL));
out = BMP_Create(w, h, 24);
generateStars();
for(int i = 0; i < 1000; i++)
{
iterate();
addToImage();
if (i % 10 == 0)
{
saveImage(i / 10);
}
}
return 0;
}
This diff is collapsed.
qdbmp.h 0 → 100755
#ifndef _BMP_H_
#define _BMP_H_
/**************************************************************
QDBMP - Quick n' Dirty BMP
v1.0.0 - 2007-04-07
http://qdbmp.sourceforge.net
The library supports the following BMP variants:
1. Uncompressed 32 BPP (alpha values are ignored)
2. Uncompressed 24 BPP
3. Uncompressed 8 BPP (indexed color)
QDBMP is free and open source software, distributed
under the MIT licence.
Copyright (c) 2007 Chai Braudo (braudo@users.sourceforge.net)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**************************************************************/
#include <stdio.h>
/* Type definitions */
#ifndef UINT
#define UINT unsigned long int
#endif
#ifndef USHORT
#define USHORT unsigned short
#endif
#ifndef UCHAR
#define UCHAR unsigned char
#endif
/* Version */
#define QDBMP_VERSION_MAJOR 1
#define QDBMP_VERSION_MINOR 0
#define QDBMP_VERSION_PATCH 1
/* Error codes */
typedef enum
{
BMP_OK = 0, /* No error */
BMP_ERROR, /* General error */
BMP_OUT_OF_MEMORY, /* Could not allocate enough memory to complete the operation */
BMP_IO_ERROR, /* General input/output error */
BMP_FILE_NOT_FOUND, /* File not found */
BMP_FILE_NOT_SUPPORTED, /* File is not a supported BMP variant */
BMP_FILE_INVALID, /* File is not a BMP image or is an invalid BMP */
BMP_INVALID_ARGUMENT, /* An argument is invalid or out of range */
BMP_TYPE_MISMATCH, /* The requested action is not compatible with the BMP's type */
BMP_ERROR_NUM
} BMP_STATUS;
/* Bitmap image */
typedef struct _BMP BMP;
/*********************************** Public methods **********************************/
/* Construction/destruction */
BMP* BMP_Create ( UINT width, UINT height, USHORT depth );
void BMP_Free ( BMP* bmp );
/* I/O */
BMP* BMP_ReadFile ( const char* filename );
void BMP_WriteFile ( BMP* bmp, const char* filename );
/* Meta info */
UINT BMP_GetWidth ( BMP* bmp );
UINT BMP_GetHeight ( BMP* bmp );
USHORT BMP_GetDepth ( BMP* bmp );
/* Pixel access */
void BMP_GetPixelRGB ( BMP* bmp, UINT x, UINT y, UCHAR* r, UCHAR* g, UCHAR* b );
void BMP_SetPixelRGB ( BMP* bmp, UINT x, UINT y, UCHAR r, UCHAR g, UCHAR b );
void BMP_GetPixelIndex ( BMP* bmp, UINT x, UINT y, UCHAR* val );
void BMP_SetPixelIndex ( BMP* bmp, UINT x, UINT y, UCHAR val );
/* Palette handling */
void BMP_GetPaletteColor ( BMP* bmp, UCHAR index, UCHAR* r, UCHAR* g, UCHAR* b );
void BMP_SetPaletteColor ( BMP* bmp, UCHAR index, UCHAR r, UCHAR g, UCHAR b );
/* Error handling */
BMP_STATUS BMP_GetError ();
const char* BMP_GetErrorDescription ();
/* Useful macro that may be used after each BMP operation to check for an error */
#define BMP_CHECK_ERROR( output_file, return_value ) \
if ( BMP_GetError() != BMP_OK ) \
{ \
fprintf( ( output_file ), "BMP error: %s\n", BMP_GetErrorDescription() ); \
return( return_value ); \
} \
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment