The car starts at the position (0,0)-> (x,y). It moves only in 2 dimensions.
The .csv file contains two values separated by commas (,). The values include:
-The acceleration of the car in the direction it is currently pointing.
-The rotation of the car in which it is currently pointing.
The car always rotates at the beginning, then it accelerates for 1 second, stops
rotates again and accelerates again. and so on...
Now I have to calculate the maximum distance from the start position and the total distance covered.
However, I get in C when I run the script slightly different values than should finally come out.
Can someone take a look at my loop or algorithm? Do I have a mistake somewhere
how I calculate the covered distance?
Here is the code:
Expand|Select|Wrap|Line Numbers
- while ((token = strtok_r(save, "\n", &save)))
- {
- char* token1;
- char* save1 = token;
- double acceleration = atof(strtok_r(save1, ",", &save1));
- double rotation = atof(strtok_r(save1, ",", &save1));
- direction += rotation;
- double ax = acceleration * cos(direction);
- double ay = acceleration * sin(direction);
- double vx = v0x + ax;
- double vy = v0y + ay;
- double x = v0x + (ax/2) + x0;
- double y = v0y + (ay/2) + y0;
- fprintf(file, "%f,%f\n", x, y, direction);
- double distance = sqrt(x*x + y*y);
- if(distance > max_distance) max_distance = distance;
- total_distance += sqrt((x-x0)*(x-x0) + (y-y0)*(y-y0));
- if(sqrt(vx*vx + vy*vy) > max_speed) max_speed = sqrt(vx*vx + vy*vy);
- v0x = vx;
- v0y = vy;
- x0 = x;
- y0 = y;
- }