diff --git a/lab9/1.c b/lab9/1.c new file mode 100644 index 0000000..bb8c1fb --- /dev/null +++ b/lab9/1.c @@ -0,0 +1,55 @@ +#include +#include +#include + +// +// +// +// + +void draw_disks(FILE * file, int disks, int peg, int width, int y) { + if ( !disks ) { return; } // no more disks to draw + if ( y < 120) { return; } // keeps the peg from overflowing; + char *colors[] = { "#7fffd4", "#ec8484", "#149dca", "#e4d93c", "#222d24", + "#ef8a48", "#ee6517", "#bbe200", "#ba1d1e", "#ffc4c4"}; + int random_color = rand() % 10; + int x = 70 + ((peg - 1) * 100); + fprintf(file, "\r\n", + x - width / 2 + 10, y, width, colors[random_color]); + draw_disks(file, disks - 1, peg, width - width / 4, y - 21); +} + +void draw_pegs(FILE * file, int pegs, int width) { + if ( !pegs ) { return; } // exit function + fprintf(file, "\r\n", width); + draw_pegs(file, pegs - 1, width + 100); +} + +void draw_peg_scene(FILE * file, int pegs, int disks) { + int scene_width = pegs * 100; + fprintf(file, "\r\n"); // start + // draw scene + fprintf(file, "\r\n", scene_width); + draw_pegs(file, pegs, 70); // draw pegs + draw_disks(file, disks, rand() % pegs + 1, 80, 230); + fprintf(file, ""); // end +} + +int main(int argc, char *argv[]) { + srand(time(NULL)); // seed random num generator to make colors unique each time + FILE * file; + if ( !(file = fopen("test.svg", "w")) ) { + perror("Can't open file!"); + return -1; + } + draw_peg_scene(file, 3, 10); + + if ( fclose(file) ) { + perror("Can't close file!"); + } + + return 0; +} diff --git a/lab9/test.svg b/lab9/test.svg new file mode 100644 index 0000000..33b81c2 --- /dev/null +++ b/lab9/test.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file