|
| 1 | +require 'picrate' |
| 2 | + |
| 3 | +# After a sketch by Jim Bumgardner |
| 4 | +# http://joyofprocessing.com/blog/tag/tilings/page/2/ |
| 5 | +# Here we create an array of offscreen images |
| 6 | +# Create a grid using the ruby-processing :grid method |
| 7 | +# Then randomly render tile images from the array |
| 8 | +class TruchetTiles < Processing::App |
| 9 | + DIM = 24 |
| 10 | + TYPE = %i[corner alt_corner alt_oval oval alt_line line].freeze |
| 11 | + attr_reader :tiles, :images |
| 12 | + |
| 13 | + def make_image(dim, alternate = :line) |
| 14 | + create_graphics(dim, dim, P2D).tap do |g| |
| 15 | + g.smooth(4) |
| 16 | + g.begin_draw |
| 17 | + g.background(255) |
| 18 | + g.stroke(0) |
| 19 | + g.stroke_weight(4) |
| 20 | + g.no_fill |
| 21 | + g.ellipse_mode(RADIUS) |
| 22 | + case alternate |
| 23 | + when :corner |
| 24 | + g.line(dim / 2, 0, dim / 2, dim / 2) |
| 25 | + g.line(0, dim / 2, dim / 2, dim / 2) |
| 26 | + g.line(dim / 2, dim, dim, dim / 2) |
| 27 | + when :alt_corner |
| 28 | + g.line(dim / 2, dim, dim / 2, dim / 2) |
| 29 | + g.line(dim, dim / 2, dim / 2, dim / 2) |
| 30 | + g.line(0, dim / 2, dim / 2, 0) |
| 31 | + when :line |
| 32 | + g.line(dim / 2, 0, dim, dim / 2) |
| 33 | + g.line(0, dim / 2, dim / 2, dim) |
| 34 | + when :alt_line |
| 35 | + g.line(0, dim / 2, dim / 2, 0) |
| 36 | + g.line(dim / 2, dim, dim, dim / 2) |
| 37 | + when :alt_oval |
| 38 | + g.ellipse(0, dim, dim / 2, dim / 2) |
| 39 | + g.ellipse(dim, 0, dim / 2, dim / 2) |
| 40 | + when :oval |
| 41 | + g.ellipse(0, 0, dim / 2, dim / 2) |
| 42 | + g.ellipse(dim, dim, dim / 2, dim / 2) |
| 43 | + end |
| 44 | + g.end_draw |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + def setup |
| 49 | + sketch_title 'Mixed Truchet Tiling' |
| 50 | + @images = TYPE.map { |type| make_image(DIM, type) } |
| 51 | + @tiles = [] |
| 52 | + grid(width, height, DIM, DIM) do |posx, posy| |
| 53 | + tiles << Tile.new(Vec2D.new(posx, posy)) |
| 54 | + end |
| 55 | + no_loop |
| 56 | + end |
| 57 | + |
| 58 | + def draw |
| 59 | + background(255) |
| 60 | + tiles.each(&:render) |
| 61 | + end |
| 62 | + |
| 63 | + def settings |
| 64 | + size(576, 576, P2D) |
| 65 | + smooth(4) |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +TruchetTiles.new |
| 70 | + |
| 71 | +# encapsulate Tile as a class |
| 72 | +class Tile |
| 73 | + include Processing::Proxy |
| 74 | + attr_reader :vec, :img |
| 75 | + |
| 76 | + def initialize(vec) |
| 77 | + @vec = vec |
| 78 | + @img = images.sample |
| 79 | + end |
| 80 | + |
| 81 | + def render |
| 82 | + image(img, vec.x, vec.y, img.width, img.height) |
| 83 | + end |
| 84 | +end |
0 commit comments