Class: Puzzlize::Cutter
- Inherits:
-
Object
- Object
- Puzzlize::Cutter
- Defined in:
- lib/puzzlize/cutter.rb
Constant Summary collapse
- CONNECTOR_TOP_PATTERN =
[-1, -1 ,1]
- CONNECTOR_RIGHT_PATTERN =
[1, 1, -1]
Instance Attribute Summary collapse
-
#image_height ⇒ Object
Returns the value of attribute image_height.
-
#image_width ⇒ Object
Returns the value of attribute image_width.
-
#piece_size ⇒ Object
Returns the value of attribute piece_size.
Instance Method Summary collapse
- #connector_locations(row_x, row_y) ⇒ Object
- #connector_radius ⇒ Object
- #connector_right(row_x, row_y) ⇒ Object
- #connector_size ⇒ Object
- #connector_top(row_x, row_y) ⇒ Object
- #connector_types(row_x, row_y) ⇒ Object
- #get_connector_locations_matrix ⇒ Object
- #get_piece_matrix ⇒ Object
- #get_rectangle_matrix ⇒ Object
- #horizontal_pieces ⇒ Object
- #piece_fitting_diamentions ⇒ Object
- #piece_points(row_x, row_y, include_connector_size = true) ⇒ Object
-
#real_piece_size ⇒ Object
piece size including all connectors and white space.
- #rectangle_locations(row_x, row_y) ⇒ Object
- #vertical_pieces ⇒ Object
Instance Attribute Details
#image_height ⇒ Object
Returns the value of attribute image_height.
5 6 7 |
# File 'lib/puzzlize/cutter.rb', line 5 def image_height @image_height end |
#image_width ⇒ Object
Returns the value of attribute image_width.
5 6 7 |
# File 'lib/puzzlize/cutter.rb', line 5 def image_width @image_width end |
#piece_size ⇒ Object
Returns the value of attribute piece_size.
5 6 7 |
# File 'lib/puzzlize/cutter.rb', line 5 def piece_size @piece_size end |
Instance Method Details
#connector_locations(row_x, row_y) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/puzzlize/cutter.rb', line 78 def connector_locations(row_x, row_y) points = [] mid = piece_size / 2 width = piece_size height = piece_size cr = connector_radius vertical_position = vertical_pieces - 1 horizontal_position = horizontal_pieces - 1 points << (row_y == 0 ? nil : [mid, 0]) points << (row_y == vertical_position ? nil : [mid, height]) points << (row_x == 0 ? nil : [0, mid]) points << (row_x == horizontal_position ? nil : [width, mid]) if row_y > 0 points[0][1] += cr unless points[0].nil? points[1][1] += cr unless points[1].nil? points[2][1] += cr unless points[2].nil? points[3][1] += cr unless points[3].nil? end if row_x > 0 #use loop points[0][0] += cr unless points[0].nil? points[1][0] += cr unless points[1].nil? points[2][0] += cr unless points[2].nil? points[3][0] += cr unless points[3].nil? end points end |
#connector_radius ⇒ Object
24 25 26 |
# File 'lib/puzzlize/cutter.rb', line 24 def connector_radius return connector_size / 2 end |
#connector_right(row_x, row_y) ⇒ Object
185 186 187 188 189 |
# File 'lib/puzzlize/cutter.rb', line 185 def connector_right(row_x, row_y) # -1 because it's a count vs location starting at 0. return nil if horizontal_pieces - 1 == row_x CONNECTOR_RIGHT_PATTERN[row_x % CONNECTOR_RIGHT_PATTERN.size] end |
#connector_size ⇒ Object
20 21 22 |
# File 'lib/puzzlize/cutter.rb', line 20 def connector_size @connector_size ||= (piece_size * 0.2).to_i end |
#connector_top(row_x, row_y) ⇒ Object
179 180 181 182 183 |
# File 'lib/puzzlize/cutter.rb', line 179 def connector_top(row_x, row_y) # -1 because it's a count vs location starting at 0. return nil if vertical_pieces - 1 == row_y CONNECTOR_TOP_PATTERN[row_x % CONNECTOR_TOP_PATTERN.size] end |
#connector_types(row_x, row_y) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/puzzlize/cutter.rb', line 163 def connector_types(row_x, row_y) # Pattern based connectors # Top and right connector can be anything. We determine this based on a pattern. top = connector_top(row_x, row_y) right = connector_right(row_x, row_y) # Fitter connectors # left and bottom connectors need are fitters. Meaning they might fit to was on top and to the right. # I determine this by shifting to that piece and looking at either the top or right piece and getting the # opposite of that piece. If that piece is at the border we return nil. left = row_x - 1 >= 0 ? (connector_right(row_x -1, row_y) * -1) : nil bottom = row_y - 1 >= 0 ? (connector_top(row_x, row_y - 1) * -1) : nil [bottom, top , left, right] end |
#get_connector_locations_matrix ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/puzzlize/cutter.rb', line 68 def get_connector_locations_matrix matrix = [] vertical_pieces.times do |y_axis| horizontal_pieces.times do |x_axis| matrix << connector_locations(x_axis, y_axis) end end matrix end |
#get_piece_matrix ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/puzzlize/cutter.rb', line 28 def get_piece_matrix matrix = [] vertical_pieces.times do |y_axis| horizontal_pieces.times do |x_axis| matrix << piece_points(x_axis, y_axis) end end matrix end |
#get_rectangle_matrix ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/puzzlize/cutter.rb', line 109 def get_rectangle_matrix matrix = [] vertical_pieces.times do |y_axis| horizontal_pieces.times do |x_axis| matrix << rectangle_locations(x_axis, y_axis) end end matrix end |
#horizontal_pieces ⇒ Object
12 13 14 |
# File 'lib/puzzlize/cutter.rb', line 12 def horizontal_pieces (image_width / piece_size).to_i end |
#piece_fitting_diamentions ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/puzzlize/cutter.rb', line 191 def piece_fitting_diamentions horizontal = horizontal_pieces * piece_size vertical = vertical_pieces * piece_size self.image_width = horizontal self.image_height = vertical [0, 0,horizontal ,vertical ] end |
#piece_points(row_x, row_y, include_connector_size = true) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/puzzlize/cutter.rb', line 38 def piece_points(row_x , row_y, include_connector_size = true) # the pieces should overlap so we have a way to make the connector start_point_x = row_x * piece_size start_point_y = row_y * piece_size # if by adding the connector buffer we have gone passed the edge of the image # go back to the edge of the image. piece_width = piece_size piece_height = piece_size end_point_x = start_point_x + piece_width end_point_y = start_point_y + piece_height #translating number of pieces to positions horizontal_position = horizontal_pieces - 1 vertical_position = vertical_pieces - 1 if include_connector_size start_point_x -= connector_radius if row_x > 0 start_point_y -= connector_radius if row_y > 0 # if the piece are not on the edges, they are bigger by the radius of the connector. piece_width += connector_radius if row_x < horizontal_position piece_height += connector_radius if row_y < vertical_position piece_width += connector_radius if row_x > 0 piece_height += connector_radius if row_y > 0 end [start_point_x, start_point_y, piece_width, piece_height] end |
#real_piece_size ⇒ Object
piece size including all connectors and white space
8 9 10 |
# File 'lib/puzzlize/cutter.rb', line 8 def real_piece_size piece_size + connector_size end |
#rectangle_locations(row_x, row_y) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/puzzlize/cutter.rb', line 119 def rectangle_locations(row_x, row_y) points = [] width = piece_size height = piece_size cr = connector_radius vertical_position = vertical_pieces - 1 horizontal_position = horizontal_pieces - 1 points << (row_y == 0 ? nil : [0,0, width, cr]) points << (row_y == vertical_position ? nil : [0,height, width, height + cr]) points << (row_x == 0 ? nil : [0,0, cr, height]) points << (row_x == horizontal_position ? nil : [width, 0, width + cr, height]) if row_x > 0 points[0][2] += cr unless points[0].nil? points[1][2] += cr unless points[1].nil? points[3][0] += cr unless points[3].nil? points[3][2] += cr unless points[3].nil? end if row_x < horizontal_position points[0][2] += cr unless points[0].nil? points[1][2] += cr unless points[1].nil? end if row_y > 0 points[1][1] += cr unless points[1].nil? points[1][3] += cr unless points[1].nil? points[2][3] += cr unless points[2].nil? points[3][3] += cr unless points[3].nil? end if row_y < vertical_position points[2][3] += cr unless points[2].nil? points[3][3] += cr unless points[3].nil? end points end |
#vertical_pieces ⇒ Object
16 17 18 |
# File 'lib/puzzlize/cutter.rb', line 16 def vertical_pieces (image_height / piece_size).to_i end |