What does this do?

View previous topic View next topic Go down

What does this do?

Post by Daimon on Sun Feb 20, 2011 7:26 pm

I'm just curious.

#include <math.h>

double dist(double x1,double y1,double x2,double y2) {
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}

double slope(double x1,double y1,double x2,double y2) {
return(atan((y2-y1)/(x2-x1)));
}

void main(int argc,char **argv) {

struct {
double x;
double y;
double speed;
double new_speed;
double dist;
} a,b,c,you;

double dist_a, dist_b,dist_c;
double raptor_accel=4.0;
double you_dir=30.0;
double interval=0.001;
double time=0.0000;
double grad;
double pi = 3.1415926535897932384626433832795;
int i;
double best_dir=0;
double best_time=0.0;
char best_got;
char got;

for (you_dir=32;you_dir<33;you_dir += 0.001) {

you.x = you.y = 0.0;
a.x = 0; a.y = 11.547005383792515290182975610039;
b.x = -10;b.y = -5.7735026918962576450914878050196;
c.x = 10;c.y=-5.7735026918962576450914878050 196;
a.dist = b.dist = c.dist = you.dist = 0.0;
a.speed = b.speed = c.speed = 0.0;
a.new_speed = b.new_speed = c.new_speed = 0.0;
you.speed = 6.0;
time = 0;

while (1) {

dist_a = dist(a.x,a.y,you.x,you.y);
dist_b = dist(b.x,b.y,you.x,you.y);
dist_c = dist(c.x,c.y,you.x,you.y);
/*
printf("you at at (%f,%f) -travelled %f\n",you.x,you.y,you.dist);
printf("raptor a at (%f,%f) ( distance %f) - travelled %f\n",a.x,a.y,dist_a,a.dist);
printf("raptor b at (%f,%f) ( distance %f) - travelled %f\n",b.x,b.y,dist_b,b.dist);
printf("raptor c at (%f,%f) ( distance %f) - travelled %f\n",c.x,c.y,dist_c,c.dist);
*/

if (dist_a == 0.0 ) {
printf("Raptor a got you in %f secs!\n",time);
break;
}
if (dist_b == 0.0 ) {
printf("Raptor b got you in %f secs!\n",time);
break;
}
if (dist_c == 0.0 ) {
printf("Raptor c got you in %f secs!\n",time);
break;
}

time += interval;

/* You Move first */

you.x += you.speed*interval*cos(pi*you_dir/180)
;
you.y += you.speed*interval*sin(pi*you_dir/180)
;

you.dist +=you.speed*interval;

/* Accelerate the raptors */

a.new_speed += raptor_accel*interval;
b.new_speed += raptor_accel*interval;
c.new_speed = b.new_speed;

if (a.new_speed > 10.0 )
a.new_speed = 10.0;

if (b.new_speed > 25.0 )
c.new_speed = b.new_speed = 25.0;
/*
printf("a raptor_speed is now %f\n",a.new_speed);
printf("b and c raptor_speed is now %f\n",b.new_speed);
*/

/* Raptor a moves */

if ( a.speed*interval > dist_a ) {
/*
printf("Raptor a will get you within this interval time is %f secs!\n",time);
*/
got = 'a';
break;
}

grad = slope(a.x,a.y,you.x,you.y);

a.x += (a.new_speed+a.speed)/2*interval*cos(g rad);
a.y += (a.new_speed+a.speed)/2*interval*sin(g rad);
a.dist += (a.new_speed+a.speed)/2*interval;
a.speed = a.new_speed;

/* Raptor b moves */

if ( b.speed *interval > dist_b ) {
/*
printf("Raptor b will get you within this interval time is %f secs!\n",time);
*/
got = 'b';
break;
}

grad = slope(b.x,b.y,you.x,you.y);
b.x += (b.new_speed+b.speed)/2*interval*cos(g rad);
b.y += (b.new_speed+b.speed)/2*interval*sin(g rad);
b.dist += (b.new_speed+b.speed)/2*interval;
b.speed = b.new_speed;

/* Raptor c moves */

if ( c.speed*interval > dist_c ) {
/*
printf("Raptor c will get you within this interval time is %f secs!\n",time);
*/
got = 'c';
break;
}

grad = slope(c.x,c.y,you.x,you.y);
if ( c.x > you.x ) {
c.x -= (c.new_speed+c.speed)/2*interval*cos(g rad);
c.y -= (c.new_speed+c.speed)/2*interval*sin(g rad);
}
else {
c.x += (c.new_speed+c.speed)/2*interval*cos(g rad);
c.y += (c.new_speed+c.speed)/2*interval*sin(g rad);
}
c.dist += (c.new_speed+c.speed)/2*interval;
c.speed = c.new_speed;

}

if ( time > best_time ) {
best_time = time;
best_dir = you_dir;
best_got = got;
}
}

printf("Best time is %f best dir is %f got by raptor %c\n",best_time,best_dir,best_got);
}

Daimon
Human
Human

Posts : 9
Join date : 2010-11-13
Age : 21

Back to top Go down

Re: What does this do?

Post by Break on Sun Feb 20, 2011 7:34 pm

it's a C/C++ program making fun of an xkcd comic. It basically solves the problem here:
XKCD 135

While whoever made it is probably decent in math, they over-thought their precision. There is really no use to have decimals to that precision, because it's doubtful that they are that accurate.
avatar
Break
Student
Student

Posts : 128
Join date : 2010-09-28
Age : 29
Location : Texas

Back to top Go down

Re: What does this do?

Post by Daimon on Sun Feb 20, 2011 7:58 pm

So, what's the answer to the problem? XD

Daimon
Human
Human

Posts : 9
Join date : 2010-11-13
Age : 21

Back to top Go down

Re: What does this do?

Post by Break on Mon Feb 21, 2011 4:09 am

Do the math, I'm too busy to work out a problem for you.
avatar
Break
Student
Student

Posts : 128
Join date : 2010-09-28
Age : 29
Location : Texas

Back to top Go down

Re: What does this do?

Post by Daimon on Mon Feb 21, 2011 12:14 pm

I thought it gave the answer @_@


Daimon
Human
Human

Posts : 9
Join date : 2010-11-13
Age : 21

Back to top Go down

Re: What does this do?

Post by Break on Mon Feb 21, 2011 3:29 pm

i don't have a c/c++ compiler on this computer, and since i never use it it wouldn't be worth my time to install one. Go install Visual Studio and compile the code if you want to see the exact answer from the program
avatar
Break
Student
Student

Posts : 128
Join date : 2010-09-28
Age : 29
Location : Texas

Back to top Go down

Re: What does this do?

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum