aboutsummaryrefslogtreecommitdiff
path: root/src/bez7.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bez7.c')
-rw-r--r--src/bez7.c283
1 files changed, 283 insertions, 0 deletions
diff --git a/src/bez7.c b/src/bez7.c
new file mode 100644
index 0000000..d63014c
--- /dev/null
+++ b/src/bez7.c
@@ -0,0 +1,283 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <time.h>
4#include <raylib.h>
5#include <rlgl.h>
6#include <raymath.h>
7
8typedef Vector2 bezier7[8];
9
10struct input {
11 Vector2 r_beg;
12 Vector2 r_end;
13 Vector2 v_beg;
14 Vector2 v_end;
15 float v_max;
16 float a_max;
17 float j_max;
18};
19
20#define RGBA(HEX) (Color) { \
21 (HEX) >> 3*8 & 0xFF, \
22 (HEX) >> 2*8 & 0xFF, \
23 (HEX) >> 1*8 & 0xFF, \
24 (HEX) >> 0*8 & 0xFF, \
25 }
26
27// #define LABEL_WIDTH 140
28#define LABEL_WIDTH 300
29#define SLIDER_WIDTH 40
30#define ITEM_WIDTH (LABEL_WIDTH + SLIDER_WIDTH)
31#define ITEM_HEIGHT 15
32#define ITEM_MARGIN 5
33#define PANEL_WIDTH (ITEM_WIDTH + 2*ITEM_MARGIN)
34#define PANEL_HEIGHT(N) (RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT + (N)*(ITEM_HEIGHT + ITEM_MARGIN) + ITEM_MARGIN)
35
36#ifdef DARK
37#define BACKGROUND RGBA (0x000000FF)
38#else
39#define BACKGROUND RGBA (0xFFFFFFFF)
40#endif
41
42#define ARROW_THICKNESS 2.5
43#define ARROW_LENGTH 15
44#define ARROW_ANGLE 10
45
46#define CONTROL_POINT_RADIUS 5
47#define CONTROL_POINT_RADIUS_FOCUSED 10
48
49
50void
51draw_arrow (Vector2 beg, Vector2 end, float thick, Color c)
52{
53 Vector2 off = Vector2Scale (Vector2Normalize (Vector2Subtract (end, beg)),
54 ARROW_LENGTH);
55 Vector2 ahl = Vector2Add (end, Vector2Rotate (off, +ARROW_ANGLE));
56 Vector2 ahr = Vector2Add (end, Vector2Rotate (off, -ARROW_ANGLE));
57
58 DrawLineEx (beg, end, thick, c);
59 DrawLineEx (end, ahl, thick, c);
60 DrawLineEx (end, ahr, thick, c);
61
62 DrawCircleV (beg, thick/2., c);
63 DrawCircleV (end, thick/2., c);
64 DrawCircleV (ahl, thick/2., c);
65 DrawCircleV (ahr, thick/2., c);
66}
67
68Vector2
69sample_bezier7 (const bezier7 pts, float t)
70{
71 float u = 1.0f - t;
72
73 float t2 = t * t;
74 float t3 = t2 * t;
75 float t4 = t3 * t;
76 float t5 = t4 * t;
77 float t6 = t5 * t;
78 float t7 = t6 * t;
79
80 /* Precompute powers of u (1 - t) */
81 float u2 = u * u;
82 float u3 = u2 * u;
83 float u4 = u3 * u;
84 float u5 = u4 * u;
85 float u6 = u5 * u;
86 float u7 = u6 * u;
87
88 /* Bernstein basis polynomials for degree 7 */
89 float b0 = u7;
90 float b1 = 7.0f * u6 * t;
91 float b2 = 21.0f * u5 * t2;
92 float b3 = 35.0f * u4 * t3;
93 float b4 = 35.0f * u3 * t4;
94 float b5 = 21.0f * u2 * t5;
95 float b6 = 7.0f * u * t6;
96 float b7 = t7;
97
98 Vector2 result;
99
100 /* Calculate final x and y by blending control points with basis weights */
101 result.x = (b0 * pts[0].x) + (b1 * pts[1].x) + (b2 * pts[2].x) + (b3 * pts[3].x) +
102 (b4 * pts[4].x) + (b5 * pts[5].x) + (b6 * pts[6].x) + (b7 * pts[7].x);
103
104 result.y = (b0 * pts[0].y) + (b1 * pts[1].y) + (b2 * pts[2].y) + (b3 * pts[3].y) +
105 (b4 * pts[4].y) + (b5 * pts[5].y) + (b6 * pts[6].y) + (b7 * pts[7].y);
106
107 return result;
108}
109
110void
111draw_bezier7 (bezier7 cp)
112{
113 char text[] = "P0";
114 DrawLineStrip (cp, 8, BLUE);
115 for (int i = 0; i < 8; ++i) {
116 DrawCircleLinesV (cp[i], 4, BLUE);
117 text[1] = '0' + i;
118 DrawText (text, cp[i].x - 5, cp[i].y + 7, 14, BLACK);
119 }
120
121 for (float t = 0; t <= 1; t += 0.01) {
122 DrawCircleV (sample_bezier7 (cp, t), 2, BLACK);
123 }
124}
125
126int
127main (int argc, char *argv[])
128{
129 srand (time (NULL));
130
131 SetConfigFlags (FLAG_MSAA_4X_HINT);
132 SetConfigFlags (FLAG_WINDOW_RESIZABLE);
133 InitWindow (800, 450, "GA");
134 SetTargetFPS (60);
135
136 Camera2D camera = {
137 .zoom = 1.0f,
138 };
139
140 Vector2 screen = {
141 .x = GetScreenWidth (),
142 .y = GetScreenHeight (),
143 };
144
145 struct input in = {
146 .r_beg = { 100, 100 },
147 // .r_end = { 100, 100 },
148 .r_end = { 300, 300 },
149 .v_beg = { 100, 10 },
150 .v_end = { 100, 10 },
151 .v_max = 250,
152 .a_max = 500,
153 .j_max = 16,
154 };
155
156 bezier7 cp;
157
158 float arrow_thickness = ARROW_THICKNESS;
159 float spline_thickness = ARROW_THICKNESS;
160 bool move_r_beg = false;
161 bool move_r_end = false;
162 bool move_v_beg = false;
163 bool move_v_end = false;
164 bool recompute = true;
165 bool show_fps = true;
166 bool show_menu = true;
167 bool show_graph = true;
168 bool draw_cp = true;
169 bool animate_sample_offset = false;
170 float animation_speed = 20;
171 int panel_item = 0;
172 bool resizing_graph = false;
173 int graph_height = screen.y/3;
174 float T = 10;
175 while (!WindowShouldClose ()) {
176 screen = (Vector2) {
177 .x = GetScreenWidth (),
178 .y = GetScreenHeight (),
179 };
180 float t = GetTime ();
181 float dt = GetFrameTime ();
182
183 if (IsMouseButtonDown (MOUSE_BUTTON_RIGHT)) {
184 Vector2 delta = GetMouseDelta ();
185 delta = Vector2Scale (delta, -1.0f/camera.zoom);
186 camera.target = Vector2Add (camera.target, delta);
187 }
188
189 if (IsKeyPressed (KEY_F)) show_fps = !show_fps;
190
191 if (IsKeyDown (KEY_LEFT )) T -= .1;
192 if (IsKeyDown (KEY_RIGHT)) T += .1;
193
194
195 if (1 || recompute) {
196 // chromosome_to_control_points (cp, in, ch);
197 cp[0] = in.r_beg;
198 cp[1] = Vector2Add (in.r_beg, Vector2Scale (in.v_beg, +1*T/7));
199 cp[2] = Vector2Add (in.r_beg, Vector2Scale (in.v_beg, +2*T/7));
200 cp[3] = Vector2Add (in.r_beg, Vector2Scale (in.v_beg, +3*T/7));
201 cp[4] = Vector2Add (in.r_end, Vector2Scale (in.v_end, -3*T/7));
202 cp[5] = Vector2Add (in.r_end, Vector2Scale (in.v_end, -2*T/7));
203 cp[6] = Vector2Add (in.r_end, Vector2Scale (in.v_end, -1*T/7));
204 cp[7] = in.r_end;
205 recompute = false;
206 }
207
208 float wheel = GetMouseWheelMove();
209 if (wheel != 0) {
210 Vector2 offset = GetMousePosition ();
211 camera.target = GetScreenToWorld2D (offset, camera);
212 camera.offset = offset;
213 camera.zoom = Clamp (expf (logf (camera.zoom) + .2*wheel), .125, 64);
214 }
215
216 Vector2 mouse_hud = GetMousePosition ();
217 Vector2 mouse = GetScreenToWorld2D (mouse_hud, camera);
218
219 BeginDrawing ();
220 ClearBackground (BACKGROUND);
221 BeginMode2D (camera);
222
223 if (CheckCollisionPointCircle (mouse, in.r_beg, 10)) {
224 DrawCircleLinesV (in.r_beg, 10, BLUE);
225 if (!move_v_beg && IsMouseButtonDown (MOUSE_BUTTON_LEFT))
226 move_r_beg = true;
227 } else if (CheckCollisionPointCircle (mouse, in.r_end, 10)) {
228 DrawCircleLinesV (in.r_end, 10, BLUE);
229 if (!move_v_end && IsMouseButtonDown (MOUSE_BUTTON_LEFT))
230 move_r_end = true;
231 } else if (CheckCollisionPointCircle (mouse, Vector2Add (in.r_beg, in.v_beg), 10)) {
232 DrawCircleLinesV (Vector2Add (in.r_beg, in.v_beg), 10, BLUE);
233 if (!move_r_beg && IsMouseButtonDown (MOUSE_BUTTON_LEFT))
234 move_v_beg = true;
235 } else if (CheckCollisionPointCircle (mouse, Vector2Add (in.r_end, in.v_end), 10)) {
236 DrawCircleLinesV (Vector2Add (in.r_end, in.v_end), 10, BLUE);
237 if (!move_r_end && IsMouseButtonDown (MOUSE_BUTTON_LEFT))
238 move_v_end = true;
239 }
240
241 if (move_r_beg) {
242 in.r_beg = mouse;
243 if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT))
244 move_r_beg = false;
245 }
246 if (move_r_end) {
247 in.r_end = mouse;
248 if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT))
249 move_r_end = false;
250 }
251 if (move_v_beg) {
252 in.v_beg = Vector2Subtract (mouse, in.r_beg);
253 if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT))
254 move_v_beg = false;
255 }
256 if (move_v_end) {
257 in.v_end = Vector2Subtract (mouse, in.r_end);
258 if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT))
259 move_v_end = false;
260 }
261
262 draw_bezier7 (cp);
263 draw_arrow (in.r_beg, Vector2Add (in.r_beg, in.v_beg), arrow_thickness, GREEN);
264 draw_arrow (in.r_end, Vector2Add (in.r_end, in.v_end), arrow_thickness, RED);
265
266 EndMode2D ();
267
268 if (move_r_beg ||
269 move_r_end ||
270 move_v_beg ||
271 move_v_end) {
272 recompute = true;
273 }
274
275 if (show_fps)
276 DrawFPS (screen.x - 75, 0);
277 EndDrawing ();
278 }
279
280 CloseWindow ();
281
282 return EXIT_SUCCESS;
283}