-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_input.c
159 lines (137 loc) · 3.57 KB
/
user_input.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
#include "physics.h"
#include "object.h"
#include <stdio.h>
#include <stdlib.h>
#define STRING_MAX 64
extern int n_objects;
extern object_t** objects;
extern char centering;
extern int center_object;
int double_from_user(double *from_user) {
int i=0;
char c;
char word[STRING_MAX];
for (i=0; i<STRING_MAX; i++) {
word[i] = 0;
}
i=0;
while ((c=getchar()) != '\n') {
word[i++] = c;
if ( ((c-'0') > 9 || (c-'0') < 0) && c != '.' && c != '-') {
printf("Input must be a number using '.' as decimal\n");
while ((c=getchar()) != '\n'); /* Skips to end of line */
return -1;
}
}
*from_user = atof(word);
return 0;
}
int int_from_user(int *from_user) {
int i=0;
char c;
char word[STRING_MAX];
for (i=0; i<STRING_MAX; i++) {
word[i] = 0;
}
i=0;
while ((c=getchar()) != '\n') {
word[i++] = c;
if ( ((c-'0') > 9 || (c-'0') < 0) && c != '-') {
printf("Input must be an integer number\n");
while ((c=getchar()) != '\n'); /* Skips to end of line */
return -1;
}
}
*from_user = atoi(word);
return 0;
}
/*
* Allows the user to input objects
*/
int add_custom_objects(void) {
n_objects = 0;
object_t** tmp;
int rc, i = 0;
double *from_user = malloc(sizeof(double));
printf("Adding custom objects...\n");
while (true) {
size_t new_size = sizeof(object_t*) * (++n_objects);
tmp = (object_t**) realloc(objects, new_size);
if (tmp) {
objects = tmp;
} else {
printf("Reallocating of memory for the %dth object failed.\n", i);
printf("Exiting...\n");
return -1;
}
do {
printf("Enter x posision: ");
rc = double_from_user(from_user);
objects[i]->p.x = *from_user;
} while (rc);
do {
printf("Enter y posision: ");
rc = double_from_user(from_user);
objects[i]->p.y = *from_user;
} while (rc);
do {
printf("Enter mass: ");
rc = double_from_user(from_user);
objects[i]->m = *from_user;
} while (rc);
do {
printf("Enter radius: ");
rc = double_from_user(from_user);
objects[i]->r = *from_user;
} while (rc);
do {
printf("Enter x velocity: ");
rc = double_from_user(from_user);
objects[i]->v.x = *from_user;
} while (rc);
do {
printf("Enter y velocity: ");
rc = double_from_user(from_user);
objects[i]->v.y = *from_user;
} while (rc);
//print_object_values(objects[i]);
printf("Type Y to add more, enter to start simulation... ");
char c = getchar();
if (c != '\n') {
while (getchar() != '\n'); /* Skips to line feed */
}
if (c != 'Y' && c != 'y') { /* No more objects */
printf("\n");
if (centering && n_objects <= center_object) { /* Invalid center object */
printf("You added %d objects, but centered object was set to %d\n",
n_objects, center_object);
printf("Objects are zero-indexed, meaning the first object is object #0\n");
int *from_user = malloc(sizeof(int));
int rc;
do {
printf("Enter new center object (less than %d)...", n_objects);
rc = int_from_user(from_user);
} while (rc || n_objects <= *from_user);
center_object = *from_user;
free(from_user);
}
break;
}
i++;
}
free(from_user);
return 0;
}
/*
* Initializes three default objects for the simulation
*/
void add_default_objects(void) {
#define DEFAULT_OBJECTS
#ifdef DEFAULT_OBJECTS
// POS MASS RADIUS VELOCITY
insert_new_object(f_vec(0, 0), 40000, 10, f_vec(0, 0));
insert_new_object(f_vec(200, 200), 50, 1, f_vec(-0.8, 0.8));
insert_new_object(f_vec(100, 100), 3, 0.9, f_vec(-1, 1));
insert_new_object(f_vec(-300, -300), 3, 0.9, f_vec(1.1, -0.3));
#endif
}