File: turtle.c

package info (click to toggle)
mlv 3.1.0-8
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 10,596 kB
  • sloc: ansic: 18,982; sh: 4,760; makefile: 381; objc: 246; xml: 92
file content (383 lines) | stat: -rw-r--r-- 9,239 bytes parent folder | download | duplicates (3)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 *   This file is part of the MLV Library.
 *
 *   Copyright (C) 2016 Adrien Boussicault
 *
 *
 *    This Library is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This Library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this Library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "MLV_turtle.h"
#include "MLV_window.h"
#include "MLV_shape.h"
#include "MLV_time.h"

#include "window.h"
#include "warning_error.h"

#include "memory_management.h"

#include "data_structure.h"

#include "platform.h"

#include "turtle.h"
#include <math.h>
#include <assert.h>

extern DataMLV* MLV_data; 

struct _MLV_Turtle {
	double angle;
	double x;
	double y;
	int write;
	int degree;
	MLV_Color color;
	MLV_Image* image; // The image were the turtle is drawing.
};

struct _MLV_Leonardo_turtle {
	int time;
	int update;
	int is_visible;
	MLV_Turtle* turtle;
};

void MLV_free_turtle( MLV_Turtle* turtle ){
	MLV_FREE( turtle, MLV_Turtle );
}

MLV_Turtle* MLV_create_turtle(){
	MLV_Turtle* turtle = (MLV_Turtle*) malloc( sizeof(MLV_Turtle) );
	turtle->angle = 0;
	turtle->x = 0;
	turtle->y = 0;
	turtle->write = 0;
	turtle->color = MLV_COLOR_GREEN;
	turtle->degree = 1;
	turtle->image = NULL;
	return turtle;
}

void set_angle_in_radian( MLV_Turtle* turtle, double angle ){
	if( turtle->degree ){
		turtle->angle = (angle*180.0)/M_PI;
	}else{
		turtle->angle = angle;
	}
}

void set_angle_in_degree( MLV_Turtle* turtle, double angle ){
	if( turtle->degree ){
		turtle->angle = angle;
	}else{
		turtle->angle = (angle*M_PI)/180.0;
	}
}

void MLV_turtle_radian( MLV_Turtle* turtle ){
	if( turtle->degree ){
		turtle->degree = 0;
		set_angle_in_degree( turtle, turtle->angle );
	}
}

void MLV_turtle_degree( MLV_Turtle* turtle ){
	if( ! turtle->degree ){
		turtle->degree = 1;
		set_angle_in_radian( turtle, turtle->angle );
	}
}

void MLV_turtle_forward( MLV_Turtle* turtle, float distance ){
	int old_x = turtle->x;
	int old_y = turtle->y;
	if( turtle->degree ){
		turtle->x += distance * cos( (turtle->angle * M_PI)/180.0 );
		turtle->y += distance * sin( (turtle->angle * M_PI)/180.0 );
	}else{
		turtle->x += distance * cos(turtle->angle);
		turtle->y += distance * sin(turtle->angle);
	}
	if( turtle->write ){
		if( turtle->image ){
			MLV_draw_line_on_image(
				old_x, old_y, turtle->x, turtle->y, turtle->color
				, turtle->image
			);
		}else{
			MLV_draw_line( old_x, old_y, turtle->x, turtle->y, turtle->color );
		}
	}
}

void MLV_turtle_right( MLV_Turtle* turtle, double angle ){
	turtle->angle += angle;
}

void MLV_turtle_left( MLV_Turtle* turtle, double angle ){
	turtle->angle -= angle;
}

void MLV_turtle_go_to( MLV_Turtle* turtle, int x, int y ){
	int old_x = turtle->x;
	int old_y = turtle->y;
	turtle->x = x;
	turtle->y = y;
	if( turtle->write ){
		if( turtle->image ){
			MLV_draw_line_on_image(
				old_x, old_y, turtle->x, turtle->y, turtle->color
				, turtle->image
			);
		}else{
			MLV_draw_line( old_x, old_y, turtle->x, turtle->y, turtle->color );
		}
	}
}

