Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <cab202_graphics.h>
  3.  
  4. // (a) Insert the function signature for a function called fill_rect
  5. // which returns no value, but which has the following parameters:
  6. // left - an integer that specifies the horizontal location of
  7. // the left hand side of the rectangle.
  8. // top - an integer that specifies the vertical location of
  9. // the upper edge of the rectangle.
  10. // right - an integer that specifies the horizontal location of
  11. // the right hand side of the rectangle.
  12. // bottom - an integer that specifies the vertical location of
  13. // the lower edge of the rectangle.
  14. // ch = a character value that will be used to draw the
  15. // display the rectangle.
  16. void fill_rect(int left, int top, int right, int bottom, char ch) {
  17.  
  18. // (b) Test to see if either the width or height of the rectangle is less
  19. // than 1. If so, the function should stop immediately and draw nothing.
  20. if (( right-left ) >= 0 && ( bottom-top ) >= 0 ) {
  21.  
  22. // (c) Set up an integer variable called y_ctr which will be used to
  23. // iterate from the top of the rectangle to the bottom of the rectangle.
  24. int y_ctr = top;
  25.  
  26. // (d) Enter a loop that will use y_ctr to iterate over each row of the
  27. // rectangle.
  28. while ( y_ctr <= bottom ) {
  29.  
  30. // (e) Draw a horizontal line from the left edge of the rectangle to
  31. // the right edge of the rectangle, in the row defined by y_ctr.
  32. draw_line( left, y_ctr, right, y_ctr, ch );
  33.  
  34. // (f) Increment y_ctr.
  35. y_ctr += 1;
  36.  
  37. }// (g) End the loop.
  38. }
  39. }// (h) End the function.
  40.  
  41. void hrule( void );
  42. void vrule( void );
  43.  
  44. int main( void ) {
  45. setup_screen();
  46.  
  47. // draw a box.
  48. int left = 2 + rand() % ( screen_width() - 2 ) / 2;
  49. int top = 2 + rand() % ( screen_height() - 2 ) / 2;
  50. int right = left + 1 + rand() % ( screen_width() - left - 1 );
  51. int bottom = top + 1 + rand() % ( screen_height() - top - 1 );
  52. int ch = '@';
  53. clear_screen();
  54. hrule();
  55. vrule();
  56. draw_formatted( 5, 1,
  57. "left: %4d, upper: %4d, right: %4d, lower: %4d - Press key to continue...",
  58. left, top, right, bottom
  59. );
  60. fill_rect( left, top, right, bottom, ch );
  61. show_screen();
  62. wait_char();
  63.  
  64. // draw a box.
  65. left = 2 + rand() % ( screen_width() - 2 ) / 2;
  66. top = 2 + rand() % ( screen_height() - 2 ) / 2;
  67. right = left + 1 + rand() % ( screen_width() - left - 1 );
  68. bottom = top + 1 + rand() % ( screen_height() - top - 1 );
  69. ch = '&';
  70. clear_screen();
  71. hrule();
  72. vrule();
  73. draw_formatted( 5, 1,
  74. "left: %4d, upper: %4d, right: %4d, lower: %4d - Press key to continue...",
  75. left, top, right, bottom
  76. );
  77. fill_rect( left, top, right, bottom, ch );
  78. show_screen();
  79. wait_char();
  80.  
  81. // draw a box with zero width.
  82. left = 2 + rand() % ( screen_width() - 2 ) / 2;
  83. top = 2 + rand() % ( screen_height() - 2 ) / 2;
  84. right = left - 1;
  85. bottom = top + 1 + rand() % ( screen_height() - top - 1 );
  86. ch = '*';
  87. clear_screen();
  88. hrule();
  89. vrule();
  90. draw_formatted( 5, 1,
  91. "left: %4d, upper: %4d, right: %4d, lower: %4d - Press key to continue...",
  92. left, top, right, bottom
  93. );
  94. fill_rect( left, top, right, bottom, ch );
  95. show_screen();
  96. wait_char();
  97.  
  98. // draw a box.
  99. left = 2 + rand() % ( screen_width() - 2 ) / 2;
  100. top = 2 + rand() % ( screen_height() - 2 ) / 2;
  101. right = left + 1 + rand() % ( screen_width() - left - 1 );
  102. bottom = top + 1 + rand() % ( screen_height() - top - 1 );
  103. ch = '#';
  104. clear_screen();
  105. hrule();
  106. vrule();
  107. draw_formatted( 5, 1,
  108. "left: %4d, upper: %4d, right: %4d, lower: %4d - Press key to continue...",
  109. left, top, right, bottom
  110. );
  111. fill_rect( left, top, right, bottom, ch );
  112. show_screen();
  113. wait_char();
  114.  
  115. // draw a box with negative width.
  116. left = 2 + rand() % ( screen_width() - 2 ) / 2;
  117. top = 2 + rand() % ( screen_height() - 2 ) / 2;
  118. right = left - 2;
  119. bottom = top + 1 + rand() % ( screen_height() - top - 1 );
  120. ch = '!';
  121. clear_screen();
  122. hrule();
  123. vrule();
  124. draw_formatted( 5, 1,
  125. "left: %4d, upper: %4d, right: %4d, lower: %4d - Press key to continue...",
  126. left, top, right, bottom
  127. );
  128. fill_rect( left, top, right, bottom, ch );
  129. show_screen();
  130. wait_char();
  131.  
  132. // draw a box.
  133. left = 2 + rand() % ( screen_width() - 2 ) / 2;
  134. top = 2 + rand() % ( screen_height() - 2 ) / 2;
  135. right = left + 1 + rand() % ( screen_width() - left - 1 );
  136. bottom = top + 1 + rand() % ( screen_height() - top - 1 );
  137. ch = '+';
  138. clear_screen();
  139. hrule();
  140. vrule();
  141. draw_formatted( 5, 1,
  142. "left: %4d, upper: %4d, right: %4d, lower: %4d - Press key to continue...",
  143. left, top, right, bottom
  144. );
  145. fill_rect( left, top, right, bottom, ch );
  146. show_screen();
  147. wait_char();
  148.  
  149. // draw a box with zero height.
  150. left = 2 + rand() % ( screen_width() - 2 ) / 2;
  151. top = 2 + rand() % ( screen_height() - 2 ) / 2;
  152. right = left + 1 + rand() % ( screen_width() - left - 1 );
  153. bottom = top - 1;
  154. ch = 'a';
  155. clear_screen();
  156. hrule();
  157. vrule();
  158. draw_formatted( 5, 1,
  159. "left: %4d, upper: %4d, right: %4d, lower: %4d - Press key to continue...",
  160. left, top, right, bottom
  161. );
  162. fill_rect( left, top, right, bottom, ch );
  163. show_screen();
  164. wait_char();
  165.  
  166. // draw a box.
  167. left = 2 + rand() % ( screen_width() - 2 ) / 2;
  168. top = 2 + rand() % ( screen_height() - 2 ) / 2;
  169. right = left + 1 + rand() % ( screen_width() - left - 1 );
  170. bottom = top + 1 + rand() % ( screen_height() - top - 1 );
  171. ch = 'b';
  172. clear_screen();
  173. hrule();
  174. vrule();
  175. draw_formatted( 5, 1,
  176. "left: %4d, upper: %4d, right: %4d, lower: %4d - Press key to continue...",
  177. left, top, right, bottom
  178. );
  179. fill_rect( left, top, right, bottom, ch );
  180. show_screen();
  181. wait_char();
  182.  
  183. // draw a box with negative right.
  184. left = 2 + rand() % ( screen_width() - 2 ) / 2;
  185. top = 2 + rand() % ( screen_height() - 2 ) / 2;
  186. right = left + 1 + rand() % ( screen_width() - left - 1 );
  187. bottom = top - 2;
  188. ch = 'c';
  189. clear_screen();
  190. hrule();
  191. vrule();
  192. draw_formatted( 5, 1,
  193. "left: %4d, upper: %4d, right: %4d, lower: %4d - Press key to continue...",
  194. left, top, right, bottom
  195. );
  196. fill_rect( left, top, right, bottom, ch );
  197. show_screen();
  198. wait_char();
  199.  
  200. // draw a box.
  201. left = 2 + rand() % ( screen_width() - 2 ) / 2;
  202. top = 2 + rand() % ( screen_height() - 2 ) / 2;
  203. right = left + 1 + rand() % ( screen_width() - left - 1 );
  204. bottom = top + 1 + rand() % ( screen_height() - top - 1 );
  205. ch = 'd';
  206. clear_screen();
  207. hrule();
  208. vrule();
  209. draw_formatted( 5, 1,
  210. "left: %4d, upper: %4d, right: %4d, lower: %4d - Press key to continue...",
  211. left, top, right, bottom
  212. );
  213. fill_rect( left, top, right, bottom, ch );
  214. show_screen();
  215. wait_char();
  216.  
  217. cleanup_screen();
  218. return 0;
  219. }
  220.  
  221. void hrule( void ) {
  222. for ( int x = 0; x < screen_width(); x++ ) {
  223. draw_char( x, 0, '0' + ( x % 10 ) );
  224. }
  225. }
  226.  
  227. void vrule( void ) {
  228. for ( int y = 0; y < screen_height(); y++ ) {
  229. draw_char( 0, y, '0' + ( y % 10 ) );
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement