| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
#include <assert.h> |
| 6 |
#include <stdbool.h> |
| 7 |
|
| 8 |
enum curve_type { |
| 9 |
CURVE_LINEAR, |
| 10 |
CURVE_CUBIC_BEZIER, |
| 11 |
CURVE_STEP, |
| 12 |
CURVE_INVALID, |
| 13 |
}; |
| 14 |
|
| 15 |
struct curve { |
| 16 |
enum curve_type type; |
| 17 |
union { |
| 18 |
struct curve_cubic_bezier { |
| 19 |
double ax, bx, cx; |
| 20 |
double ay, by, cy; |
| 21 |
} bezier; |
| 22 |
struct curve_step { |
| 23 |
int steps; |
| 24 |
bool jump_start, jump_end; |
| 25 |
} step; |
| 26 |
}; |
| 27 |
}; |
| 28 |
|
| 29 |
static const struct curve CURVE_LINEAR_INIT = {.type = CURVE_LINEAR}; |
| 30 |
static const struct curve CURVE_INVALID_INIT = {.type = CURVE_INVALID}; |
| 31 |
|
| 32 |
static inline struct curve curve_new_cubic_bezier(double x1, double y1, double x2, double y2) { |
| 33 |
double cx = 3. * x1; |
| 34 |
double bx = 3. * (x2 - x1) - cx; |
| 35 |
double cy = 3. * y1; |
| 36 |
double by = 3. * (y2 - y1) - cy; |
| 37 |
return (struct curve){ |
| 38 |
.type = CURVE_CUBIC_BEZIER, |
| 39 |
.bezier = {.ax = 1. - cx - bx, .bx = bx, .cx = cx, .ay = 1. - cy - by, .by = by, .cy = cy}, |
| 40 |
}; |
| 41 |
} |
| 42 |
static inline struct curve curve_new_step(int steps, bool jump_start, bool jump_end) { |
| 43 |
assert(steps > 0); |
| 44 |
return (struct curve){ |
| 45 |
.type = CURVE_STEP, |
| 46 |
.step = {.steps = steps, .jump_start = jump_start, .jump_end = jump_end}, |
| 47 |
}; |
| 48 |
} |
| 49 |
struct curve curve_parse(const char *str, const char **end, char **err); |
| 50 |
|
| 51 |
double curve_sample(const struct curve *curve, double progress); |
| 52 |
char *curve_to_c(const struct curve *curve); |