-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasteringWebtasksec.cpp
More file actions
135 lines (114 loc) · 3.8 KB
/
MasteringWebtasksec.cpp
File metadata and controls
135 lines (114 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
struct Speed{
int sx; int sy; int sz;
};
typedef struct Speed Sp;
struct Car{
string make;
string model;
int year;
Sp speed;
int x;
int y;
int z;
};
void accelerate(Car* car, int ix, int iy, int iz) {
car->speed.sx += ix;
car->speed.sy += iy;
car->speed.sz += iz;
}
void brake(Car* car, int dx, int dy, int dz) {
car->speed.sx -= dx;
car->speed.sy -= dy;
car->speed.sz -= dz;
}
void move(Car* car) {
car->x += car->speed.sx;
car->y += car->speed.sy;
car->z += car->speed.sz;
}
bool detect_collision(Car* car1, Car* car2) {
double distance = sqrt(pow((car1->x - car2->x), 2) + pow((car1->y - car2->y), 2) + pow((car1->z - car2->z), 2));
if (distance <= 0) { // assuming collision distance of 0 units
return true;
} else {
return false;
}
}
double time_to_collision(Car* car1, Car* car2) {
double speedcar1 = pow(pow((car1->speed.sx), 2) + pow((car1->speed.sy), 2) + pow((car1->speed.sz), 2), (1.0/3));
double speedcar2 = pow(pow((car2->speed.sx), 2) + pow((car2->speed.sy), 2) + pow((car2->speed.sz), 2), (1.0/3));
double relative_speed = fabs(speedcar1 - speedcar2);
double distance = sqrt(pow((car1->x - car2->x), 2) + pow((car1->y - car2->y), 2) + pow((car1->z - car2->z), 2));
if (relative_speed == 0) {
return INFINITY; // cars are moving at the same speed
} else {
double time = distance / relative_speed;
return time;
}
}
int main() {
int spx1, spy1, spz1, spx2, spy2, spz2, x1, y1, z1, x2, y2, z2;
int ix, iy, iz, dx, dy, dz;
cout << "Enter speed with co-ordinates of car1: ";
cin >> spx1 >> spy1 >> spz1;
cout << "Enter co-ordinates of car1: ";
cin >> x1 >> y1 >> z1;
cout << "Enter speed with co-ordinates of car2: ";
cin >> spx2 >> spy2 >> spz2;
cout << "Enter co-ordinates of car2: ";
cin >> x2 >> y2 >> z2;
Sp sp1;
sp1.sx = spx1; sp1.sy = spy1; sp1.sz = spz1;
Sp sp2;
sp2.sx = spx2; sp2.sy = spy2; sp2.sz;
Car car1 = {"Hyundai","Verna" ,2020, sp1, x1, y1, z1};
Car car2 = {"Kia", "Seltos", 2021, sp2, x2, y2, z2};
int p = 0;
char q;
printf("Which car do you want to accelerate or brake?");
scanf("%d", &p);
if (p == 'A') {
printf("Press A if you want to accelerate, B if you want to brake.");
scanf(" %c", &q);
if (q == 'A') {
int ix, iy, iz;
printf("Enter acceleration with 3 components: ");
scanf("%d %d %d", &ix, &iy, &iz);
accelerate(&car1, ix, iy, iz);
} else {
int dx, dy, dz;
printf("Enter braking with 3 components: ");
scanf("%d %d %d", &dx, &dy, &dz);
brake(&car1, dx, dy, dz);
}
} else {
printf("Press A if you want to accelerate, B if you want to brake.");
scanf(" %c", &q);
if (q == 'A') {
int ix, iy, iz;
printf("Enter acceleration with 3 components: ");
scanf("%d %d %d", &ix, &iy, &iz);
accelerate(&car2, ix, iy, iz);
} else {
int dx, dy, dz;
printf("Enter braking with 3 components: ");
scanf("%d %d %d", &dx, &dy, &dz);
brake(&car2, dx, dy, dz);
}
}
double time_to_collision1 = time_to_collision(&car1, &car2);
move(&car1);
move(&car2);
if (detect_collision(&car1, &car2)) {
printf("Cars will not collide.\n");
return 0;
} else {
printf("Cars will collide.\n");
}
printf("Time to collision: %lf", time_to_collision1);
return 0;
}