-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectile.cpp
More file actions
158 lines (126 loc) · 3.27 KB
/
Projectile.cpp
File metadata and controls
158 lines (126 loc) · 3.27 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//////////////////////////////////////////////////////////////
//
// Your #includes here; make sure you are allowed them ...
//
#include <stdlib.h>
#include <cmath>
#include <limits>
//////////////////////////////////////////////////////////////
//
// #includes and function declarations; do not remove
//
#ifndef MARMOSET_TESTING
#include <iostream>
using namespace std;
#endif
//////////////////////////////////////////////////////////////
//
// Function declarations; do not remove
// Replace "..." in bisectionHelper() with any parameter(s)
// you need to ensure termination when precision and
// minIntervalSize are too tightly specified.
//
bool projectileDestination(const float h, const float v, const float m,
const float theta, const float d,
const float t, const float b, const float w,
float& destX, float& destY);
//////////////////////////////////////////////////////////////
//
// Your code here ...
//
// Return std::numeric_limits<float>::quiet_NaN() (comes from
// <limits>) if cannot compute the root for some reason
//
bool projectileDestination(const float h, const float v, const float m,
const float theta, const float d,
const float t, const float b, const float w,
float& destX, float& destY) {
if (theta == 0)
{
destX = 0;
destY = h;
}
if (theta < -90 || theta > 270)
return false;
if (h < 0 || v < 0 || m < 0 || d < 0 || b < 0 || w < 0)
return false;
float distance = d;
float build = b;
float width = w;
float angle = theta;
float height = h;
float vrtd = t;
float vi = v;
float destx = destX;
float desty = destY;
float range;
float time;
float g = 9.8;
double Pi = 3.1415926535897;
angle = (theta*Pi)/180;
float vx = vi*cos(angle);
float vy = vi*sin(angle);
if (v == 0 || theta == 90)
{
destY = h;
destX = 0;
return true;
}
time = vy/g;
float y = pow(vy, 2)/(2*g);
float timecheck = time + sqrt(2*g*(h+y))/g;
range = vx*timecheck;
if (b < range && range < b+w)
{
destY = t;
destX = range;
return true;
}
if (range < b)
{
destY = 0;
destX = range;
return true;
}
if (range > b+w)
{
destY = 0;
destX = range;
return true;
}
if (range == b)
{
destX = b;
destY = vy*time - 0.5*g*pow(time, 2);
return true;
}
}
//////////////////////////////////////////////////////////////
//
// Test Driver
//
// Do not remove the #ifndef and its associated #endif
// This is required so that when you submit your code,
// this test driver will be ignored by Marmoset
//
#ifndef MARMOSET_TESTING
#define isNaN(X) (X != X) // NaN is the only float that is not equal to itself
int main(const int argc, const char* const argv[]) {
// Some test driver code here ....
float h = 50;
float v = 24.2;
float m = 1;
float theta = 27.89; // Angle in degrees; will need to be converted by function
float d = 100;
float t = 20;
float b = 30;
float w = 20;
float hitsAtX;
float hitsAtY;
if (projectileDestination(h,v,m,theta,d,t,b,w,hitsAtX,hitsAtY))
cout << "Projectile hit at (" << hitsAtX << ", " << hitsAtY << ")" <<endl;
else
cout << "Unable to calculate where projectile hits." << endl;
return 0;
}
#endif