Status report
Some more code for you to look at:
/* Gravity simulator version 3.0 by Alex Heywood*/
/* Actual units km, kg, N, days, */
#include
#include
#include
#include
#define GRAVITY 6.67e-11
#define PMASS 5.97e22
#define SUNMASS 2e22
main()
{
int days, secs;
double pX, pY, vX, vY, fX, fY, f, n;
/* Set starting Conditions */
pX = 0;
pY = 15e10;
vX = 8.9e11;
vY = 0;
days = 365;
/* Main Process */
while ( days > 0 )
{
secs = 86400;
while ( secs > 0 && (pX * pX) + (pY * pY) != 0) /* Second half of this statement prevent division by 0 */
{
f = (GRAVITY * SUNMASS * PMASS)/((pX * pX) + (pY * pY)); if (pX == 0) /* If either co-ordinate is 0, all the force will be along the othre axis. */
{ /* These if statements allow this, and prevent division of/by 0 */
fX = 0; fY = f;
}
else if (pY == 0)
{
fX = f; fY = 0;
}
else
{
n = (pX * pX) / (pY * pY);
fX = sqrt (n * f * f / (n + 1));
fY = sqrt (f * f / (n + 1));
}
if (pX > 0)
{
vX = vX - (fX / PMASS);
}
else
{
vX = vX + (fX / PMASS);
}
if (pY > 0)
{
vY = vY - (fY / PMASS);
}
else
{
vY = vY + (fY / PMASS);
}
pX = pX + vX; /* Definitely looking dodgy. What about halves? */
pY = pY + vY;
--secs;
}
--days;
printf ("x = %.2f, y = %.2f, days left = %4d \n", pX, pY, days);
}
printf ("End\n");
}
I should mention that it doesn't work properly yet. I messed around with the numbers to get it to work at all, and there's an error in it somewhere causing some strange behaviour. Still, the maths part is pretty much all there. Which leaves the graphics...
If anyone reading this has some experience writing in C or programming in general, feel free to get in touch and make suggestions.
/* Gravity simulator version 3.0 by Alex Heywood*/
/* Actual units km, kg, N, days, */
#include
#include
#include
#include
#define GRAVITY 6.67e-11
#define PMASS 5.97e22
#define SUNMASS 2e22
main()
{
int days, secs;
double pX, pY, vX, vY, fX, fY, f, n;
/* Set starting Conditions */
pX = 0;
pY = 15e10;
vX = 8.9e11;
vY = 0;
days = 365;
/* Main Process */
while ( days > 0 )
{
secs = 86400;
while ( secs > 0 && (pX * pX) + (pY * pY) != 0) /* Second half of this statement prevent division by 0 */
{
f = (GRAVITY * SUNMASS * PMASS)/((pX * pX) + (pY * pY)); if (pX == 0) /* If either co-ordinate is 0, all the force will be along the othre axis. */
{ /* These if statements allow this, and prevent division of/by 0 */
fX = 0; fY = f;
}
else if (pY == 0)
{
fX = f; fY = 0;
}
else
{
n = (pX * pX) / (pY * pY);
fX = sqrt (n * f * f / (n + 1));
fY = sqrt (f * f / (n + 1));
}
if (pX > 0)
{
vX = vX - (fX / PMASS);
}
else
{
vX = vX + (fX / PMASS);
}
if (pY > 0)
{
vY = vY - (fY / PMASS);
}
else
{
vY = vY + (fY / PMASS);
}
pX = pX + vX; /* Definitely looking dodgy. What about halves? */
pY = pY + vY;
--secs;
}
--days;
printf ("x = %.2f, y = %.2f, days left = %4d \n", pX, pY, days);
}
printf ("End\n");
}
I should mention that it doesn't work properly yet. I messed around with the numbers to get it to work at all, and there's an error in it somewhere causing some strange behaviour. Still, the maths part is pretty much all there. Which leaves the graphics...
If anyone reading this has some experience writing in C or programming in general, feel free to get in touch and make suggestions.

0 Comments:
Post a Comment
<< Home