Skip to content

Instantly share code, notes, and snippets.

@tensiondriven
Created June 17, 2012 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tensiondriven/2945452 to your computer and use it in GitHub Desktop.
Save tensiondriven/2945452 to your computer and use it in GitHub Desktop.
Objectiveli simple frame rules
def frametest
Log.log ''
Log.log ''
Log.log ''
Log.log ''
Log.log 'xx start'
frames, counts = get_flat_goals_array
@width = 640
@height = 480
# get totals
counts = [0, 0, 0]
frames.each do |frame|
counts[frame.priority - 1] = counts[frame.priority - 1] + 1
end
a0 = @width * @height / ( counts[0].to_f / 1 + counts[1].to_f / 2 + counts[2].to_f / 3)
@counts = counts
@frames = draw_frameset @width, @height, 0, 0, 0, frames, 0, @width * @height, a0
Log.log @frames.inspect
Log.log 'xx complete'
render 'frametest', :layout => false
end
def draw_frameset tw, th, ow, oh, p = 0, frames, iteration, total_area, a0
Log.log ''
# calculate areas
a = []
a[0] = a0
a[1] = a[0] / 2
a[2] = a[0] / 3
# pop
#counts.each_with_index do |item, index|
# Log.log "#{item} items at index #{index} #{counts[p]} #{p}"
#end
frame = frames.shift
# calculate aspect ratio (find best fit)
Log.log "Total area: #{total_area}"
Log.log "Box area: #{(a[frame.priority - 1])}"
if tw/th > th/tw
Log.log "Available area is wider than it is tall"
h = th
w = a[frame.priority - 1] / th
o = {
:w => w.to_i, :h => h.to_i,
:ow => ow.to_i, :oh => oh.to_i,
:tw => tw.to_i, :th => th.to_i,
:title => "#{frame.priority}", :it => iteration,
:a => (w*h).to_i
}
ohd = 0
owd = w
else
Log.log "Available area is taller than it is wide"
h = a[frame.priority - 1] / tw
w = tw
o = {
:w => w.to_i, :h => h.to_i,
:ow => ow.to_i, :oh => oh.to_i,
:tw => tw.to_i, :th => th.to_i,
:title => "#{frame.priority}", :it => iteration,
:a => (w*h).to_i
}
ohd = h
owd = 0
end
# calculate box
# subtract box from area
Log.log "P#{p} w,h = #{h},#{w}"
w = tw == w ? tw : tw - w
h = th == h ? th : th - h
new_area = w * h
Log.log "newA: #{new_area} (#{w} x #{h})"
Log.log "box: #{o.inspect}"
if frames.count > 0 #&& iteration < 2
Log.log ('-- recursing (counts[p] > 0)')
return [o].concat draw_frameset w, h, ow + owd, oh + ohd, p, frames, iteration + 1, total_area, a0
else
Log.log ('-- returning (counts[p] == 0)')
return [o]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment