wang

0.1.0 • Public • Published

Wang tiles!

This is a simple implementation of the wang tiling algorithm presented in Tilings and Patterns by Grunbaum and Shephard, and discussed in Aperiodic Texture Mapping by Jos Stam.

The algorithm generates an aperiodic tiling of the following 16 tiles:

Tiles

The tiles use 6 colors, shown here as red, yellow, cyan, purple, green and grey.

The tiles aren't uniformly distributed in the resulting image. This is their frequency distribution:

Tile Frequency

And the colors aren't evenly distributed either. Here's their frequency distribution:

Color Frequency

Usage

The wang(width, height, [seed]) function returns a Uint8Array of width x height tile indexes. The returned array starts with the first row, then the next row, and so on. Each tile index is an integer from 0-15, corresponding to a tile in the diagram above.

// If you're using node...
var wang = require('./wang').wang
 
// Generate 2 rows of 5 wang tiles
var width = 5;
var height = 3;
var arr = wang(width, height); // width, height
 
// Print out the tile indexes
for(var y = 0; y < height; y++) {
  for(var x = 0; x < width; x++) {
    console.log("At "+x+","++ "", arr[+ y * width]);
  }
}

Here's the output:

$ node test.js
At 0,0:  11
At 1,0:  3
At 2,0:  1
At 3,0:  9
At 4,0:  3
At 0,1:  0
At 1,1:  14
At 2,1:  13
At 3,1:  0
At 4,1:  12
At 0,2:  9
At 1,2:  5
At 2,2:  2
At 3,2:  8
At 4,2:  1

Or, as a picture:

Example tiling

Tile color list

If you need it, here's a list of the colors on each tile. Each tile is described in left-top-right-bottom order.

= {r:0y:1b:2g:3p:4w:5}
# A list of the colors of each tile, left, top, right, bottom. 
colors = [
  [c.rc.yc.yc.r# 0 
  [c.gc.bc.bc.g# 1 
  [c.bc.pc.pc.b# 2 
  [c.wc.gc.gc.w# 3 
 
  [c.bc.pc.bc.g# 4 
  [c.wc.gc.bc.g# 5 
  [c.gc.bc.pc.b# 6 
  [c.gc.bc.gc.w# 7 
 
  [c.pc.rc.gc.y# 8 
  [c.bc.rc.wc.y# 9 
  [c.pc.rc.bc.r# 10 
  [c.gc.yc.wc.y# 11 
 
  [c.yc.wc.rc.b# 12 
  [c.yc.gc.rc.p# 13 
  [c.yc.wc.yc.g# 14 
  [c.rc.bc.rc.p# 15 
]

Readme

Keywords

none

Package Sidebar

Install

npm i wang

Weekly Downloads

2

Version

0.1.0

License

BSD

Last publish

Collaborators

  • josephg