Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <allegro.h>
- #include <time.h>
- #include <math.h>
- #define COL_BACK 0x000000
- #define COL_CON 0x808080
- #define DIM 600
- typedef struct {
- int x, y;
- struct pointStack *next;
- } pointStack;
- void push(int x, int y, pointStack **sp) {
- pointStack *newSE = (pointStack*) malloc(sizeof(pointStack));
- if ((*sp) == NULL) {
- newSE->next = NULL;
- newSE->x = x;
- newSE->y = y;
- (*sp) = newSE;
- return;
- }
- newSE->x = x;
- newSE->y = y;
- newSE->next = (*sp);
- (*sp) = newSE;
- return;
- }
- void pop(int *x, int *y, pointStack **sp) {
- if ((*sp) == NULL)
- return;
- (*x) = (*sp)->x;
- (*y) = (*sp)->y;
- pointStack *tr = sp;
- (*sp) = (*sp)->next;
- free(tr);
- return;
- }
- void draw4SimCircle(int xc, int yc) {
- int r;
- r = 100 + rand() % 100;
- float alpha, angle;
- angle = 1.0 / r;
- for (alpha = 0; alpha < (M_PI / 2); alpha += angle) {
- int x = r * cos(alpha);
- int y = r * sin(alpha);
- putpixel(screen, xc + x, yc + y, COL_CON);
- putpixel(screen, xc + x, yc - y, COL_CON);
- putpixel(screen, xc - x, yc + y, COL_CON);
- putpixel(screen, xc - x, yc - y, COL_CON);
- }
- }
- void draw8SimCircle(int xc, int yc) {
- int r;
- r = 100 + rand() % 100;
- float alpha, angle;
- angle = 1.0 / r;
- for (alpha = 0; alpha < (M_PI / 4); alpha += angle) {
- int x = r * cos(alpha);
- int y = r * sin(alpha);
- putpixel(screen, xc + x, yc + y, COL_CON);
- putpixel(screen, xc + x, yc - y, COL_CON);
- putpixel(screen, xc - x, yc + y, COL_CON);
- putpixel(screen, xc - x, yc - y, COL_CON);
- putpixel(screen, xc + y, yc + x, COL_CON);
- putpixel(screen, xc + y, yc - x, COL_CON);
- putpixel(screen, xc - y, yc + x, COL_CON);
- putpixel(screen, xc - y, yc - x, COL_CON);
- }
- }
- // wypelnienie rekurencyjne powoduje przepelnienie stosu i zawieszenie programu
- void fillRec(int x, int y, int col) {
- int pc = getpixel(screen, x, y);
- if ((pc != col) && (pc != COL_CON)) {
- putpixel(screen, x, y, col);
- fillRec(x + 1, y, col);
- fillRec(x, y - 1, col);
- fillRec(x - 1, y, col);
- fillRec(x, y + 1, col);
- }
- }
- //nalezy uzyc wypelnienia iteracyjnego
- void fillIt(int x, int y, int col) {
- pointStack *sp;
- int pc;
- int xs, ys;
- xs = x;
- ys = y;
- do {
- pc = getpixel(screen, x, y);
- if ((pc != col) && (pc != COL_CON))
- putpixel(screen, x, y, col);
- pc = getpixel(screen, x + 1, y);
- if ((pc != col) && (pc != COL_CON)) {
- push(x, y, &sp);
- x = x + 1;
- continue;
- }
- pc = getpixel(screen, x, y - 1);
- if ((pc != col) && (pc != COL_CON)) {
- push(x, y, &sp);
- y = y - 1;
- continue;
- }
- pc = getpixel(screen, x - 1, y);
- if ((pc != col) && (pc != COL_CON)) {
- push(x, y, &sp);
- x = x - 1;
- continue;
- }
- pc = getpixel(screen, x, y + 1);
- if ((pc != col) && (pc != COL_CON)) {
- push(x, y, &sp);
- y = y + 1;
- continue;
- }
- pc = getpixel(screen, x, y);
- if ((pc == col) || (pc == COL_CON)) {
- pop(&x, &y, &sp);
- continue;
- }
- } while((x != xs || y != ys) && sp != NULL);
- }
- int main() {
- srand(time(NULL));
- allegro_init();
- install_keyboard();
- set_color_depth(32);
- set_gfx_mode(GFX_AUTODETECT_WINDOWED, DIM, DIM, 0, 0);
- set_palette(default_palette);
- clear_to_color(screen, COL_BACK);
- int xc, yc;
- xc = 200 + rand() % 200;
- yc = 200 + rand() % 200;
- draw8SimCircle(xc, yc);
- int col = makecol(rand() % 255, rand() % 255, rand() % 255);
- fillIt(xc, yc, col);
- readkey();
- remove_keyboard();
- allegro_exit();
- return 0;
- }
- END_OF_MAIN();
Advertisement
Add Comment
Please, Sign In to add comment