void MLV_turtle_color( MLV_Turtle* turtle, MLV_Color color ){
	turtle->color = color;
}

void MLV_turtle_write( MLV_Turtle* turtle, int write ){
	turtle->write = write;
	if( write ){
		if( turtle->image ){
			MLV_draw_point_on_image(
				turtle->x, turtle->y, turtle->color, turtle->image
			);
		}else{
			MLV_draw_point( turtle->x, turtle->y, turtle->color );
		}
	}
}

int MLV_turtle_X_coordinate( MLV_Turtle* turtle ){
	return turtle->x;
}

int MLV_turtle_Y_coordinate( MLV_Turtle* turtle ){
	return turtle->y;
}

double MLV_turtle_orientation( MLV_Turtle* turtle ){
	return turtle->angle;
}

double MLV_turtle_orientation_in_radian( MLV_Turtle* turtle ){
	if( turtle->degree ){
		return (turtle->angle*M_PI)/180.0;
	}else{
		return turtle->angle;
	}
}

double MLV_turtle_orientation_in_degree( MLV_Turtle* turtle ){
	if( turtle->degree ){
		return turtle->angle;
	}else{
		return (turtle->angle*180.0)/M_PI;
	}
}

int MLV_turtle_is_writing( MLV_Turtle* turtle ){
	return turtle->write;
}

void MLV_turtle_point_to( MLV_Turtle* turtle, int x, int y ){
	double vx = (x - turtle->x);
	double vy = (y - turtle->y);
	assert( (vx != 0.0) || (vy != 0) );
	double vn = sqrt( vx*vx + vy*vy );
	if( asin(vy/vn) >= 0.0 ){
		set_angle_in_radian( turtle, acos(vx/vn) );
	}else{
		set_angle_in_radian( turtle, - acos(vx/vn) );
	}
}

void MLV_turtle_attach_on_image( MLV_Turtle* turtle, MLV_Image* image ){
	turtle->image = image;
}

void MLV_turtle_orient_to( MLV_Turtle* turtle, double angle ){
	turtle->angle  = angle;
}

void MLV_draw_turtle_on_image( MLV_Turtle* turtle, MLV_Image* image ){
	double size = 8;

	double angle = MLV_leonardo_orientation_in_radian();

	double x = MLV_leonardo_X_coordinate();
	double y = MLV_leonardo_Y_coordinate();

	double dx = cos( angle ); double dy = sin( angle );
	double nx = -dy; double ny = dx;

	const double sqrt3_2 = .86603;

	double Ax = x - (sqrt3_2*size/3.0)*dx + (size/2.0)*nx;
	double Ay = y - (sqrt3_2*size/3.0)*dy + (size/2.0)*ny;

	double Bx = x + (2*sqrt3_2*size/3.0)*dx;
	double By = y + (2*sqrt3_2*size/3.0)*dy;

	double Cx = x - (.86603*size/3.0)*dx - (size/2.0)*nx;
	double Cy = y - (.86603*size/3.0)*dy - (size/2.0)*ny;

	MLV_draw_filled_triangle_on_image(
		Ax, Ay, Bx, By, Cx, Cy, turtle->color, image
	);
}

void free_leonardo_turtle(){
	MLV_free_turtle( MLV_data->leonardo->turtle );
	MLV_FREE( MLV_data->leonardo, MLV_Lenoardo_turtle );
	MLV_data->leonardo = NULL;
}

void init_leonardo_turtle(){
	if( MLV_data->leonardo ){
		free_leonardo_turtle();
	}
	MLV_data->leonardo = MLV_MALLOC( 1, MLV_Leonardo_turtle );

	MLV_data->leonardo->turtle = MLV_create_turtle();
	MLV_turtle_degree( MLV_data->leonardo->turtle );
	MLV_turtle_orient_to( MLV_data->leonardo->turtle, 0 );
	MLV_turtle_color( MLV_data->leonardo->turtle, MLV_COLOR_GREEN );
	MLV_turtle_write( MLV_data->leonardo->turtle, 0 );

	MLV_turtle_go_to(
		MLV_data->leonardo->turtle,
		MLV_get_window_width()/2, MLV_get_window_height()/2
	);

	MLV_leonardo_speed( 500 );
	MLV_leonardo_should_update_window( 1 );
	MLV_data->leonardo->is_visible = 0;
}

