-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtd3.cpp
More file actions
137 lines (121 loc) · 4.05 KB
/
Copy pathtd3.cpp
File metadata and controls
137 lines (121 loc) · 4.05 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
136
137
#include <iostream>
#include "td3.hpp"
#include "support.hpp"
#include <stdlib.h>
#include <math.h> // sin, cos
using namespace std;
using namespace support;
double* extend_array(double* array, int length, int new_size) {
double *new_array = new double[new_size];
for (int i = 0 ; i < new_size; i ++){
if(i< length){
new_array[i] = array[i];
}
else{
new_array[i] = 0;
}
}
delete array;
return new_array;
}
double* shrink_array(double* array, int length, int new_size) {
double *new_array = new double[new_size];
for (int i = 0 ; i < length; i ++){
if (i<new_size){
new_array[i] = array[i];
}
}
delete array;
return new_array;
}
double* append_to_array(double element,
double* array,
int ¤t_size,
int &max_size) {
if (current_size == max_size){
max_size+=5;
array = extend_array(array,current_size, max_size);
}
array[current_size] = element;
current_size ++;
return array;
}
double* remove_from_array(double* array,
int ¤t_size,
int &max_size) {
if (current_size > 0){
current_size --;
}
if (current_size <= (max_size - 5)){
max_size-=5;
array = shrink_array(array,current_size, max_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)) {
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);
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;
}
}
return hit_target;
}
void merge_telemetry(double **telemetries,
int tot_telemetries,
int *telemetries_sizes,
double* &telemetry,
int &telemetry_current_size,
int &telemetry_max_size) {
for (int i=0; i<tot_telemetries;i ++){
for(int j=0;j<telemetries_sizes[i];j ++){
telemetry = append_to_array((telemetries[i][j]),(telemetry),(telemetry_current_size), (telemetry_max_size));
// std::cout << telemetries[i][j] << std::endl;
}
}
double tmp;
int i, j;
for (i = 0; i < (telemetry_current_size); i += 3) {
for (j = 0; j < (telemetry_current_size-i-3); j+=3) {
if (telemetry[j] > telemetry[j+3]){
tmp= telemetry[j];
telemetry[j] = telemetry[j+3];
telemetry[j+3] = tmp;
tmp = telemetry[j+1];
telemetry[j+1] = telemetry[j+4];
telemetry[j+4] = tmp;
tmp = telemetry[j+2];
telemetry[j+2] = telemetry[j+5];
telemetry[j+5] = tmp;
}
}
}
}