-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcannon.c
208 lines (115 loc) · 4.14 KB
/
cannon.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#import "stdio.h"
#import "math.h"
#import "time.h" //For calculating elapsed time
typedef int bool;
#define true 1
#define false 0
/* System constants & Debug miscellania */
#define SCALE_FACTOR 0.001
#define CIRC_SCALE_FACTOR 0.005
#define DEBUG false
/* Debug function */
//void show(const char *format, ...){
// if(DEBUG) { printf(*format, ...); }
//}
/* */
/** Main structs & functions **/
/* Defining the Fortress's struct and functions */
struct Circle {
double x_0, y_0, radius;
} fortress;
// Returns wether or not the point (x,y) belongs to the circle.
bool belongsTo(double *x, double *y){
return (*x - fortress.x_0)*(*x - fortress.x_0) + (*y - fortress.y_0)*(*y - fortress.y_0) <= fortress.radius*fortress.radius;
}
/* */
/* Defining the Cannon's struct and functions */
struct LineOfSight {
double x_0, y_0, angle, spread;
} cannon;
// Given a vector defined by x and y, returns its angle in relation to the x axis
double getAngle(double x, double y){
double angle;
angle = acos( ( x/( pow( x * x + y * y,0.5) ) ) );
if(x < 0 && y < 0) { return (2*(M_PI - angle) + angle); } //Third quad.
else if(x > 0 && y < 0) { return (2*M_PI - angle); } //Fourth quad.
else { return angle; } //Defaults
}
// Determines if the point is within the area of sight of the cannon, or not
bool isHit(double *x, double *y){
double vector_angle = getAngle( (*x) - cannon.x_0, (*y) - cannon.y_0);
double hspread = cannon.spread/2; // stores half the spread value for performance reasons
return (vector_angle >= cannon.angle - hspread && vector_angle <= cannon.angle + hspread);
}
/* */
/* Main iteration */
struct Iteration {
double x_i, y_i, x_begin, y_begin, x_end, y_end;
double length, area_grid, total_area;
} iterate;
// Iterates over a circunscribed square in relation to the fortress's circle
void scan(){
iterate.length = 2 * fortress.radius * SCALE_FACTOR;
iterate.area_grid = iterate.length * iterate.length;
iterate.total_area = 0.0;
iterate.x_begin = fortress.x_0 - fortress.radius;
iterate.x_end = fortress.x_0 + fortress.radius;
iterate.y_end = fortress.y_0 - fortress.radius;
iterate.y_i = fortress.y_0 + fortress.radius;
while (iterate.y_i >= iterate.y_end){
iterate.x_i = iterate.x_begin;
while (iterate.x_i <= iterate.x_end){
if(DEBUG) { printf("y: %lf x: %lf", iterate.y_i, iterate.x_i); }
if( belongsTo( &(iterate.x_i), &(iterate.y_i) ) && isHit ( &(iterate.x_i), &(iterate.y_i) ) ) {
iterate.total_area += iterate.area_grid;
if(DEBUG) { printf("Total area so far: %lf", iterate.total_area); }
}
iterate.x_i += iterate.length;
}
iterate.y_i -= iterate.length;
}
printf("%.1lf\n", iterate.total_area);
}
/*
// Iterates over a circle (i.e. the fortress itself)
struct IterateCircle{
double total_area, dangle, dradius, area_grid;
double angle;
} itcirc;
double getx(){ return (fortress.x_0 + fortress.radius * cos(itcirc.angle)); }
double gety() { return (fortress.y_0 + fortress.radius * sin(itcirc.angle)); }
void scan_circle(){
itcirc.dangle = 5*CIRC_SCALE_FACTOR;
itcirc.dradius = fortress.radius * CIRC_SCALE_FACTOR;
itcirc.area_grid = itcirc.dradius * itcirc.dangle * fortress.radius;
itcirc.total_area = 0;
while(fortress.radius > 0){
while(itcirc.angle < 2*M_PI){
if(isHit(getx(), gety()) { itcirc.total_area += itcirc.area_grid; }
itcirc.angle += itcirc.dangle;
}
fortress.radius -= itcirc.dradius;
itcirc.angle = 0;
itcirc.area_grid = itcirc.dradius * itcirc.dangle * fortress.radius;
}
printf("%.1lf\n", itcirc.total_area);
}
*/
/* */
/* I/o Voodoo */
bool getInput() {
scanf("%lf%lf%lf", &fortress.x_0, &fortress.y_0, &fortress.radius);
if(fortress.x_0 == 0 && fortress.y_0 == 0 && fortress.radius == 0) { return false; }
scanf("%lf%lf%lf%lf", &cannon.x_0, &cannon.y_0, &cannon.angle, &cannon.spread);
cannon.angle = cannon.angle * 2*M_PI/360;
cannon.spread = cannon.spread * 2*M_PI/360;
return true;
}
/* */
int main(){
while(getInput()){
// if (DEBUG) { printf("%lf %lf %lf \n %lf %lf %lf %lf", fortress.x_0, fortress.y_0, fortress.radius, cannon.x_0, cannon.y_0, cannon.angle, cannon.spread); }
scan();
}
return 0;
}