#!/usr/bin/env ruby # frozen_string_literal: true # Write a TikZ picture of the optimal polygon for each given k. # # paper_polys.rb SIZE_CM K1 K2 ... # # Reads out/polys_10000/_poly.txt (k <= 10000) or out/poly_.txt and # writes paper/figs/poly_.tex. Lattice points of the (enlarged) bounding # box are drawn when the box is small: covered points dark, others light. ROOT = File.expand_path('..', __dir__) def vertex_file(k) [File.join(ROOT, 'out', 'polys_10000', format('%07d_poly.txt', k)), File.join(ROOT, 'out', "poly_#{k}.txt")].find { |f| File.exist?(f) } || abort("no polygon file for k=#{k}") end def cross(o, a, b) = ((a[0] - o[0]) * (b[1] - o[1])) - ((a[1] - o[1]) * (b[0] - o[0])) def covered?(verts, pt) verts.each_index.all? { |i| cross(verts[i], verts[(i + 1) % verts.size], pt) >= 0 } end def lattice_dots(verts, box, unit) xs, ys = box dots = [] ((xs.min - 1)..(xs.max + 1)).each do |x| ((ys.min - 1)..(ys.max + 1)).each do |y| style = covered?(verts, [x, y]) ? 'black!75' : 'black!20' dots << format('\fill[%s] (%.4f,%.4f) circle (%.3fcm);', style, x * unit, y * unit, [0.035, unit * 0.09].min) end end dots end def picture(k, size) verts = File.readlines(vertex_file(k)).grep_v(/^#/).map { |l| l.split.map(&:to_i) } xs = verts.map(&:first) ys = verts.map(&:last) span = [xs.max - xs.min, ys.max - ys.min, 1].max + (verts.size < 60 ? 2 : 0) unit = size / span.to_f path = verts.map { |x, y| format('(%.4f,%.4f)', x * unit, y * unit) }.join(' -- ') out = ['\begin{tikzpicture}'] out << "\\filldraw[fill=blue!10, draw=blue!60!black, line width=0.5pt, line join=round] #{path} -- cycle;" out.concat(lattice_dots(verts, [xs, ys], unit)) if span <= 40 r = [0.06, unit * 0.18].min verts.each { |x, y| out << format('\fill[orange!85!black] (%.4f,%.4f) circle (%.3fcm);', x * unit, y * unit, r) } cx = (xs.min + xs.max) / 2.0 * unit out << format('\node[anchor=north] at (%.4f,%.4f) {\small $k=%s$};', cx, (ys.min * unit) - 0.25, k.to_s.gsub(/(\d)(?=(\d{3})+$)/, '\1{,}')) out << '\end{tikzpicture}' out.join("\n") end size, *ks = ARGV abort('usage: paper_polys.rb SIZE_CM K...') if ks.empty? dir = File.join(ROOT, 'paper', 'figs') Dir.mkdir(dir) unless Dir.exist?(dir) ks.each do |k| File.write(File.join(dir, "poly_#{k}.tex"), picture(Integer(k), Float(size))) end puts "wrote #{ks.size} pictures to #{dir}"