Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. require_relative 'pixel'
  2.  
  3. class Screen
  4. attr_accessor :width
  5. attr_accessor :height
  6. attr_accessor :matrix
  7.  
  8. def initialize(width, height)
  9. self.width = width
  10. self.height = height
  11. self.matrix = Array.new(self.width) { Array.new(self.height) }
  12. end
  13.  
  14. # Insert a Pixel at x, y
  15. def insert(pixel, x, y)
  16. valid_coordinate = inbounds(x,y)
  17. self.matrix.insert([x][y], pixel) if !valid_coordinate.nil?
  18. end
  19.  
  20. def at(x, y)
  21. valid_coordinate = inbounds(x,y)
  22. self.matrix[x][y] if !valid_coordinate.nil?
  23. end
  24.  
  25. private
  26.  
  27. def inbounds(x, y)
  28. if (x < self.width && x >= 0) && (y < self.height && y >= 0)
  29. return x
  30. return y
  31. else
  32. nil
  33. end
  34. end
  35.  
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement