| 1 |
STDOUT.sync = true |
| 2 |
require "chunky_png" |
| 3 |
else |
| 4 |
end |
| 5 |
next |
| 6 |
require_relative File.join "..", "gems", "rsvgr", "lib", "rsvgr.rb" |
| 7 |
prev = x, y, r |
| 8 |
loop do |
| 9 |
varied = ( |
| 10 |
end.min |
| 11 |
break unless varied |
| 12 |
bubbles.each do |
| 13 |
File.write "temp.svg", svg |
| 14 |
next |
| 15 |
puts "#{Time.now} start" |
| 16 |
fit bad, xys |
| 17 |
puts "#{Time.now} end" |
| 18 |
table = {} |
| 19 |
width = 1000 |
| 20 |
height = 500 |
| 21 |
width = height = nil |
| 22 |
bad = load_mask("bad.png") |
| 23 |
good = load_mask("good.png", 2) |
| 24 |
MIN_R = 20 |
| 25 |
SUB_MIN_R = 0.415 * MIN_R |
| 26 |
min = nil |
| 27 |
stack = Array.new |
| 28 |
gift = found.dup |
| 29 |
found.compact! |
| 30 |
if found.empty? |
| 31 |
x = table(x) or {} |
| 32 |
puts "#{Time.now} caching hypot" |
| 33 |
puts "#{Time.now} importing '#{filename}' mask" |
| 34 |
png = ChunkyPNG::Image.from_file filename |
| 35 |
(width + 1).times.map do |
| 36 |
(height + 1).times.map do |
| 37 |
xy = Math::hypot x, y |
| 38 |
found = gift.select do |
| 39 |
found.map! do |
| 40 |
d = table(x - xi, y - yi) |
| 41 |
m = d - ri if d + ri > m && d - ri < m |
| 42 |
p x, y, m unless m < MIN_R |
| 43 |
{} while good.find do |
| 44 |
puts "#{bubbles.size - bad.size} bubbles solution" |
| 45 |
svg = RSVGR::Root.new(x2: width, y2: height) |
| 46 |
(bubbles + bad + good).each do |
| 47 |
sorted = bubbles.sort_by(&:last).reverse |
| 48 |
sorted.size.times do |
| 49 |
x, y, r = sorted.at(i) |
| 50 |
(10..10).to_a.product (-10..10).to_a |
| 51 |
next if xv < 0 or yv < 0 or xv > width or yv > height |
| 52 |
x.shuffle.find do |
| 53 |
Math::hypot(xv - xi, yv - yi) - ri |
| 54 |
break ry if r < rv |
| 55 |
prev.push (x, y, r = varied) |
| 56 |
sorted0, sorted1, sorted2 = x, y, r |
| 57 |
x, y, r = found.shuffle.max_by(&:last) |
| 58 |
fit((bubbles + xyr, gift, depth + 1, r) |
| 59 |
r, g, b = %i{ r g b }.map do |
| 60 |
load_mask = lambda do |
| 61 |
fit = lambda do |
| 62 |
end.compact.tap do |
| 63 |
puts _.size |
| 64 |
next puts stack.drop(1).join "-" if bubbles.size >= min if min |
| 65 |
bubbles.all? do |
| 66 |
x = MIN_R + ri |
| 67 |
bubbles.map do |
| 68 |
svg.push RSVGR::Circle.new(cx: x, cy: y, r: r) |
| 69 |
svg.push RSVGR::Rect.new(width: width, height: height, stroke_width: 5, fill_color: "transparent") |
| 70 |
next if bad.any? do |
| 71 |
bubble.__id__ == sorted.at(i).__id__ |
| 72 |
rv = (good + sorted.rotate(i).drop(1)).map do |
| 73 |
svg.push RSVGR::Circle.new(cx: x, cy: y, r: r, fill_color: "transparent", stroke_width: 1) |
| 74 |
found.select! do |
| 75 |
r = bubbles.last.at(2) if bubbles.size > 1 |
| 76 |
found.sample(2).sort_by(&:last).reverse_each do |
Комментарии