table management fixes
This commit is contained in:
+20
-33
@@ -1,5 +1,6 @@
|
||||
class Section
|
||||
include SimplyStored::Couch
|
||||
include Qwaiter::Distribution
|
||||
|
||||
property :title
|
||||
property :path, type: Array, default: []
|
||||
@@ -77,50 +78,45 @@ class Section
|
||||
w = width
|
||||
h = height
|
||||
n = tables.size
|
||||
return unless w > 0 && h > 0 && n > 0
|
||||
l = Math.sqrt((w*h).to_f/n)
|
||||
epsilon = (10 ** -(Float::DIG - 1)).to_f
|
||||
while (w/l).floor * (h/l).floor < n # find a fitting combination
|
||||
l = if w.remainder(l) < epsilon then (h/l).ceil
|
||||
elsif h.remainder(l) < epsilon then (w/l).ceil
|
||||
elsif w.remainder(l) < epsilon && h.remainder(l) < epsilon then [w / (w/l).ceil.succ, h / (h/l).ceil.succ].max # Failsafe for when both are valid and it still does not fit
|
||||
else [w / (w/l).ceil, h / (h/l).ceil].max end
|
||||
end
|
||||
x0 = w.remainder(l - epsilon)/2 # Start with half a remainder space, will end with the other halve
|
||||
return false unless w > 0 && h > 0 && n > 0
|
||||
lx, ly = distribute_lattice(w, h, n)
|
||||
x0 = lx/2
|
||||
x = x0
|
||||
y = h.remainder(l - epsilon)/2
|
||||
y = ly/2
|
||||
saves = []
|
||||
for table in tables.sort_by(&:number)
|
||||
if x + l > w + (1e4*epsilon) # New row, error = epsilon times possible tables
|
||||
if x > w + (1e4*epsilon) # New row, error = epsilon times possible tables in a row
|
||||
x = x0
|
||||
y += l
|
||||
y += ly
|
||||
end
|
||||
table.position_x = x + (l/2) - (table_width/2) # Starting point of square + half the square (center) minus half the table size
|
||||
table.position_y = y + (l/2) - (table_height/2)
|
||||
table.position_x = x - (table.width/2) # Starting point of square + half the square (center) minus half the table size
|
||||
table.position_y = y - (table.height/2)
|
||||
saves << table.save
|
||||
x += l
|
||||
x += lx
|
||||
end
|
||||
[l, saves.all?]
|
||||
saves.all?
|
||||
end
|
||||
|
||||
def arrange_tables_in_rows_of(n)
|
||||
return unless n.present?
|
||||
return false unless n.present?
|
||||
n = n.to_i
|
||||
return unless n > 0
|
||||
return false unless n > 0
|
||||
dx = width / n
|
||||
dy = height / (tables.size.to_f/n).ceil
|
||||
x = 0.0
|
||||
y = 0.0
|
||||
saves = []
|
||||
tables.sort_by(&:number).each.with_index do |table, i|
|
||||
table.position_x = x + (dx/2) - (table_width/2)
|
||||
table.position_y = y + (dy/2) - (table_height/2)
|
||||
table.position_x = x + (dx/2) - (table.width/2)
|
||||
table.position_y = y + (dy/2) - (table.height/2)
|
||||
x += dx
|
||||
if (i + 1).multiple_of?( n )
|
||||
x = 0.0
|
||||
y += dy
|
||||
end
|
||||
table.save
|
||||
saves << table.save
|
||||
end
|
||||
return saves.all?
|
||||
end
|
||||
def arrange_tables_in_columns_of(n)
|
||||
return unless n.present?
|
||||
@@ -131,8 +127,8 @@ class Section
|
||||
x = 0.0
|
||||
y = 0.0
|
||||
tables.sort_by(&:number).each.with_index do |table, i|
|
||||
table.position_x = x + (dx/2) - (table_width/2)
|
||||
table.position_y = y + (dy/2) - (table_height/2)
|
||||
table.position_x = x + (dx/2) - (table.width/2)
|
||||
table.position_y = y + (dy/2) - (table.height/2)
|
||||
y += dy
|
||||
if (i + 1).multiple_of?( n )
|
||||
y = 0.0
|
||||
@@ -142,13 +138,4 @@ class Section
|
||||
end
|
||||
end
|
||||
|
||||
# Method returning the sections table width
|
||||
def table_width
|
||||
2.0
|
||||
end
|
||||
|
||||
# Method returning the sections table height
|
||||
def table_height
|
||||
2.0
|
||||
end
|
||||
end
|
||||
|
||||
@@ -36,4 +36,13 @@ class Table
|
||||
number
|
||||
end
|
||||
|
||||
# Method returning the sections table width
|
||||
def width
|
||||
2.0
|
||||
end
|
||||
|
||||
# Method returning the sections table height
|
||||
def height
|
||||
2.0
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user