p5.js function that creates an empty or a filled quadrille
.
createQuadrille(width, height)
: creates an empty quadrille havingwidth
number of columns andheight
number of rows.createQuadrille(
jagged_array
)
: creates a quadrille and fills its cells taking thejagged_array
items as source. Note thatnull
array
items represent empty quadrille cells.createQuadrille(array)
: creates a quadrille and fills its cells taking thearray
items as source. Note thatnull
array
items represent empty quadrille cells.createQuadrille(width, array)
: creates a quadrille and fills its cells taking thearray
items as source up towidth
number of columns. Observe that (one or) several quadrille rows may be created to include all thearray
items. Note thatnull
array
items represent empty quadrille cells.createQuadrille(string)
: creates a quadrille and fills its cells takingstring
as source. The resulting number of quadrillecolumns
matches that of the string length.createQuadrille(width, string)
: creates a quadrille and fills its cells takingstring
as source. Note that (one or) several quadrille rows may be created to include all thestring
characters.createQuadrille(width, image, [coherence])
: creates a quadrille and fills its cells takingimage
(either a p5.Image or a p5.Graphics) as source. Thecoherence
boolean param defines whether or not the quadrille filling algorithm should use spatial coherence.createQuadrille(width, height, order, pattern)
: creates a quadrille and fills its cells usingpattern
(any data type instance butundefined
ornull
) which is randomly repeatedalong
the quadrille up toorder
number of times.createQuadrille(width,
bitboard
, pattern)
: creates a quadrille and fills its cells takingbitboard
as source, usingpattern
(any data type instance butundefined
ornull
) to represent1
(or on) bits.
p5.js function that draws the quadrille at (x, y)
screen position on the graphics
(which is the main canvas
by default), using the display parameter values.
drawQuadrille(quadrille, [{
[graphics=this],
[x=0],
[y=0],
[cellLength=Quadrille.CELL_LENGTH],
[outlineWeight=Quadrille.OUTLINE_WEIGHT],
[outline=Quadrille.OUTLINE],
[textColor=Quadrille.TEXT_COLOR],
[textZoom=Quadrille.TEXT_ZOOM],
[tileDisplay=Quadrille.TILE],
[imageDisplay=Quadrille.IMAGE],
[stringDisplay=Quadrille.STRING],
[colorDisplay=Quadrille.COLOR],
[numberDisplay=Quadrille.NUMBER],
[arrayDisplay],
[objectDisplay]
}])
Observations
- The default display parameter values are defined as
TEXT_COLOR = 'white'
,TEXT_ZOOM = 0.89
,OUTLINE = 'grey'
,OUTLINE_WEIGHT = 2
andCELL_LENGTH = 100
. - To display cells populated with an
array
or anobject
you should provide implementations of thearrayDisplay
andobjectDisplay
functions, respectively. Provide your own display functions to override the defaults (e.g., to display the quadrille using a tiling different than the square). - The display functions are parameterized as follows:
tileDisplay
:{graphics: graphics, outline: outline, outlineWeight: outlineWeight, cellLength: cellLength, row: i, col: j}
.imageDisplay
,colorDisplay
,numberDisplay
,arrayDisplay
andobjectDisplay
:{graphics: graphics, cell: cell, outline: outline, outlineWeight: outlineWeight, cellLength: cellLength, row: i, col: j}
.stringDisplay
:{graphics: graphics, cell: cell, textColor: textColor, textZoom: textZoom, outline: outline, outlineWeight: outlineWeight, cellLength: cellLength, row: i, col: j}
.
memory2D
: Array2D: quadrille memory read-write property.width
Number: quadrille width read-write property.height
Number: quadrille height read-write property.size
Number: read-only property that retrieves the quadrille width times the quadrille height.order
Number: read only property that retrieves the quadrille non-empty number of cells.
from(image, [coherence=false])
,from(
bitboard
, pattern)
: fills quadrille cells with givenimage
orbitboard
usingpattern
(any data type instance butundefined
ornull
), respectively.toArray()
: returns a row-major order array of the quadrille cells. The resulting array hasquadrille.width * quadrille.height
dimensions.toInt()
: returns the integer representation of the quadrille filled cells using big-endian and row-major ordering.toMatrix()
: returns a row-major order matrix of the quadrille cells. The resulting 2D array hasquadrille.width * quadrille.height
dimensions.
reflect()
; horizontal reflection of the quadrille cells.rotate()
: π/2 clockwise rotation of the quadrille cells.transpose()
: transposes the quadrille cells.
clear()
,clear(row)
,clear(row, col)
: clears quadrille cells. Either all quadrille cells, a givenrow
or a cell, resp.clone()
: returns a shallow copy of the quadrille.delete(row)
: deletes the given quadrillerow
.fill(pattern)
,fill(row, pattern)
,fill(row, col, pattern)
: fills quadrille cells with givenpattern
(any data type instance butundefined
ornull
). Either current empty cells, a wholerow
, or a cell, respectively.insert(row)
: inserts an emptyrow
into the quadrille.isEmpty(row, col)
: returnstrue
if cell found at(row, col)
is empty andfalse
otherwise.magnitude(row)
: returns the number of non-empty cells of a given quadrillerow
.rand(order, pattern)
: fills the quadrille withpattern
(any data type instance butundefined
ornull
) up toorder
(number of repeations), randomly adding or removing cells as necessary.randomize()
: randomly re-arranges the quadrille cells.read(row, col)
: returns the contents of the quadrille cell at(row, col)
. Returnsundefined
if the cell doesn't exist.replace(pattern)
,replace(pattern1, pattern2)
: either replaces non empty cells withpattern
or searchespattern1
cell ocurrences and replaces them withpattern2
, respectively. Both,pattern1
andpattern2
are any data type instances butundefined
ornull
.ring(row, col, [dimension=1])
: returns the ring of neighbor cells centered at (row, col) as a new quadrille.
colorize(color0, [color1=color0], [color2=color0], [color3=color0])
: colorizes the quadrille according to upper-left cornercolor0
, bottom-left cornercolor1
, upper-right cornercolor2
, and bottom-right cornercolor3
colors.colorizeTriangle(row0, col0, row1, col1, row2, col2, color0, [color1=color0], [color2=color0])
: colorizes the triangle defined by vertices(vertex0=) (row0, col0)
,(vertex1=)(row1, col1)
, and(vertex2=)(row2, col2)
, using barycentric coordinates to interpolatecolor0
,color1
andcolor2
. Implemented as:colorizeTriangle(row0, col0, row1, col1, row2, col2, color0, color1=color0, color2=color0) { this.rasterizeTriangle( row0, col0, row1, col1, row2, col2, ({ pattern: xyza }) => color(xyza), // fragment shader colorizes (row0, col0), (row1, col1), (row2, col2) triangle // vertex attributes to be interpolated (each encoded as an array): [red(color0), green(color0), blue(color0), alpha(color0)], // vertex0 color [red(color1), green(color1), blue(color1), alpha(color1)], // vertex1 color [red(color2), green(color2), blue(color2), alpha(color2)] // vertex2 color ); }
filter(mask, [row=0, col=0])
: applies convolution mask filter either to the whole quadrille or at specific(row, col)
cell.sort([{[mode='LUMA'], [target='magenta'], [ascending=true], [textColor='black'], [textZoom=Quadrille.TEXT_ZOOM], [background=Quadrille.BACKGROUND], [cellLength=this.width], [numberColor=Quadrille.numberColor], [min=0], [max=0]}])
: sorts quadrille cells according to their coloring. Note that theBACKGROUND
param is black,mode
is either'LUMA'
,'AVG'
, or'DISTANCE'
,target
is ap5.Color
instance andascending
is a boolean. Remaining params defined as within thedrawQuadrille
function.rasterize(shader, pattern0, [pattern1=pattern0], [pattern2=pattern0], [pattern3=pattern0])
: rasterizes the quadrille according to upper-left corner vertexpattern0
, bottom-left corner vertexpattern1
, upper-right corner vertexpattern2
, and bottom-right corner vertexpattern3
, using (fragment)shader
.rasterizeTriangle(row0, col0, row1, col1, row2, col2, shader, pattern0, [pattern1=pattern0], [pattern2=pattern0])
: rasterizes the triangle defined by vertices(row0, col0)
,(row1, col1)
, and(row2, col2)
, using barycentric coordinates. The user provided software rendered (fragment) shader is a function parameterized with the object literal{ pattern: interpolated_data_array, row: i, col: j }
and that should return a p5.Color.
The following operators are inspired by CSG as a high-level quadrille modelling technique.
Quadrille.AND(quadrille1, quadrille2, [row=0], [col=0])
: returns the quadrille obtained from the intersection of the two given quadrilles.Quadrille.DIFF(quadrille1, quadrille2, [row=0], [col=0])
: returns the quadrille obtained from the difference of the two given quadrilles.Quadrille.NEG(quadrille, pattern)
: returns the quadrille obtained from clearing thequadrille
filled cells and filling its empty cells withpattern
(any data type instance butundefined
ornull
).Quadrille.OP(quadrille1, quadrille2, operator, [row=0], [col=0])
: returns the quadrille obtained after applying the given logical operator between the two given quadrilles. This method is useful to implement the other high-level logical operators. For instance the AND operator is implemented as follows:static AND(quadrille1, quadrille2, row=0, col=0) { return this.OP(quadrille1, quadrille2, (q1, q2) => { if (q1 && q2) { return q1; } }, row, col); }
Quadrille.OR(quadrille1, quadrille2, [row=0], [col=0])
: returns the quadrille obtained from the union of the two given quadrilles.Quadrille.XOR(quadrille1, quadrille2, [row=0], [col=0])
: returns the quadrille obtained from the intersection minus the union of the two given quadrilles.
Link the p5.quadrille.js
library into your HTML file, after you have linked in p5.js. For example:
<!doctype html>
<html>
<head>
<script src="p5.js"></script>
<script src="p5.sound.js"></script>
<script src=https://cdn.jsdelivr.net/gh/objetos/p5.quadrille.js/p5.quadrille.js></script>
<script src="sketch.js"></script>
</head>
<body>
</body>
</html>
to include its minified version use:
<script src=https://cdn.jsdelivr.net/gh/objetos/p5.quadrille.js/p5.quadrille.min.js></script>
instead.
To run and hack the demo:
- Clone the repo (
git clone https://github.com/objetos/p5.quadrille.js
) and open it with your favorite editor. - Install the p5-vscode extension.
- Head over
demo/index.html
and press your editorGo Live
button.