Haifisch7734

Kolo

Oct 27th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <allegro.h>
  4. #include <time.h>
  5. #include <math.h>
  6.  
  7. #define COL_BACK    0x000000
  8. #define COL_CON     0x808080
  9. #define DIM         600
  10.  
  11. typedef struct {
  12.     int x, y;
  13.     struct pointStack *next;
  14. } pointStack;
  15.  
  16. void push(int x, int y, pointStack **sp) {
  17.     pointStack *newSE = (pointStack*) malloc(sizeof(pointStack));
  18.     if ((*sp) == NULL) {
  19.         newSE->next = NULL;
  20.         newSE->x = x;
  21.         newSE->y = y;
  22.         (*sp) = newSE;
  23.         return;
  24.     }
  25.     newSE->x = x;
  26.     newSE->y = y;
  27.     newSE->next = (*sp);
  28.     (*sp) = newSE;
  29.  
  30.     return;
  31. }
  32.  
  33. void pop(int *x, int *y, pointStack **sp) {
  34.     if ((*sp) == NULL)
  35.         return;
  36.     (*x) = (*sp)->x;
  37.     (*y) = (*sp)->y;
  38.     pointStack *tr = sp;
  39.     (*sp) = (*sp)->next;
  40.     free(tr);
  41.     return;
  42. }
  43.  
  44. void draw4SimCircle(int xc, int yc) {
  45.     int r;
  46.     r = 100 + rand() % 100;
  47.     float alpha, angle;
  48.     angle = 1.0 / r;
  49.     for (alpha = 0; alpha < (M_PI / 2); alpha += angle) {
  50.         int x = r * cos(alpha);
  51.         int y = r * sin(alpha);
  52.         putpixel(screen, xc + x, yc + y, COL_CON);
  53.         putpixel(screen, xc + x, yc - y, COL_CON);
  54.         putpixel(screen, xc - x, yc + y, COL_CON);
  55.         putpixel(screen, xc - x, yc - y, COL_CON);
  56.     }
  57. }
  58.  
  59.  
  60. void draw8SimCircle(int xc, int yc) {
  61.     int r;
  62.     r = 100 + rand() % 100;
  63.     float alpha, angle;
  64.     angle = 1.0 / r;
  65.     for (alpha = 0; alpha < (M_PI / 4); alpha += angle) {
  66.         int x = r * cos(alpha);
  67.         int y = r * sin(alpha);
  68.         putpixel(screen, xc + x, yc + y, COL_CON);
  69.         putpixel(screen, xc + x, yc - y, COL_CON);
  70.         putpixel(screen, xc - x, yc + y, COL_CON);
  71.         putpixel(screen, xc - x, yc - y, COL_CON);
  72.         putpixel(screen, xc + y, yc + x, COL_CON);
  73.         putpixel(screen, xc + y, yc - x, COL_CON);
  74.         putpixel(screen, xc - y, yc + x, COL_CON);
  75.         putpixel(screen, xc - y, yc - x, COL_CON);
  76.     }
  77. }
  78.  
  79. // wypelnienie rekurencyjne powoduje przepelnienie stosu i zawieszenie programu
  80. void fillRec(int x, int y, int col) {
  81.     int pc = getpixel(screen, x, y);
  82.     if ((pc != col) && (pc != COL_CON)) {
  83.         putpixel(screen, x, y, col);
  84.         fillRec(x + 1, y, col);
  85.         fillRec(x, y - 1, col);
  86.         fillRec(x - 1, y, col);
  87.         fillRec(x, y + 1, col);
  88.     }
  89. }
  90.  
  91. //nalezy uzyc wypelnienia iteracyjnego
  92. void fillIt(int x, int y, int col) {
  93.     pointStack *sp;
  94.     int pc;
  95.     int xs, ys;
  96.     xs = x;
  97.     ys = y;
  98.     do {
  99.         pc = getpixel(screen, x, y);
  100.         if ((pc != col) && (pc != COL_CON))
  101.             putpixel(screen, x, y, col);
  102.  
  103.         pc = getpixel(screen, x + 1, y);
  104.         if ((pc != col) && (pc != COL_CON)) {
  105.             push(x, y, &sp);
  106.             x = x + 1;
  107.             continue;
  108.         }
  109.         pc = getpixel(screen, x, y - 1);
  110.         if ((pc != col) && (pc != COL_CON)) {
  111.             push(x, y, &sp);
  112.             y = y - 1;
  113.             continue;
  114.         }
  115.         pc = getpixel(screen, x - 1, y);
  116.         if ((pc != col) && (pc != COL_CON)) {
  117.             push(x, y, &sp);
  118.             x = x - 1;
  119.             continue;
  120.         }
  121.         pc = getpixel(screen, x, y + 1);
  122.         if ((pc != col) && (pc != COL_CON)) {
  123.             push(x, y, &sp);
  124.             y = y + 1;
  125.             continue;
  126.         }
  127.         pc = getpixel(screen, x, y);
  128.         if ((pc == col) || (pc == COL_CON)) {
  129.             pop(&x, &y, &sp);
  130.             continue;
  131.         }
  132.     } while((x != xs || y != ys) && sp != NULL);
  133. }
  134. int main() {
  135.     srand(time(NULL));
  136.  
  137.     allegro_init();
  138.     install_keyboard();
  139.     set_color_depth(32);
  140.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, DIM, DIM, 0, 0);
  141.     set_palette(default_palette);
  142.     clear_to_color(screen, COL_BACK);
  143.  
  144.     int xc, yc;
  145.     xc = 200 + rand() % 200;
  146.     yc = 200 + rand() % 200;
  147.     draw8SimCircle(xc, yc);
  148.  
  149.     int col = makecol(rand() % 255, rand() % 255, rand() % 255);
  150.     fillIt(xc, yc, col);
  151.     readkey();
  152.     remove_keyboard();
  153.     allegro_exit();
  154.     return 0;
  155. }
  156. END_OF_MAIN();
Advertisement
Add Comment
Please, Sign In to add comment