Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following function using the following structure for a track. typedef

ID: 3828796 • Letter: W

Question

Write the following function using the following structure for a track. typedef struct { double time; // time of the track double x, y; // position of track double vx, vy; // velocity of track double ax, ay; // velocity of track } Point; (a) A function that determines magnitude of the speed and acceleration of the track (e.g. calcSA(track t, double *speed, double *ac); (b) A function that determines the heading of the track (angle of motion from the positive x-axis). The trigonometric functions (sin, cos, tan) are defined in math.h and all return the angle in radians. In other words, calculate the angle the velocity vector makes, positive counter-clockwise, with respect to the positive x-axis). For ex: double calc_heading(track t); // returns the angle in radians (c) A function that takes in an array of tracks and determines the average speed, e.g. double aveSpeed(track t[]); (d) A function that takes in three tracks and determine if the tracks lie on a straight line, e.g. int isStraightLine(track t1, track t2, track t3). The function returns 1 if the tracks are collinear and 0 if they are not.

Explanation / Answer

Please refer below code.I used test data for reference.You can change whatever value you want by editing main() function

#include<stdlib.h>
#include<stdio.h>
#include<math.h>

typedef struct track
{
double time;
double x,y;
double vx,vy;
double ax,ay;
}track;
//speed and acceleration magnitude
//mag = sqrt(xcomp^2 + ycomp^2)
void calcSA(track t, double *speed, double *ac)
{
*speed = sqrt((t.vx * t.vx) + (t.vy * t.vy));
*ac = sqrt((t.ax * t.ax) + (t.ay * t.ay));
}
//calculating angle
double calc_heading(track t)
{
double angle;

//angle = tan^-1(vy/vx)
angle = atan(t.vy/t.vx);
return angle;
}
//calculating average speed
double aveSpeed(track t[])
{
int i;
double avge_speed = 0.0;
for(i = 0; i < 3; i++)
{
avge_speed += sqrt((t[i].vx * t[i].vx) + (t[i].vy * t[i].vy));
}
return avge_speed/3;
}
int isStraightLine(track t1, track t2, track t3)
{
double s1,s2,s3;
double epsilon=0.001;

//checking points are collinear by calculatiog slopes og line between two points
s1 = fabs(t2.x - t1.x) / fabs(t2.y - t1.y);
s2 = fabs(t3.x - t1.x) / fabs(t3.y - t1.y);
s3 = fabs(t2.x - t2.x) / fabs(t3.y - t2.y);

if((fabs(s1 - s2) < epsilon) && (fabs(s1 - s3) < epsilon))
return 1;

return 0;
}
int main()
{
double speed,ac,angle,avge_speed;
track t;
int colinear;
track arr[3];
track t1,t2,t3;

/* for Example.
I am taking random values for reference. You can take any values
*/
t.time = 0.3; //ms
t.x = 0.7;
t.y = 0.8;
t.vx = 35.7; //m/s
t.vy = 63.9;
t.ax = 3.2;
t.ay = 4.2;

calcSA(t, &speed, &ac);
printf("Speed = %f Acceleration = %f ", speed, ac);

angle = calc_heading(t);
printf("Angle in Radian = %f ", angle);

arr[0].vx = 22.3;
arr[0].vy = 32.1;

arr[1].vx = 21.1;
arr[1].vy = 1.2;

arr[2].vx = 56.7;
arr[2].vy = 30.3;

avge_speed = aveSpeed(arr);
printf("Average Speed = %f ", avge_speed);

t1.x = 1.1;
t1.y = 1.2;
t2.x = 2.1;
t2.y = 2.2;
t3.x = 3.1;
t3.y = 3.2;
colinear = isStraightLine(t1, t2,t3);
if(colinear)
printf("Tracks are colinear ");
else
printf("Tracks are NOT colinear ");

return 0;
}

Please refer below output for reference

Speed = 73.196311 Acceleration = 5.280152
Angle in Radian = 1.061309
Average Speed = 41.502719
Tracks are NOT colinear

Process returned 0 (0x0) execution time : 0.040 s
Press any key to continue.