Class: ArrayToPoints

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-doom.rb

Instance Method Summary collapse

Constructor Details

#initialize(width, height, raw_data) ⇒ ArrayToPoints

Returns a new instance of ArrayToPoints.



102
103
104
105
106
# File 'lib/ruby-doom.rb', line 102

def initialize(width, height, raw_data)
  @width = width
  @height = height
  @raw_data = raw_data
end

Instance Method Details

#convert(idx, width) ⇒ Object

converts an index into an array of width blah to a point on an x/y coordinate plane with 0,0 in the upper left hand corner so in an array that’s 8 units wide, unit 12 converts to a point 4 across, 1 down



126
127
128
# File 'lib/ruby-doom.rb', line 126

def convert(idx, width)
  Point.new(idx % width, idx/width)
end

#pointsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ruby-doom.rb', line 107

def points
  pts = []
  # for each byte in the image
  idx = 0
  @raw_data.each do |byte|
    # for each bit in the image
    0.upto(7) do |bit|
      if (byte &  128 >> bit) == 0
        tmp_pt = convert(idx, @width)
        pts << Point.new(tmp_pt.x, @height-1-tmp_pt.y)
      end
      idx += 1
    end
  end
  return pts
end