-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtd3.cpp
159 lines (122 loc) · 4.23 KB
/
td3.cpp
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
136
137
138
139
140
141
142
143
#include <iostream>
#include "td3.hpp"
#include "support.hpp"
#include <stdlib.h>
#include <math.h> // sin, cos
#include <assert.h>
using namespace std;
using namespace support;
double* extend_array(double* array, int length, int new_size) {
// IMPLEMENT YOUR FUNCTION HERE
double* allocate = new double[new_size];
for(int i=0; i<new_size; i++){
if (i<length){
*(allocate+i) = *(array+i);
} else {
*(allocate+i) = 0;
}
}
delete array;
return allocate;
}
double* shrink_array(double* array, int length, int new_size) {
double* allocate = new double[new_size];
for(int i=0; i<new_size; i++){
if (i<length){
*(allocate+i) = *(array+i);
}
}
delete array;
return allocate;
}
double* append_to_array(double element,
double* array,
int ¤t_size,
int &max_size) {
if (current_size == max_size){
array = extend_array(array, current_size, max_size+5);
max_size = max_size+5;
}
*(array+current_size) = element;
current_size++;
return array;
}
double* remove_from_array(double* array,
int ¤t_size,
int &max_size) {
if (current_size <= max_size - 4){
array = shrink_array(array, current_size, max_size-5);
max_size = max_size-5;
}
current_size--;
return array;
}
bool simulate_projectile(const double magnitude, const double angle,
const double simulation_interval,
double *targets, int &tot_targets,
int *obstacles, int tot_obstacles,
double* &telemetry,
int &telemetry_current_size,
int &telemetry_max_size) {
// YOU CAN MODIFY THIS FUNCTION TO RECORD THE TELEMETRY
bool hit_target, hit_obstacle;
double v0_x, v0_y, x, y, t;
double PI = 3.14159265;
double g = 9.8;
v0_x = magnitude * cos(angle * PI / 180);
v0_y = magnitude * sin(angle * PI / 180);
t = 0;
x = 0;
y = 0;
hit_target = false;
hit_obstacle = false;
while (y >= 0 && (! hit_target) && (! hit_obstacle)) {
double * target_coordinates = find_collision(x, y, targets, tot_targets);
if (target_coordinates != NULL) {
remove_target(targets, tot_targets, target_coordinates);
hit_target = true;
} else if (find_collision(x, y, obstacles, tot_obstacles) != NULL) {
hit_obstacle = true;
} else {
t = t + simulation_interval;
y = v0_y * t - 0.5 * g * t * t;
x = v0_x * t;
}
telemetry = append_to_array(t, telemetry, telemetry_current_size, telemetry_max_size);
telemetry = append_to_array(x, telemetry, telemetry_current_size, telemetry_max_size);
telemetry = append_to_array(y, telemetry, telemetry_current_size, telemetry_max_size);
}
return hit_target;
}
void sort(double *obstacles, int num_obstacles) {
num_obstacles = int(num_obstacles/3);
for(int i=0;i<num_obstacles;i++){
for(int j=i+1;j<num_obstacles;j++){
if (obstacles[3*i] > obstacles[3*j]){
double temp1 = obstacles[3*i];
double temp2 = obstacles[3*i+1];
double temp3 = obstacles[3*i+2];
obstacles[3*i] = obstacles[3*j];
obstacles[3*i+1] = obstacles[3*j+1];
obstacles[3*i+2] = obstacles[3*j+2];
obstacles[3*j] = temp1;
obstacles[3*j+1] = temp2;
obstacles[3*j+2] = temp3;
}
}
}
}
void merge_telemetry(double **telemetries,
int tot_telemetries,
int *telemetries_sizes,
double* &global_telemetry,
int &global_telemetry_current_size,
int &global_telemetry_max_size) {
// IMPLEMENT YOUR FUNCTION HERE
for (int i=0; i<tot_telemetries; i++){
for (int j=0; j<telemetries_sizes[i]; j++){
global_telemetry = append_to_array(telemetries[i][j], global_telemetry, global_telemetry_current_size, global_telemetry_max_size);
}
}
sort(global_telemetry, global_telemetry_current_size);
}