Math c library & fixes for graphics
This commit is contained in:
parent
1aa082a156
commit
7a60d9d0bd
|
|
@ -25,12 +25,12 @@ typedef struct {
|
||||||
image_data_t image_create(uint16_t width, uint16_t height);
|
image_data_t image_create(uint16_t width, uint16_t height);
|
||||||
void image_free(image_data_t image);
|
void image_free(image_data_t image);
|
||||||
|
|
||||||
void image_draw_point(image_data_t image, uint16_t x, uint16_t y, image_color_t color);
|
void image_draw_point(image_data_t image, int16_t x, int16_t y, image_color_t color);
|
||||||
void image_draw_hline(image_data_t image, uint16_t x1, uint16_t x2, uint16_t y, image_color_t color);
|
void image_draw_hline(image_data_t image, int16_t x1, int16_t x2, int16_t y, image_color_t color);
|
||||||
void image_draw_vline(image_data_t image, uint16_t x, uint16_t y1, uint16_t y2, image_color_t color);
|
void image_draw_vline(image_data_t image, int16_t x, int16_t y1, int16_t y2, image_color_t color);
|
||||||
void image_draw_line(image_data_t image, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, image_color_t color);
|
void image_draw_line(image_data_t image, int16_t x1, int16_t y1, int16_t x2, int16_t y2, image_color_t color);
|
||||||
void image_draw_rect(image_data_t image, uint16_t x, uint16_t y, uint16_t w, uint16_t h, image_color_t color);
|
void image_draw_rect(image_data_t image, int16_t x, int16_t y, uint16_t w, uint16_t h, image_color_t color);
|
||||||
void image_fill_rect(image_data_t image, uint16_t x, uint16_t y, uint16_t w, uint16_t h, image_color_t color);
|
void image_fill_rect(image_data_t image, int16_t x, int16_t y, uint16_t w, uint16_t h, image_color_t color);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,35 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define M_PI 3.14159265359
|
||||||
|
#define M_PI_2 1.57079632679
|
||||||
|
|
||||||
#define abs(x) (((x) < 0) ? -(x) : (x))
|
#define abs(x) (((x) < 0) ? -(x) : (x))
|
||||||
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
||||||
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static inline bool isnan(double x) {
|
||||||
|
return x != x;
|
||||||
|
}
|
||||||
|
static inline bool isinf(double x) {
|
||||||
|
return !isnan(x) && isnan(x - x);
|
||||||
|
}
|
||||||
|
|
||||||
|
float sinf(float x);
|
||||||
|
static inline float cosf(float x) {
|
||||||
|
return sinf(x + M_PI_2);
|
||||||
|
}
|
||||||
|
static inline float tanf(float x) {
|
||||||
|
return sinf(x) / cosf(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
float fmodf(float x, float y);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,11 @@ extern "C" {
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#define RAND_MAX INT_MAX
|
||||||
|
|
||||||
extern unsigned char __heap_base;
|
extern unsigned char __heap_base;
|
||||||
|
|
||||||
#define IMPORT(name) __attribute__((import_module("env"), import_name(#name)))
|
#define IMPORT(name) __attribute__((import_module("env"), import_name(#name)))
|
||||||
|
|
@ -23,7 +27,7 @@ int memcmp(const void* s1, const void* s2, size_t n);
|
||||||
IMPORT(log) void printf(const char* format, ...);
|
IMPORT(log) void printf(const char* format, ...);
|
||||||
|
|
||||||
EXPORT(__srand) void srand(uint64_t seed);
|
EXPORT(__srand) void srand(uint64_t seed);
|
||||||
uint64_t rand(void);
|
int rand(void);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,46 +24,54 @@ static inline void set_point(image_data_t image, uint16_t x, uint16_t y, image_c
|
||||||
image.pixels[x + y * image.width] = color;
|
image.pixels[x + y * image.width] = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void image_draw_point(image_data_t image, uint16_t x, uint16_t y, image_color_t color) {
|
void image_draw_point(image_data_t image, int16_t x, int16_t y, image_color_t color) {
|
||||||
if (x >= image.width || y >= image.height) {
|
if (x >= image.width || y >= image.height || x < 0 || y < 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
set_point(image, x, y, color);
|
set_point(image, x, y, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
void image_draw_hline(image_data_t image, uint16_t x1, uint16_t x2, uint16_t y, image_color_t color) {
|
void image_draw_hline(image_data_t image, int16_t x1, int16_t x2, int16_t y, image_color_t color) {
|
||||||
|
if (y < 0 || y >= image.height) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (x1 > x2) {
|
if (x1 > x2) {
|
||||||
uint16_t temp = x1;
|
uint16_t temp = x1;
|
||||||
x1 = x2;
|
x1 = x2;
|
||||||
x2 = temp;
|
x2 = temp;
|
||||||
}
|
}
|
||||||
if (x1 > image.width) {
|
if (x1 > image.width || x2 < 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
x1 = max(0, x1);
|
||||||
x2 = min(x2, image.width);
|
x2 = min(x2, image.width);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
image_draw_point(image, x1++, y, color);
|
set_point(image, x1++, y, color);
|
||||||
} while (x1 < x2);
|
} while (x1 < x2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void image_draw_vline(image_data_t image, uint16_t x, uint16_t y1, uint16_t y2, image_color_t color) {
|
void image_draw_vline(image_data_t image, int16_t x, int16_t y1, int16_t y2, image_color_t color) {
|
||||||
|
if (x < 0 || x >= image.width) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (y1 > y2) {
|
if (y1 > y2) {
|
||||||
uint16_t temp = y1;
|
uint16_t temp = y1;
|
||||||
y1 = y2;
|
y1 = y2;
|
||||||
y2 = temp;
|
y2 = temp;
|
||||||
}
|
}
|
||||||
if (y1 > image.width) {
|
if (y1 > image.width || y2 < 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
y1 = max(0, y1);
|
||||||
y2 = min(y2, image.height);
|
y2 = min(y2, image.height);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
image_draw_point(image, x, y1++, color);
|
set_point(image, x, y1++, color);
|
||||||
} while (y1 < y2);
|
} while (y1 < y2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void image_draw_line(image_data_t image, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, image_color_t color) {
|
void image_draw_line(image_data_t image, int16_t x1, int16_t y1, int16_t x2, int16_t y2, image_color_t color) {
|
||||||
int dx = abs(x2 - x1);
|
int dx = abs(x2 - x1);
|
||||||
int dy = abs(y2 - y1);
|
int dy = abs(y2 - y1);
|
||||||
int sx = (x1 < x2) ? 1 : -1;
|
int sx = (x1 < x2) ? 1 : -1;
|
||||||
|
|
@ -72,10 +80,13 @@ void image_draw_line(image_data_t image, uint16_t x1, uint16_t y1, uint16_t x2,
|
||||||
int e2;
|
int e2;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (x1 >= image.width || y1 >= image.height) {
|
if ((x1 >= image.width && x2 >= image.width) || (y1 >= image.height && y2 >= image.height)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
set_point(image, x1, y1, color);
|
if ((x1 < 0 && x2 < 0) || (y1 < 0 && y2 < 0)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
image_draw_point(image, x1, y1, color);
|
||||||
if (x1 == x2 && y1 == y2) {
|
if (x1 == x2 && y1 == y2) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -91,7 +102,14 @@ void image_draw_line(image_data_t image, uint16_t x1, uint16_t y1, uint16_t x2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void image_draw_rect(image_data_t image, uint16_t x, uint16_t y, uint16_t w, uint16_t h, image_color_t color) {
|
void image_draw_rect(image_data_t image, int16_t x, int16_t y, uint16_t w, uint16_t h, image_color_t color) {
|
||||||
|
if (w == 0 || h == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
w = min(image.width - x, w);
|
||||||
|
h = min(image.height - y, h);
|
||||||
|
|
||||||
image_draw_hline(image, x, x + w - 1, y, color);
|
image_draw_hline(image, x, x + w - 1, y, color);
|
||||||
image_draw_hline(image, x, x + w, y + h - 1, color);
|
image_draw_hline(image, x, x + w, y + h - 1, color);
|
||||||
|
|
||||||
|
|
@ -99,7 +117,15 @@ void image_draw_rect(image_data_t image, uint16_t x, uint16_t y, uint16_t w, uin
|
||||||
image_draw_vline(image, x + w - 1, y, y + h - 1, color);
|
image_draw_vline(image, x + w - 1, y, y + h - 1, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
void image_fill_rect(image_data_t image, uint16_t x, uint16_t y, uint16_t w, uint16_t h, image_color_t color) {
|
void image_fill_rect(image_data_t image, int16_t x, int16_t y, uint16_t w, uint16_t h, image_color_t color) {
|
||||||
|
if (w == 0 || h == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
x = max(0, x);
|
||||||
|
y = max(0, y);
|
||||||
|
w = min(image.width - x, w);
|
||||||
|
h = min(image.height - y, h);
|
||||||
|
|
||||||
while (h--) {
|
while (h--) {
|
||||||
image_draw_hline(image, x, x + w, y++, color);
|
image_draw_hline(image, x, x + w, y++, color);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,327 @@
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
float sinf(float x) {
|
||||||
|
static const float lookup_table[256] = {
|
||||||
|
0.0f, // 0
|
||||||
|
0.0061599463f, // 1
|
||||||
|
0.012319659f, // 2
|
||||||
|
0.018478904f, // 3
|
||||||
|
0.024637448f, // 4
|
||||||
|
0.030795058f, // 5
|
||||||
|
0.036951497f, // 6
|
||||||
|
0.043106537f, // 7
|
||||||
|
0.049259938f, // 8
|
||||||
|
0.055411477f, // 9
|
||||||
|
0.061560903f, // 10
|
||||||
|
0.067708001f, // 11
|
||||||
|
0.073852524f, // 12
|
||||||
|
0.079994254f, // 13
|
||||||
|
0.086132944f, // 14
|
||||||
|
0.092268363f, // 15
|
||||||
|
0.098400272f, // 16
|
||||||
|
0.10452846f, // 17
|
||||||
|
0.11065269f, // 18
|
||||||
|
0.1167727f, // 19
|
||||||
|
0.12288829f, // 20
|
||||||
|
0.12899922f, // 21
|
||||||
|
0.13510525f, // 22
|
||||||
|
0.14120616f, // 23
|
||||||
|
0.14730169f, // 24
|
||||||
|
0.15339166f, // 25
|
||||||
|
0.1594758f, // 26
|
||||||
|
0.16555387f, // 27
|
||||||
|
0.17162569f, // 28
|
||||||
|
0.17769097f, // 29
|
||||||
|
0.18374951f, // 30
|
||||||
|
0.1898011f, // 31
|
||||||
|
0.19584547f, // 32
|
||||||
|
0.20188241f, // 33
|
||||||
|
0.2079117f, // 34
|
||||||
|
0.21393308f, // 35
|
||||||
|
0.21994635f, // 36
|
||||||
|
0.2259513f, // 37
|
||||||
|
0.23194765f, // 38
|
||||||
|
0.2379352f, // 39
|
||||||
|
0.24391371f, // 40
|
||||||
|
0.24988301f, // 41
|
||||||
|
0.25584278f, // 42
|
||||||
|
0.26179287f, // 43
|
||||||
|
0.26773301f, // 44
|
||||||
|
0.27366298f, // 45
|
||||||
|
0.27958259f, // 46
|
||||||
|
0.28549159f, // 47
|
||||||
|
0.29138973f, // 48
|
||||||
|
0.29727685f, // 49
|
||||||
|
0.30315268f, // 50
|
||||||
|
0.309017f, // 51
|
||||||
|
0.31486961f, // 52
|
||||||
|
0.32071021f, // 53
|
||||||
|
0.32653871f, // 54
|
||||||
|
0.33235481f, // 55
|
||||||
|
0.33815828f, // 56
|
||||||
|
0.34394893f, // 57
|
||||||
|
0.3497265f, // 58
|
||||||
|
0.35549083f, // 59
|
||||||
|
0.36124167f, // 60
|
||||||
|
0.36697879f, // 61
|
||||||
|
0.372702f, // 62
|
||||||
|
0.37841105f, // 63
|
||||||
|
0.38410574f, // 64
|
||||||
|
0.38978586f, // 65
|
||||||
|
0.39545122f, // 66
|
||||||
|
0.40110153f, // 67
|
||||||
|
0.40673664f, // 68
|
||||||
|
0.41235632f, // 69
|
||||||
|
0.41796035f, // 70
|
||||||
|
0.42354852f, // 71
|
||||||
|
0.4291206f, // 72
|
||||||
|
0.43467644f, // 73
|
||||||
|
0.44021577f, // 74
|
||||||
|
0.44573835f, // 75
|
||||||
|
0.45124406f, // 76
|
||||||
|
0.45673263f, // 77
|
||||||
|
0.46220389f, // 78
|
||||||
|
0.4676576f, // 79
|
||||||
|
0.47309354f, // 80
|
||||||
|
0.47851157f, // 81
|
||||||
|
0.48391145f, // 82
|
||||||
|
0.48929292f, // 83
|
||||||
|
0.49465582f, // 84
|
||||||
|
0.5f, // 85
|
||||||
|
0.5053252f, // 86
|
||||||
|
0.5106312f, // 87
|
||||||
|
0.51591784f, // 88
|
||||||
|
0.52118486f, // 89
|
||||||
|
0.52643216f, // 90
|
||||||
|
0.53165948f, // 91
|
||||||
|
0.53686661f, // 92
|
||||||
|
0.54205334f, // 93
|
||||||
|
0.54721951f, // 94
|
||||||
|
0.55236501f, // 95
|
||||||
|
0.55748945f, // 96
|
||||||
|
0.56259274f, // 97
|
||||||
|
0.5676747f, // 98
|
||||||
|
0.57273513f, // 99
|
||||||
|
0.57777381f, // 100
|
||||||
|
0.58279061f, // 101
|
||||||
|
0.58778524f, // 102
|
||||||
|
0.59275758f, // 103
|
||||||
|
0.59770751f, // 104
|
||||||
|
0.60263467f, // 105
|
||||||
|
0.60753894f, // 106
|
||||||
|
0.6124202f, // 107
|
||||||
|
0.61727822f, // 108
|
||||||
|
0.62211281f, // 109
|
||||||
|
0.6269238f, // 110
|
||||||
|
0.63171101f, // 111
|
||||||
|
0.63647425f, // 112
|
||||||
|
0.6412133f, // 113
|
||||||
|
0.64592808f, // 114
|
||||||
|
0.65061831f, // 115
|
||||||
|
0.65528381f, // 116
|
||||||
|
0.65992457f, // 117
|
||||||
|
0.66454017f, // 118
|
||||||
|
0.66913062f, // 119
|
||||||
|
0.67369562f, // 120
|
||||||
|
0.67823511f, // 121
|
||||||
|
0.68274885f, // 122
|
||||||
|
0.68723667f, // 123
|
||||||
|
0.69169843f, // 124
|
||||||
|
0.69613391f, // 125
|
||||||
|
0.70054305f, // 126
|
||||||
|
0.70492554f, // 127
|
||||||
|
0.70928127f, // 128
|
||||||
|
0.71361017f, // 129
|
||||||
|
0.7179119f, // 130
|
||||||
|
0.72218645f, // 131
|
||||||
|
0.72643358f, // 132
|
||||||
|
0.73065311f, // 133
|
||||||
|
0.73484498f, // 134
|
||||||
|
0.7390089f, // 135
|
||||||
|
0.74314487f, // 136
|
||||||
|
0.74725252f, // 137
|
||||||
|
0.75133187f, // 138
|
||||||
|
0.75538272f, // 139
|
||||||
|
0.7594049f, // 140
|
||||||
|
0.76339829f, // 141
|
||||||
|
0.76736265f, // 142
|
||||||
|
0.77129793f, // 143
|
||||||
|
0.775204f, // 144
|
||||||
|
0.77908057f, // 145
|
||||||
|
0.78292763f, // 146
|
||||||
|
0.78674495f, // 147
|
||||||
|
0.79053241f, // 148
|
||||||
|
0.79428989f, // 149
|
||||||
|
0.7980172f, // 150
|
||||||
|
0.8017143f, // 151
|
||||||
|
0.80538094f, // 152
|
||||||
|
0.809017f, // 153
|
||||||
|
0.81262237f, // 154
|
||||||
|
0.81619692f, // 155
|
||||||
|
0.81974047f, // 156
|
||||||
|
0.82325292f, // 157
|
||||||
|
0.82673419f, // 158
|
||||||
|
0.83018404f, // 159
|
||||||
|
0.83360237f, // 160
|
||||||
|
0.8369891f, // 161
|
||||||
|
0.84034407f, // 162
|
||||||
|
0.84366715f, // 163
|
||||||
|
0.84695822f, // 164
|
||||||
|
0.8502171f, // 165
|
||||||
|
0.8534438f, // 166
|
||||||
|
0.85663807f, // 167
|
||||||
|
0.8597998f, // 168
|
||||||
|
0.86292899f, // 169
|
||||||
|
0.86602545f, // 170
|
||||||
|
0.86908895f, // 171
|
||||||
|
0.87211949f, // 172
|
||||||
|
0.875117f, // 173
|
||||||
|
0.87808126f, // 174
|
||||||
|
0.8810122f, // 175
|
||||||
|
0.8839097f, // 176
|
||||||
|
0.88677371f, // 177
|
||||||
|
0.88960397f, // 178
|
||||||
|
0.89240056f, // 179
|
||||||
|
0.8951633f, // 180
|
||||||
|
0.89789206f, // 181
|
||||||
|
0.90058666f, // 182
|
||||||
|
0.90324718f, // 183
|
||||||
|
0.90587342f, // 184
|
||||||
|
0.90846527f, // 185
|
||||||
|
0.91102266f, // 186
|
||||||
|
0.91354549f, // 187
|
||||||
|
0.91603357f, // 188
|
||||||
|
0.91848695f, // 189
|
||||||
|
0.92090553f, // 190
|
||||||
|
0.92328912f, // 191
|
||||||
|
0.92563766f, // 192
|
||||||
|
0.9279511f, // 193
|
||||||
|
0.93022931f, // 194
|
||||||
|
0.93247223f, // 195
|
||||||
|
0.93467975f, // 196
|
||||||
|
0.93685186f, // 197
|
||||||
|
0.93898833f, // 198
|
||||||
|
0.94108927f, // 199
|
||||||
|
0.94315445f, // 200
|
||||||
|
0.94518387f, // 201
|
||||||
|
0.94717735f, // 202
|
||||||
|
0.94913495f, // 203
|
||||||
|
0.95105654f, // 204
|
||||||
|
0.95294201f, // 205
|
||||||
|
0.95479131f, // 206
|
||||||
|
0.95660442f, // 207
|
||||||
|
0.95838124f, // 208
|
||||||
|
0.96012163f, // 209
|
||||||
|
0.96182567f, // 210
|
||||||
|
0.96349317f, // 211
|
||||||
|
0.96512407f, // 212
|
||||||
|
0.96671838f, // 213
|
||||||
|
0.96827602f, // 214
|
||||||
|
0.9697969f, // 215
|
||||||
|
0.97128105f, // 216
|
||||||
|
0.97272825f, // 217
|
||||||
|
0.97413862f, // 218
|
||||||
|
0.97551197f, // 219
|
||||||
|
0.9768483f, // 220
|
||||||
|
0.97814763f, // 221
|
||||||
|
0.97940975f, // 222
|
||||||
|
0.98063475f, // 223
|
||||||
|
0.98182255f, // 224
|
||||||
|
0.9829731f, // 225
|
||||||
|
0.98408633f, // 226
|
||||||
|
0.98516226f, // 227
|
||||||
|
0.98620075f, // 228
|
||||||
|
0.98720181f, // 229
|
||||||
|
0.9881655f, // 230
|
||||||
|
0.98909163f, // 231
|
||||||
|
0.98998022f, // 232
|
||||||
|
0.99083126f, // 233
|
||||||
|
0.99164468f, // 234
|
||||||
|
0.99242049f, // 235
|
||||||
|
0.99315864f, // 236
|
||||||
|
0.99385911f, // 237
|
||||||
|
0.99452192f, // 238
|
||||||
|
0.99514693f, // 239
|
||||||
|
0.99573416f, // 240
|
||||||
|
0.99628365f, // 241
|
||||||
|
0.9967953f, // 242
|
||||||
|
0.99726915f, // 243
|
||||||
|
0.99770516f, // 244
|
||||||
|
0.99810332f, // 245
|
||||||
|
0.99846363f, // 246
|
||||||
|
0.99878597f, // 247
|
||||||
|
0.99907047f, // 248
|
||||||
|
0.99931705f, // 249
|
||||||
|
0.99952573f, // 250
|
||||||
|
0.99969643f, // 251
|
||||||
|
0.99982923f, // 252
|
||||||
|
0.99992412f, // 253
|
||||||
|
0.99998105f, // 254
|
||||||
|
1.0f // 255
|
||||||
|
};
|
||||||
|
|
||||||
|
x = fmodf(x, 2.0f * (float)M_PI);
|
||||||
|
if (x < 0) x += 2.0f * (float)M_PI;
|
||||||
|
|
||||||
|
float reference_angle;
|
||||||
|
int sign = 1;
|
||||||
|
|
||||||
|
if (x <= (float)M_PI_2) {
|
||||||
|
reference_angle = x;
|
||||||
|
} else if (x <= (float)M_PI) {
|
||||||
|
reference_angle = (float)M_PI - x;
|
||||||
|
} else if (x <= 3.0f * (float)M_PI_2) {
|
||||||
|
reference_angle = x - (float)M_PI;
|
||||||
|
sign = -1;
|
||||||
|
} else {
|
||||||
|
reference_angle = 2.0f * (float)M_PI - x;
|
||||||
|
sign = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
float position = reference_angle * (255.0f / ((float)M_PI / 2.0f));
|
||||||
|
int i = (int)position;
|
||||||
|
float interpolated;
|
||||||
|
|
||||||
|
if (i >= 255) {
|
||||||
|
interpolated = lookup_table[255];
|
||||||
|
} else {
|
||||||
|
float fractional = position - i;
|
||||||
|
interpolated = lookup_table[i] + fractional * (lookup_table[i + 1] - lookup_table[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sign * interpolated;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float fmodf(float x, float y) {
|
||||||
|
if (y == 0.0 || isnan(x) || isnan(y)) {
|
||||||
|
return (0.0 / 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool neg = x < 0.0;
|
||||||
|
float ax = neg ? -x : x;
|
||||||
|
float ay = (y < 0.0) ? -y : y;
|
||||||
|
|
||||||
|
if (ax < ay) {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
float tmp = ay;
|
||||||
|
while (tmp <= ax) {
|
||||||
|
tmp *= 2.0f;
|
||||||
|
}
|
||||||
|
if (isinf(tmp)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (tmp > ay) {
|
||||||
|
tmp *= 0.5f;
|
||||||
|
if (ax >= tmp) {
|
||||||
|
ax -= tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return neg ? -ax : ax;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -122,12 +122,14 @@ void* realloc(void* block, size_t size) {
|
||||||
header_t* header;
|
header_t* header;
|
||||||
void* ret;
|
void* ret;
|
||||||
|
|
||||||
if (!block || !size)
|
if (!block || !size) {
|
||||||
return malloc(size);
|
return malloc(size);
|
||||||
|
}
|
||||||
|
|
||||||
header = (header_t*)block - 1;
|
header = (header_t*)block - 1;
|
||||||
if (header->size >= size)
|
if (header->size >= size) {
|
||||||
return block;
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
ret = malloc(size);
|
ret = malloc(size);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
|
|
@ -171,11 +173,14 @@ int memcmp(const void* s1, const void* s2, size_t n) {
|
||||||
|
|
||||||
static uint64_t rand_state;
|
static uint64_t rand_state;
|
||||||
|
|
||||||
void srand(uint64_t seed) { rand_state = seed; }
|
void srand(uint64_t seed) {
|
||||||
|
rand_state = seed;
|
||||||
|
}
|
||||||
|
|
||||||
uint64_t rand(void) {
|
int rand(void) {
|
||||||
uint64_t z = (rand_state += 0x9e3779b97f4a7c15);
|
uint64_t z = (rand_state += 0x9e3779b97f4a7c15);
|
||||||
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
|
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
|
||||||
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
|
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
|
||||||
return z ^ (z >> 31);
|
int r = (int)(z ^ (z >> 31));
|
||||||
|
return r < 0 ? -r : r;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,228 @@
|
||||||
#include <graphics.h>
|
#include <graphics.h>
|
||||||
|
#include <math.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static image_data_t image;
|
static image_data_t image;
|
||||||
|
static image_color_t color;
|
||||||
|
static uint16_t current_x;
|
||||||
|
static uint16_t current_y;
|
||||||
|
|
||||||
EXPORT(step) void step() {
|
static void _moveto(uint16_t x, uint16_t y) {
|
||||||
// for (int i = 0; i < 1; i++) {
|
current_x = x;
|
||||||
// int x1 = rand() % image.width;
|
current_y = y;
|
||||||
// int y1 = rand() % image.height;
|
|
||||||
// int x2 = rand() % image.width;
|
|
||||||
// int y2 = rand() % image.height;
|
|
||||||
|
|
||||||
// image_fill_rect(image, x1, y1, x2 - x1, y2 - y1, {(uint32_t)(rand() | 0xFF000000)});
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void _lineto(uint16_t x, uint16_t y) {
|
||||||
|
image_draw_line(image, current_x, current_y, x, y, color);
|
||||||
|
_moveto(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
// D E F I N E S ///////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#define NUM_ASTEROIDS 10
|
||||||
|
#define ERASE 0
|
||||||
|
#define DRAW 1
|
||||||
|
|
||||||
|
// T Y P E D E F S ///////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// the structure for a vertex
|
||||||
|
|
||||||
|
typedef struct vertex_typ {
|
||||||
|
float x, y; // a single point in the 2-D plane.
|
||||||
|
} vertex, *vertex_ptr;
|
||||||
|
|
||||||
|
// the structure for an object
|
||||||
|
|
||||||
|
typedef struct object_typ {
|
||||||
|
int num_vertices; // number of vertices in this object
|
||||||
|
uint32_t color; // color of object
|
||||||
|
float xo, yo; // position of object
|
||||||
|
float x_velocity; // x velocity of object
|
||||||
|
float y_velocity; // y velocity of object
|
||||||
|
float scale; // scale factor
|
||||||
|
float angle; // rotation rate
|
||||||
|
vertex vertices[16]; // 16 vertices
|
||||||
|
} object, *object_ptr;
|
||||||
|
|
||||||
|
object asteroids[NUM_ASTEROIDS];
|
||||||
|
|
||||||
|
void Scale_Object(object_ptr object, float scale) {
|
||||||
|
int index;
|
||||||
|
|
||||||
|
// for all vertices scale the x and y component
|
||||||
|
|
||||||
|
for (index = 0; index < object->num_vertices; index++) {
|
||||||
|
object->vertices[index].x *= scale;
|
||||||
|
object->vertices[index].y *= scale;
|
||||||
|
} // end for index
|
||||||
|
|
||||||
|
} // end Scale_Object
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void Rotate_Object(object_ptr object, float angle) {
|
||||||
|
int index;
|
||||||
|
float x_new, y_new, cs, sn;
|
||||||
|
|
||||||
|
// pre-compute sin and cos
|
||||||
|
cs = cosf(angle);
|
||||||
|
sn = sinf(angle);
|
||||||
|
|
||||||
|
// for each vertex rotate it by angle
|
||||||
|
for (index = 0; index < object->num_vertices; index++) {
|
||||||
|
// rotate the vertex
|
||||||
|
x_new = object->vertices[index].x * cs - object->vertices[index].y * sn;
|
||||||
|
y_new = object->vertices[index].y * cs + object->vertices[index].x * sn;
|
||||||
|
|
||||||
|
// store the rotated vertex back into structure
|
||||||
|
object->vertices[index].x = x_new;
|
||||||
|
object->vertices[index].y = y_new;
|
||||||
|
|
||||||
|
} // end for index
|
||||||
|
|
||||||
|
} // end Rotate_Object
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void Create_Field(void) {
|
||||||
|
|
||||||
|
int index;
|
||||||
|
|
||||||
|
for (index = 0; index < NUM_ASTEROIDS; index++) {
|
||||||
|
|
||||||
|
// fill in the fields
|
||||||
|
|
||||||
|
asteroids[index].num_vertices = 6;
|
||||||
|
asteroids[index].color = 0xFF000000 | (rand());
|
||||||
|
asteroids[index].xo = 41 + rand() % (image.width - 82);
|
||||||
|
asteroids[index].yo = 41 + rand() % (image.height - 82);
|
||||||
|
|
||||||
|
asteroids[index].x_velocity = -3 + rand() % 6;
|
||||||
|
asteroids[index].y_velocity = -3 + rand() % 6;
|
||||||
|
asteroids[index].scale = (float)(rand() % 30) / 10;
|
||||||
|
asteroids[index].angle = (float)(-5 + (float)(rand() % 10)) / 100;
|
||||||
|
|
||||||
|
printf("xo=%f, yo=%f, xv=%f, yv=%f, r=%d\n", asteroids[index].xo, asteroids[index].yo, asteroids[index].x_velocity, asteroids[index].y_velocity, rand() % 6);
|
||||||
|
|
||||||
|
asteroids[index].vertices[0].x = 4.0;
|
||||||
|
asteroids[index].vertices[0].y = 3.5;
|
||||||
|
asteroids[index].vertices[1].x = 8.5;
|
||||||
|
asteroids[index].vertices[1].y = -3.0;
|
||||||
|
asteroids[index].vertices[2].x = 6;
|
||||||
|
asteroids[index].vertices[2].y = -5;
|
||||||
|
asteroids[index].vertices[3].x = 2;
|
||||||
|
asteroids[index].vertices[3].y = -3;
|
||||||
|
asteroids[index].vertices[4].x = -4;
|
||||||
|
asteroids[index].vertices[4].y = -6;
|
||||||
|
asteroids[index].vertices[5].x = -3.5;
|
||||||
|
asteroids[index].vertices[5].y = 5.5;
|
||||||
|
|
||||||
|
// now scale the asteroid to proper size
|
||||||
|
|
||||||
|
Scale_Object((object_ptr)&asteroids[index], asteroids[index].scale);
|
||||||
|
|
||||||
|
} // end for index
|
||||||
|
|
||||||
|
} // end Create_Field
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void Draw_Asteroids(int erase) {
|
||||||
|
int index, vertex;
|
||||||
|
float xo, yo;
|
||||||
|
|
||||||
|
for (index = 0; index < NUM_ASTEROIDS; index++) {
|
||||||
|
|
||||||
|
// draw the asteroid
|
||||||
|
|
||||||
|
if (erase == ERASE) {
|
||||||
|
color.color = 0xFF000000;
|
||||||
|
} else {
|
||||||
|
color.color = asteroids[index].color;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get position of object
|
||||||
|
xo = asteroids[index].xo;
|
||||||
|
yo = asteroids[index].yo;
|
||||||
|
|
||||||
|
// moveto first vertex
|
||||||
|
|
||||||
|
_moveto((int)(xo + asteroids[index].vertices[0].x), (int)(yo + asteroids[index].vertices[0].y));
|
||||||
|
|
||||||
|
for (vertex = 1; vertex < asteroids[index].num_vertices; vertex++) {
|
||||||
|
_lineto((int)(xo + asteroids[index].vertices[vertex].x), (int)(yo + asteroids[index].vertices[vertex].y));
|
||||||
|
|
||||||
|
} // end for vertex
|
||||||
|
|
||||||
|
// close object
|
||||||
|
|
||||||
|
_lineto((int)(xo + asteroids[index].vertices[0].x), (int)(yo + asteroids[index].vertices[0].y));
|
||||||
|
|
||||||
|
} // end for index
|
||||||
|
|
||||||
|
} // end Draw_Asteroids
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void Translate_Asteroids() {
|
||||||
|
|
||||||
|
int index;
|
||||||
|
|
||||||
|
for (index = 0; index < NUM_ASTEROIDS; index++) {
|
||||||
|
// translate current asteroid
|
||||||
|
|
||||||
|
asteroids[index].xo += asteroids[index].x_velocity;
|
||||||
|
asteroids[index].yo += asteroids[index].y_velocity;
|
||||||
|
// collision detection i.e. bounds check
|
||||||
|
|
||||||
|
if (asteroids[index].xo > image.width - 40 || asteroids[index].xo < 40) {
|
||||||
|
asteroids[index].x_velocity = -asteroids[index].x_velocity;
|
||||||
|
asteroids[index].xo += asteroids[index].x_velocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (asteroids[index].yo > image.height - 40 || asteroids[index].yo < 40) {
|
||||||
|
asteroids[index].y_velocity = -asteroids[index].y_velocity;
|
||||||
|
asteroids[index].yo += asteroids[index].y_velocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end for index
|
||||||
|
|
||||||
|
} // end Translate_Asteroids
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void Rotate_Asteroids(void) {
|
||||||
|
|
||||||
|
int index;
|
||||||
|
|
||||||
|
for (index = 0; index < NUM_ASTEROIDS; index++) {
|
||||||
|
// rotate current asteroid
|
||||||
|
Rotate_Object((object_ptr)&asteroids[index], asteroids[index].angle);
|
||||||
|
|
||||||
|
} // end for index
|
||||||
|
|
||||||
|
} // end Rotate_Asteroids
|
||||||
|
|
||||||
EXPORT(cpptest) image_data_t* cpptest(uint16_t w, uint16_t h) {
|
EXPORT(cpptest) image_data_t* cpptest(uint16_t w, uint16_t h) {
|
||||||
image = image_create(w, h);
|
image = image_create(w, h);
|
||||||
|
|
||||||
image_fill_rect(image, 0, 0, w, h, {0xFF000000});
|
image_fill_rect(image, 0, 0, w, h, {0xFF000000});
|
||||||
|
|
||||||
|
Create_Field();
|
||||||
|
|
||||||
return ℑ
|
return ℑ
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EXPORT(step) void step() {
|
||||||
|
Draw_Asteroids(ERASE);
|
||||||
|
|
||||||
|
// transform field
|
||||||
|
|
||||||
|
Rotate_Asteroids();
|
||||||
|
|
||||||
|
Translate_Asteroids();
|
||||||
|
|
||||||
|
// draw field
|
||||||
|
|
||||||
|
Draw_Asteroids(DRAW);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue