-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation.c
More file actions
217 lines (171 loc) · 6.09 KB
/
animation.c
File metadata and controls
217 lines (171 loc) · 6.09 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <SDL2/SDL.h>
#include <stdbool.h>
#include <math.h>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
typedef struct {
double x, y;
double dx, dy;
} Point;
typedef struct {
Point p;
Point q;
} Line;
void updatePoint(Point* point) {
point->x += point->dx;
point->y += point->dy;
}
void updateLine(Line* line) {
updatePoint(&(line->p));
updatePoint(&(line->q));
}
void renderPoint(SDL_Renderer *renderer, Point point, int r, int g, int b) {
SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);
SDL_RenderDrawPoint(renderer, (int) point.x, (int) point.y);
}
void renderDot(SDL_Renderer *renderer, Point point, int thickness, int r, int g, int b) {
SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);
SDL_Rect rect = {point.x - thickness / 2.0, point.y - thickness / 2.0, thickness, thickness};
SDL_RenderFillRect(renderer, &rect);
}
void renderSegment(SDL_Renderer *renderer, Line line, int r, int g, int b) {
SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer, (int) line.p.x, (int) line.p.y, (int) line.q.x, (int) line.q.y);
}
void renderLine(SDL_Renderer *renderer, Line line, int r, int g, int b) {
SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);
// vertical line
if (line.p.x == line.q.x)
SDL_RenderDrawLine(renderer, (int) line.p.x, 0, (int) line.q.x, WINDOW_HEIGHT-1);
// slope != infinity
double slope = (line.q.y - line.p.y) / (line.q.x - line.p.x);
SDL_RenderDrawLine(renderer, 0, (int) (line.p.y - slope * line.p.x), WINDOW_WIDTH-1, (int) (line.p.y + slope * (WINDOW_WIDTH - line.p.x)));
}
Point calculate_Qp(Point A, Point P, Point Q) {
double a = P.x - A.x;
double b = P.y - A.y;
double c = Q.x - A.x;
double d = Q.y - A.y;
double det = -a*c -b*d;
double x = (-b*b*c + a*b*d) / det;
double y = (-b*b*d - a*a*d) / det;
Point output = {x, y, 0, 0};
return output;
}
Point math2graphics(Point A) {
Point output;
output.x = A.x + (WINDOW_WIDTH / 2);
output.y = -1 * A.y + (WINDOW_HEIGHT * 3 / 4);
return output;
}
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
SDL_Window *window = SDL_CreateWindow("Distance Recognition Without LIDAR",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_SHOWN);
if (!window) {
SDL_Log("Could not create window: %s", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
SDL_Log("Could not create renderer: %s", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// Initialize points with random positions and velocities
Point O = {0, 0, 0, 0};
Point A = {-100, 0, 0, 0};
Point B = {100, 0, 0, 0};
Point P = {0, 200, 0, 0}; // unknown to the observer
int trail_max = 100;
Point Q_es[trail_max];
double t = 0;
double dt = 0.01;
bool running = true;
SDL_Event event;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
t += dt;
updatePoint(&O);
updatePoint(&A);
updatePoint(&B);
updatePoint(&P);
Point Q = {80 + 200*cos(t), 300 + 50*sin(2*t), 0, 0}; // unknown to the observer
double w = B.x - A.x;
double theta = atan2(P.y - A.y, P.x - A.x);
double dist = (w/2) * tan(theta); // d has to be obtained the hard way
Point P_e = {0, dist, 0, 0};
Line AB = {A, B};
Line AP_e = {A, P_e};
Line BP_e = {B, P_e};
Point Qp = calculate_Qp(A, P, Q);
Point Qpp = calculate_Qp(B, P, Q);
Line PQp = {P, Qp};
Line PQpp = {P, Qpp};
Line AQ = {A, Q};
Line BQ = {B, Q};
// Calculate Q_e
double a = Qp.x - A.x;
double b = Qp.y - A.y;
double c = Qpp.x - B.x;
double d = Qpp.y - B.y;
double x = (w/2.)*(a*d+b*c)/(a*d-b*c);
double y = b*d*w/(a*d-b*c);
Point Q_e = {x, y, 0, 0};
Point gO = math2graphics(O);
Point gA = math2graphics(A);
Point gB = math2graphics(B);
Point gP = math2graphics(P);
Point gQ = math2graphics(Q);
Point gP_e = math2graphics(P_e);
Point gQp = math2graphics(Qp);
Point gQpp = math2graphics(Qpp);
Line gAP_e = {math2graphics(AP_e.p), math2graphics(AP_e.q)};
Line gBP_e = {math2graphics(BP_e.p), math2graphics(BP_e.q)};
Line gPQp = {math2graphics(PQp.p), math2graphics(PQp.q)};
Line gPQpp = {math2graphics(PQpp.p), math2graphics(PQpp.q)};
Line gAQ = {math2graphics(AQ.p), math2graphics(AQ.q)};
Line gBQ = {math2graphics(BQ.p), math2graphics(BQ.q)};
Point gQ_e = math2graphics(Q_e);
for (int i=trail_max-1; i>0; --i) {
Q_es[i] = Q_es[i-1];
}
Q_es[0] = gQ_e;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); // Black color
SDL_RenderClear(renderer);
renderSegment(renderer, gAP_e, 30, 30, 30);
renderSegment(renderer, gBP_e, 30, 30, 30);
renderLine(renderer, gPQp, 30, 30, 30);
renderLine(renderer, gPQpp, 30, 30, 30);
renderSegment(renderer, gAQ, 30, 30, 30);
renderSegment(renderer, gBQ, 30, 30, 30);
renderDot(renderer, gO, 3, 255, 255, 255); // known
renderDot(renderer, gA, 3, 255, 255, 255); // known
renderDot(renderer, gB, 3, 255, 255, 255); // known
renderDot(renderer, gP, 5, 255, 100, 100); // unknown
renderDot(renderer, gQ, 5, 255, 100, 100); // unknown
renderDot(renderer, gQp, 3, 255, 255, 100); // observed
renderDot(renderer, gQpp, 3, 255, 255, 100); // observed
renderDot(renderer, gP_e, 3, 100, 255, 255); // calculated
for (int i=trail_max-1; i>=0; --i)
renderPoint(renderer, Q_es[i], 100, 100, 100);
renderDot(renderer, gQ_e, 3, 100, 255, 255); // calculated
SDL_RenderPresent(renderer);
SDL_Delay(16); // ~60 FPS
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}