fork download
  1. #include <stdio.h>
  2.  
  3. /* screen size - adapt to console size */
  4. #define NUM_COLUMNS 80
  5. #define NUM_ROWS 25
  6.  
  7. /* screen buffer */
  8. char screen[NUM_COLUMNS][NUM_ROWS];
  9.  
  10. /* initialize buffer by setting all pixels to spaces */
  11. void initialize_buffer() {
  12. for (int i = 0; i < NUM_ROWS; i++)
  13. for (int j = 0; j < NUM_COLUMNS; j++)
  14. screen[j][i] = ' ';
  15. }
  16.  
  17.  
  18. /* update screen by copying buffer to console window */
  19. void update_screen() {
  20. for (int i = 0; i < NUM_ROWS; i++) {
  21. for (int j = 0; j < NUM_COLUMNS; j++)
  22. putchar(screen[j][i]);
  23. putchar('\n');
  24. }
  25. fflush(stdout);
  26. }
  27.  
  28. /* checks whether a given point (x,y) is within the screen */
  29. unsigned in_screen(int x, int y) {
  30. // TODO
  31. return 0;
  32. }
  33.  
  34. /* draw a single point at given position (x,y) and with given character */
  35. void draw_pixel(int x, int y, char c) {
  36. // TODO
  37. }
  38.  
  39. /* clear a single pixel at position (x,y) */
  40. void clear_pixel(int x, int y) {
  41. // TODO
  42. }
  43.  
  44. /* draw a horizontal line (x1,y)-(x2,y) with dashes (-) */
  45. void draw_horizontal_line(int x1, int x2, int y) {
  46. // TODO
  47. }
  48.  
  49. /* draw a vertical line (x,y1)-(x,y2) with vertical bars (|) */
  50. void draw_vertical_line(int x, int y1, int y2) {
  51. // TODO
  52. }
  53.  
  54. /* draw a horizontal arrow (x1,y)-(x2,y) */
  55. void draw_horizontal_arrow(int x1, int x2, int y) {
  56. draw_horizontal_line(x1, x2, y);
  57. if (x1 < x2)
  58. draw_pixel(x2, y, '>');
  59. else
  60. draw_pixel(x2, y, '<');
  61. }
  62.  
  63. /* draw a vertical arrow (x,y1)-(x,y2) */
  64. void draw_vertical_arrow(int x, int y1, int y2) {
  65. draw_vertical_line(x, y1, y2);
  66. if (y1 < y2)
  67. draw_pixel(x, y2, 'v');
  68. else
  69. draw_pixel(x, y2, '^');
  70. }
  71.  
  72. /* draw a rectangle (x1,y1)-(x2,y1)-(x2,y2)-(x1,y2) with plus, dashes and vertical bars */
  73. void draw_rectangle(int x1, int y1, int x2, int y2) {
  74. // TODO
  75. }
  76.  
  77. void draw_filled_rectangle(int x1, int y1, int x2, int y2, char c) {
  78. // TODO
  79. }
  80.  
  81. /* draw a general line (x1,y1)-(x2,y2) with character c */
  82. void draw_line(int x1, int y1, int x2, int y2, char c) {
  83. // TODO
  84. }
  85.  
  86. /* flood fill an area starting at given position with character c */
  87. void flood_fill(int x, int y, char c) {
  88. // TODO
  89. }
  90.  
  91.  
  92. /* main program: entry point */
  93.  
  94. int main(int argc, const char *argv[]) {
  95.  
  96. initialize_buffer();
  97.  
  98. draw_rectangle(25, 2, 68, 8);
  99. draw_line(0, NUM_ROWS - 1, NUM_COLUMNS - 1, 0, '*');
  100. flood_fill(2, 2, '.');
  101. draw_pixel(30, 10, '1');
  102. draw_pixel(20, 15, '2');
  103. draw_pixel(75, 20, '3');
  104. draw_line(8, 8, 45, 15, '+');
  105. draw_horizontal_arrow(0, NUM_COLUMNS - 1, 0);
  106. draw_horizontal_arrow(NUM_COLUMNS - 1, NUM_COLUMNS / 2, NUM_ROWS - 2);
  107. draw_vertical_arrow(0, 1, NUM_ROWS - 1);
  108. draw_filled_rectangle(3, 3, 8, 5, '#');
  109.  
  110. update_screen();
  111. return 0;
  112. }
  113.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout