1- module Tiler
2- @acute = false
1+ # frozen_string_literal: true
2+ # Tile factory
3+ class TileFactory
4+ attr_reader :acute
35
4- def self . acute ( x )
5- @acute = x
6+ def initialize ( acute = false )
7+ @acute = acute
68 end
79
810 # setup the initial tiling with all red tiles
9- def self . tile ( a , b , c )
10- tile = @acute ? ATile . new ( 0 , a , b , c ) : Tile . new ( 0 , a , b , c )
11+ def tile ( a , b , c )
12+ return ATile . new ( 0 , a , b , c ) if acute
13+ Tile . new ( 0 , a , b , c )
1114 end
1215end
1316
17+ # Regular Tile class
1418class Tile
1519 include Processing ::Proxy
1620 PHI = ( 1.0 + Math . sqrt ( 5 ) ) / 2.0 # golden ratio
17- RED = [ 255 , 0 , 0 ]
18- BLUE = [ 0 , 0 , 255 ]
19- COLORS = [ RED , BLUE ]
21+ COLORS = %w( #ff0000 #0000ff ) . freeze # red blue
2022 attr_reader :a , :b , :c , :col
2123
22- def initialize ( col , a , b , c )
24+ def initialize ( col , a , b , c )
2325 @col , @a , @b , @c = col , a , b , c
2426 end
2527
2628 def display
2729 no_stroke
28- fill ( * COLORS [ col ] )
30+ fill ( color ( COLORS [ col ] ) )
2931 triangle ( a . x , a . y , b . x , b . y , c . x , c . y )
3032 # fill(0, 0, 255)
3133 # ellipse(a.x, a.y, 4,4)
@@ -35,7 +37,7 @@ def display
3537
3638 def subdivide
3739 result = [ ]
38- if ( col == 0 )
40+ if col . zero?
3941 # Subdivide red triangle
4042 p = b - a
4143 p /= PHI
@@ -58,10 +60,11 @@ def subdivide
5860 end
5961end
6062
63+ # Acute Tile class
6164class ATile < Tile
6265 def subdivide
6366 result = [ ]
64- if ( col == 0 )
67+ if col . zero?
6568 # Subdivide red (half kite) triangle
6669 q = b - a
6770 q /= PHI
@@ -80,6 +83,6 @@ def subdivide
8083 result << ATile . new ( 1 , b , p , a )
8184 result << ATile . new ( 0 , p , c , b )
8285 end
83- result
86+ result
8487 end
8588end
0 commit comments