Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ before_install:
- rm Gemfile.lock
rvm:
- 1.9.3
- 2.2.0
- 2.2
- 2.3
- 2.4
- 2.5
- jruby-19mode
- rbx-2
script: bundle exec rspec spec
11 changes: 6 additions & 5 deletions lib/bin_packing/box.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
module BinPacking
class Box
attr_accessor :width, :height, :x, :y, :packed
attr_accessor :width, :height, :x, :y, :packed, :can_rotate

def initialize(width, height)
@width = width
@height = height
@x = 0
@y = 0
@packed = false
@can_rotate = true
end

def area
@area ||= @width * @height
end

def rotate
@width, @height = [@height, @width]
end

def packed?
@packed
end

def can_rotate?
@can_rotate
end

def label
"#{@width}x#{@height} at [#{@x},#{@y}]"
end
Expand Down
5 changes: 4 additions & 1 deletion lib/bin_packing/heuristics/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ def find_position_for_new_node!(box, free_rectangles)

free_rectangles.each do |free_rect|
try_place_rect_in(free_rect, box, width, height, best_score)
try_place_rect_in(free_rect, box, height, width, best_score)

if box.can_rotate?
try_place_rect_in(free_rect, box, height, width, best_score)
end
end

best_score
Expand Down
20 changes: 20 additions & 0 deletions spec/bin_packing/packer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@
expect(packer.pack([box]).size).to be 1
expect(packer.pack([box]).size).to be 0
end

it 'respects the can_rotate property' do
bin = bin_of_size_1
box = BinPacking::Box.new(1_000, 9_000)
box.can_rotate = false
packer = BinPacking::Packer.new([bin])
expect(packer.pack([box]).size).to be 0
expect(bin.boxes.size).to be 0
expect(box.width).to be 1_000
expect(box.height).to be 9_000
expect(box.packed?).to be false

bin = bin_of_size_1
box = BinPacking::Box.new(1_000, 9_000)
box.can_rotate = true
packer = BinPacking::Packer.new([bin])
expect(packer.pack([box]).size).to be 1
expect(bin.boxes.size).to be 1
expect(box.packed?).to be true
end
end

describe '#pack!' do
Expand Down