brewersfan1976

draw_rectangle.rb

Oct 18th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.02 KB | None | 0 0
  1. def drawRectangle(canvas, rectangle)
  2.     x = rectangle[1]
  3.     y = rectangle[0]
  4.    
  5.     # draw top
  6.    
  7.     canvas[rectangle[1]][rectangle[0]] = '*'
  8.     y = y + 1
  9.    
  10.     while (y <= rectangle[2] - 1)
  11.           canvas[x][y] = '-'
  12.           y = y + 1
  13.     end
  14.    
  15.     canvas[x][y] = '*'
  16.     y = rectangle[1]
  17.     x = x + 1
  18.    
  19.     # draw left side
  20.  
  21.     if rectangle[3] - rectangle[1] != 1 then        
  22.        while (x <= rectangle[3] - 1)
  23.              canvas[x][y] = '|'
  24.              x = x + 1
  25.        end
  26.    
  27.        # draw right side
  28.    
  29.        x = rectangle[0] + 1
  30.        y = rectangle[2]
  31.    
  32.        while (x <= rectangle[3] - 1)
  33.              canvas[x][y] = '|'
  34.              x = x + 1
  35.        end
  36.     end
  37.        
  38.     # draw bottom
  39.    
  40.     canvas[rectangle[3]][rectangle[0]] = '*'
  41.     x = rectangle[3]
  42.     y = rectangle[0]
  43.     y = y + 1
  44.    
  45.     while (y <= rectangle[2] - 1)
  46.           canvas[x][y] = '-'
  47.           y = y + 1
  48.     end
  49.    
  50.     canvas[x][y] = '*'
  51.    
  52.     return canvas
  53. end
Advertisement
Add Comment
Please, Sign In to add comment