void MLV_show_leonardo(){
	if( ! MLV_data->leonardo->is_visible ){
		MLV_register_a_post_producter( &MLV_draw_leonardo_on_image );
		MLV_data->leonardo->is_visible = 1;
	}
	if( MLV_data->leonardo->update ){
		MLV_update_window();
	}
}

void MLV_hide_leonardo(){
	if( MLV_data->leonardo->is_visible ){
		MLV_unregister_a_post_producter( &MLV_draw_leonardo_on_image );
		MLV_data->leonardo->is_visible = 0;
	}
	if( MLV_data->leonardo->update ){
		MLV_update_window();
	}
}

void leonardo_updates_window(){
	if( MLV_data->leonardo->update ){
		MLV_update_window();
	}
	if( MLV_data->leonardo->time != 0 ){
		MLV_wait_milliseconds( MLV_data->leonardo->time );
	}
}

void MLV_leonardo_radian(){
	MLV_turtle_radian( MLV_data->leonardo->turtle );
}

void MLV_leonardo_degree(){
	MLV_turtle_degree( MLV_data->leonardo->turtle );
}

void MLV_leonardo_forward( float distance ){
	MLV_turtle_forward( MLV_data->leonardo->turtle, distance );
	leonardo_updates_window();
}

void MLV_leonardo_right( double angle ){
	MLV_turtle_right( MLV_data->leonardo->turtle, angle ); 
	leonardo_updates_window();
}

void MLV_leonardo_left( double angle ){
	MLV_turtle_left( MLV_data->leonardo->turtle, angle );
	leonardo_updates_window();
}

void MLV_leonardo_go_to( int x, int y ){
	MLV_turtle_go_to( MLV_data->leonardo->turtle, x, y );
	leonardo_updates_window();
}

void MLV_leonardo_orient_to( double angle ){
	MLV_turtle_orient_to( MLV_data->leonardo->turtle, angle );
	leonardo_updates_window();
}

void MLV_leonardo_color( MLV_Color color ){
	MLV_turtle_color( MLV_data->leonardo->turtle, color );
}

void MLV_leonardo_write( int write ){
	MLV_turtle_write( MLV_data->leonardo->turtle, write );
}

int MLV_leonardo_X_coordinate(){
	return MLV_turtle_X_coordinate( MLV_data->leonardo->turtle );
}

int MLV_leonardo_Y_coordinate(){
	return MLV_turtle_Y_coordinate( MLV_data->leonardo->turtle );
}

double MLV_leonardo_orientation(){
	return MLV_turtle_orientation( MLV_data->leonardo->turtle );
}

double MLV_leonardo_orientation_in_radian(){
	return MLV_turtle_orientation_in_radian( MLV_data->leonardo->turtle );
}

double MLV_leonardo_orientation_in_degree(){
	return MLV_turtle_orientation_in_degree( MLV_data->leonardo->turtle );
}

int MLV_leonardo_is_writing(){
	return MLV_turtle_is_writing( MLV_data->leonardo->turtle );
}

void MLV_leonardo_point_to( int x, int y ){
	MLV_turtle_point_to( MLV_data->leonardo->turtle, x, y );
}

void MLV_leonardo_speed( int time ){
	MLV_data->leonardo->time = time;
}

void MLV_leonardo_should_update_window( int yes ){
	MLV_data->leonardo->update = yes;
}

void MLV_draw_leonardo_on_image( MLV_Image* image ){
	MLV_draw_turtle_on_image( MLV_data->leonardo->turtle, image );
}