/* _______ ____ __ ___ ___ * \ _ \ \ / \ / \ \ / / ' ' ' * | | \ \ | | || | \/ | . . * | | | | | | || ||\ /| | * | | | | | | || || \/ | | ' ' ' * | | | | | | || || | | . . * | |_/ / \ \__// || | | * /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque * / \ * / . \ * fnptr.txt - Function pointer explanation. / / \ \ * | < / \_ * | \/ /\ / * \_ / > / * | \ / / * | ' / * \__/ */ C allows you to create and use function pointers. A function pointer is a variable that points to a function, and you can use it to call that function. Why is this useful? Function pointers can be passed as parameters. As an example, here's a function from Allegro: void create_light_table(COLOR_MAP *table, const PALETTE pal, int r, g, b, void (*callback)(int pos)); Don't worry about the syntax just yet, but the last parameter, 'callback', is a pointer to a function that takes an int parameter. create_light_table() can take some time to complete its work, and you may want to display a progress indicator. So you write a function to draw the progress indicator, and then, for 'callback', you specify a pointer to your function. This will enable create_light_table() to call your function at intervals during its processing. (If you don't want to use the callback, you can pass NULL, but this only works because create_light_table() checks actively for NULL. You can't always specify NULL when you want nothing to happen.) There are many other uses. In addition to using function pointers as parameters, Allegro has some global function pointers you can set to point to your functions. Function pointers can also be used in structs, and this is where DUMB makes the most use of them. So how are they used? void bar(void) { ... } /* Here's a function */ void (*foo)(void) = &bar; /* Take a pointer */ (*foo)(); /* Call the function */ char *baz(float a) { ... } /* Here's another function */ char *(*foobarbaz)(float a) = &baz; /* Take a pointer */ char *rv = (*foobarbaz)(0.1); /* Call the function */ In both these cases, note how the statement for calling the pointed-to function (third line) resembles the definition of the function pointer (second line). This is true of any variable in C, and can lead to some truly obfuscated definitions if you are that way inclined. Such definitions can be clarified with typedefs, but before you use those, it is important you understand how the above statements work. I speak from experience: function pointer notation looks random and scary, until you understand why it's the way it is; then it makes perfect sense. (It is actually permissible to omit the & when taking a pointer and to write e.g. foobarbaz(0.1) instead of (*foobarbaz)(0.1). However, I recommend not doing this, since the syntax for using the pointer no longer resembles the definition. Writing e.g. (*foobarbaz)(0.1) also makes a clear distinction between function pointer calls and ordinary function calls, which makes code more readable.) Note that function pointers have the return value and parameter list specified. A function pointer can only point to a function with a matching return value and matching parameters. (You can break this rule by casting the pointer explicitly, but there is no situation where doing so is portable to all computers, and I strongly advise against it unless you're writing system code. If you're not sure whether you're writing system code or not, then you're not.) The parameter names need not match (although the types must). If you wish to rename a parameter in your function, you do not have to change the function pointer accordingly. In fact, when you define a function pointer, you don't even have to specify the names of parameters if you don't want to. I normally do so for clarity. It is possible to typedef a function pointer. In order to typedef a function pointer, you start by declaring the pointer as a variable: void (*myfunc)(void); Then you write 'typedef' before it and replace the variable name, which is myfunc, with the type name (this rule can be applied to any variable when you want to use typedef): typedef void (*MYTYPE)(void); Now 'MYTYPE' represents a pointer to a function with no parameters and no return value. The following two lines are completely equivalent: MYTYPE myfunc; void (*myfunc)(void); Note that we use MYTYPE without an asterisk (*), since it is already a pointer. That's it. If you feel anything should be explained better here, or if you feel something should be added, please don't hesitate to let me know! Ben Davis entheh@users.sf.net IRC EFnet #dumb See readme.txt for details on using IRC. href='#n87'>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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
/* penrose.c
*
* Penrose tile generator.
*
* Uses half-tile technique outlined on:
*
* http://tartarus.org/simon/20110412-penrose/penrose.xhtml
*/
#include <assert.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include "puzzles.h" /* for malloc routines, and PI */
#include "penrose.h"
/* -------------------------------------------------------
* 36-degree basis vector arithmetic routines.
*/
/* Imagine drawing a
* ten-point 'clock face' like this:
*
* -E
* -D | A
* \ | /
* -C. \ | / ,B
* `-._\|/_,-'
* ,-' /|\ `-.
* -B' / | \ `C
* / | \
* -A | D
* E
*
* In case the ASCII art isn't clear, those are supposed to be ten
* vectors of length 1, all sticking out from the origin at equal
* angular spacing (hence 36 degrees). Our basis vectors are A,B,C,D (I
* choose them to be symmetric about the x-axis so that the final
* translation into 2d coordinates will also be symmetric, which I
* think will avoid minor rounding uglinesses), so our vector
* representation sets
*
* A = (1,0,0,0)
* B = (0,1,0,0)
* C = (0,0,1,0)
* D = (0,0,0,1)
*
* The fifth vector E looks at first glance as if it needs to be
* another basis vector, but in fact it doesn't, because it can be
* represented in terms of the other four. Imagine starting from the
* origin and following the path -A, +B, -C, +D: you'll find you've
* traced four sides of a pentagram, and ended up one E-vector away
* from the origin. So we have
*
* E = (-1,1,-1,1)
*
* This tells us that we can rotate any vector in this system by 36
* degrees: if we start with a*A + b*B + c*C + d*D, we want to end up
* with a*B + b*C + c*D + d*E, and we substitute our identity for E to
* turn that into a*B + b*C + c*D + d*(-A+B-C+D). In other words,
*
* rotate_one_notch_clockwise(a,b,c,d) = (-d, d+a, -d+b, d+c)
*
* and you can verify for yourself that applying that operation
* repeatedly starting with (1,0,0,0) cycles round ten vectors and
* comes back to where it started.
*
* The other operation that may be required is to construct vectors
* with lengths that are multiples of phi. That can be done by
* observing that the vector C-B is parallel to E and has length 1/phi,
* and the vector D-A is parallel to E and has length phi. So this
* tells us that given any vector, we can construct one which points in