Method: Map#fit

Defined in:
lib/IFMapper/Map.rb

#fitObject

Change map’s width and height to make sure all rooms and connections will fit in map



76
77
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
108
109
110
111
112
113
114
115
116
# File 'lib/IFMapper/Map.rb', line 76

def fit
  # First, adjust map's width and height
  @width = @height = 3
  minXY = []
  maxXY = []

  @sections.each { |section|
    next if section.rooms.empty?

    sizes = section.min_max_rooms
    minXY.push sizes[0]
    maxXY.push sizes[1]

    w = maxXY[-1][0] - minXY[-1][0]
    h = maxXY[-1][1] - minXY[-1][1]

    # We store +3 to allow for complex connections if needed.
    @width  = w + 3 if w >= @width - 2
    @height = h + 3 if h >= @height - 2
  }


  # Okay, minXY[]/maxXY[] contains all the minXY/maxXY positions of
  # each section.  With that info and @weight/@height, we can
  # start shifting all nodes in the section so that they will fit.
  idx = 0
  @sections.each  { |p|
    next if p.rooms.size == 0
    x = y = 0
    x = 1 - minXY[idx][0] if minXY[idx][0] < 1
    y = 1 - minXY[idx][1] if minXY[idx][1] < 1
    x = @width  - maxXY[idx][0] - 2 if maxXY[idx][0] >= @width  - 1
    y = @height - maxXY[idx][1] - 2 if maxXY[idx][1] >= @height - 1
    idx += 1
    next if x == 0 and y == 0 # nothing to shift
    p.rooms.each { |r| 
  r.x += x
  r.y += y
    }
  }
end