Skip to content

Instantly share code, notes, and snippets.

@runemadsen
Created September 9, 2012 04:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runemadsen/3682600 to your computer and use it in GitHub Desktop.
Save runemadsen/3682600 to your computer and use it in GitHub Desktop.
Trying Ammann Beenker tiling in Processing
float magic = 1+sqrt(2);
int maxLevel = 2;
void setup()
{
size(1280, 800);
background(255);
smooth();
fill(255);
//drawRhombus(150, 30, 4000, 0, 0);
drawRectangle(0, 0, 1280, 0, 0);
}
void drawRhombus(float x, float y, float diagonal, float deg, int level)
{
if(level == maxLevel)
stroke(0);
else
stroke(255);
// a rhombus is actually just 4 right sided triangles, where each hypotenuse is the side of the rhombus.
// let's do some right sided triangle magic to find hypotenuse from the angle (45/2) and the adjacent (diagonal / 2)
float sideLength = (diagonal / 2.0) / cos(radians(45.0/2));
pushMatrix();
translate(x, y);
rotate(radians(deg));
// figure out skewing of rhombus and draw it
PVector skew = new PVector(sin(radians(45.0)) * sideLength, cos(radians(45.0)) * sideLength);
quad(0.0, 0.0, 0.0, sideLength, skew.x, skew.y + sideLength, skew.x, skew.y);
// draw children
if(level < maxLevel)
{
// find small diagonal of rhombus by using the pythagorean theorem:
// getting the opposite from the hypotenuse (sidelength) and angle (45 / 2)
float shortDiagonal = sin(radians(45.0/2.0)) * sideLength;
drawRhombus(0.0, 0.0, shortDiagonal * 2.0, 0.0, level + 1);
drawRhombus(0.0, 0.0 + sideLength, shortDiagonal * 2.0, -90, level + 1);
drawRhombus(skew.x, skew.y + sideLength, shortDiagonal * 2.0, -180, level + 1);
// rectside length is equal to the small rhombus side length
float rectSide = (shortDiagonal / 2.0) / cos(radians(45.0/2.0));
float rectDiagonal = rectSide * sqrt(2); // have to be multiplied by 2
drawRectangle(skew.x - (rectSide * 2.0), rectDiagonal + (rectSide * 2.0), rectSide * 2.0, -90, level + 1);
drawRectangle(skew.x + (rectDiagonal), skew.y + (rectDiagonal), rectSide * 2.0, 135, level + 1);
drawRectangle(-rectDiagonal, sideLength - rectDiagonal, rectSide * 2.0, -45, level + 1);
drawRectangle(rectSide * 2.0, sideLength, rectSide * 2.0, 90, level + 1);
}
popMatrix();
}
void drawRectangle(float x, float y, float sideLength, float deg, int level)
{
if(level == maxLevel)
stroke(0);
else
stroke(255);
pushMatrix();
translate(x, y);
rotate(radians(deg));
// draw big rect
rect(0, 0, sideLength, sideLength);
//ellipse(0, sideLength, 10, 10);
// draw children
if(level < maxLevel)
{
// draw rects
float shortRectSide = sideLength / magic;
// in a square, the diagonal is the side multipled by the sq. root of 2
float shortRectDiagonal = shortRectSide * sqrt(2);
drawRectangle(shortRectDiagonal/2, shortRectDiagonal / 2, shortRectSide, 135, level + 1);
drawRectangle(shortRectDiagonal/2 + shortRectSide, shortRectDiagonal / 2 + shortRectSide, shortRectSide, 180, level + 1);
drawRectangle(sideLength - (shortRectDiagonal/2), (shortRectDiagonal / 2), shortRectSide, -135, level + 1);
drawRectangle(sideLength + (shortRectDiagonal / 2), (shortRectDiagonal / 2), shortRectSide, 135, level + 1);
drawRectangle(sideLength - (shortRectDiagonal/2), sideLength + (shortRectDiagonal / 2), shortRectSide, -135, level + 1);
// find rhombus diagonal from its hypotenuse
float rhompDiagonal = cos(radians(45.0/2.0)) * shortRectSide;
drawRhombus(0, 0, rhompDiagonal * 2, -45, level + 1);
drawRhombus(0, sideLength, rhompDiagonal * 2.0, -90, level + 1);
drawRhombus(0, sideLength, rhompDiagonal * 2.0, -135, level + 1);
drawRhombus(sideLength - (shortRectDiagonal/2), shortRectDiagonal / 2, rhompDiagonal * 2, 0, level + 1);
}
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment