-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonda_cuerda.c
184 lines (155 loc) · 4.72 KB
/
onda_cuerda.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
#include "raylib.h"
#include <math.h>
#include <stdio.h>
#define SAMPLES 10000
#define SIZE 1
#define OFFSET 140
#define SPACING 0.01f
float A = 6;
float w = .4f * M_PI;
float k = M_PI / 6;
float fi = 0;
int tm = 0;
float az = 10; // amplitude zoom
float lz = 15; // longitud zoom
int settingsSwitch = 0; // 1 = A , 2 = w, 3 = k, 4 = fi
float y(float x, float t) { return A * sinf((w * t) - (k * x) + fi); }
float v(float x, float t) { return A * w * cosf((w * t) - (k * x) + fi); }
float a(float x, float t) { return -A * w * w * sinf((w * t) - (k * x) + fi); }
int main(void) {
// Initialization
const int screenWidth = 1600;
const int screenHeight = 900;
InitWindow(screenWidth, screenHeight, "Onda cuerda");
Camera2D camera = {0};
camera.offset = (Vector2){20, screenHeight / 2.0f};
camera.rotation = 0.0f;
camera.zoom = 1.0f;
Rectangle sample[SAMPLES] = {0};
Color sampleColor[SAMPLES] = {0};
for (int i = 0; i < SAMPLES; i++) {
sample[i].height = SIZE;
sample[i].width = SIZE;
sample[i].x = i * SPACING;
sample[i].y = 0;
sampleColor[i] = BLACK;
}
SetTargetFPS(75);
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//---------------------------------------------------------------------------------
// 2d camera controlls
// Camera rotation
if (IsKeyDown(KEY_Q))
camera.rotation--;
else if (IsKeyDown(KEY_E))
camera.rotation++;
// Reset
if (IsKeyPressed(KEY_R)) {
camera.zoom = 1.0f;
camera.rotation = 0.0f;
az = 10;
tm = 0;
lz = 15;
}
// input
if (IsKeyDown(KEY_PAGE_DOWN)) {
tm -= 1;
}
if (IsKeyDown(KEY_PAGE_UP)) {
tm += 1;
}
if (IsKeyDown(KEY_UP))
az += .1;
else if (IsKeyDown(KEY_DOWN))
az -= .1;
if (IsKeyDown(KEY_RIGHT))
lz += .05f;
else if (IsKeyDown(KEY_LEFT))
lz -= .05f;
if (IsKeyPressed(KEY_A))
settingsSwitch = 1;
if (IsKeyPressed(KEY_W))
settingsSwitch = 2;
if (IsKeyPressed(KEY_K))
settingsSwitch = 3;
if (IsKeyPressed(KEY_F))
settingsSwitch = 4;
if (IsKeyPressed(KEY_ONE))
settingsSwitch = 1;
if (IsKeyPressed(KEY_TWO))
settingsSwitch = 2;
if (IsKeyPressed(KEY_THREE))
settingsSwitch = 3;
if (IsKeyPressed(KEY_FOUR))
settingsSwitch = 4;
switch (settingsSwitch) {
case 1:
A += ((float)GetMouseWheelMove() * 0.05f);
break;
case 2:
w += ((float)GetMouseWheelMove() * 0.05f);
break;
case 3:
k += ((float)GetMouseWheelMove() * 0.05f);
break;
case 4:
fi += ((float)GetMouseWheelMove() * 0.05f);
break;
default:
break;
}
// calculate
for (int i = 0; i < SAMPLES; i++) {
sample[i].x = i * SPACING * lz;
sample[i].y = y(i * SPACING, tm) * az;
}
// convert numbers to text
char time[11];
sprintf(time, "%d", tm); // convert int t to time
char At[11];
sprintf(At, "%f", A); // convert amplitud to text
char wt[11];
sprintf(wt, "%f", w); // convert Pulsacion to text
char kt[11];
sprintf(kt, "%f", k); // convert k to text
char fit[11];
sprintf(fit, "%f", fi); // convert fi0 to text
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode2D(camera);
for (int i = 0; i < SAMPLES; i++) {
DrawRectangleRec(sample[i], sampleColor[i]);
}
EndMode2D();
DrawFPS(10, 10);
DrawText("Flechas para zoom, PageUp PageDown controla el tiempo, R "
"restaura los valores al estado inicial.",
10, 35, 20, GRAY);
DrawText(
"La tecla A activa la configuración de la amplitud, W de la pulsación, "
"K del valor k y F del desfase. La rueda del ratón cambio los valores.",
10, 60, 20, GRAY);
DrawText("Amplitud:", 10, 100, 20, BLACK);
DrawText(At, OFFSET, 100, 20, BLACK);
DrawText("Pulsación:", 10, 130, 20, BLACK);
DrawText(wt, OFFSET, 130, 20, BLACK);
DrawText("Nº onda (k):", 10, 160, 20, BLACK);
DrawText(kt, OFFSET, 160, 20, BLACK);
DrawText("Desfase:", 10, 190, 20, BLACK);
DrawText(fit, OFFSET, 190, 20, BLACK);
DrawText("Tiempo:", 10, 220, 20, BLACK);
DrawText(time, OFFSET, 220, 20, BLACK);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}