Checkerboards


Checkerboards

Rectangular

I wanted to draw checkerboards in Python Turtle.  To get a filled polygon in Turtle, it has to be drawn entirely in one go as a closed shape.  The common type of checkerboards are rectangular (or square).  These are pretty straightforward to draw.

Here is an output of a rectangular checkerboard:

Rectangular checkerboard
Rectangular checkerboard

Here is the code to draw the output shown above:

Note: You can run, edit and make your own changes to all the code samples in this post and try them out (thanks to trinket).


Circular

Now that we have a rectangular checkerboard, I wanted to draw circular checkerboards.  This involves the use of Turtle's circle method itself (with extent parameter set) to draw individual arcs.  Each individual polygon can be considered as a quad with four points.  These are calculated and connected using lines and arcs.

Here is an output of a circular checkerboard:

Circular checkerboard
Circular checkerboard

Here is the code for the circular checkerboard shown above:



Polygonal

After that, I wondered if we could draw checkerboards for any given polygon.

This is what I had in mind:

Expected polygon checkerboard
Expected polygon checkerboard

This too involves drawing separate quads filled alternatively as with the circular checkerboard.  The difference here is that the sides are straight lines instead of curves with a fixed radius.  We move the Turtle to calculate these four points and then connect them.

Here are some outputs of checkerboards for different polygons:

Polygon checkerboard - Triangle
Polygon checkerboard - Triangle
Polygon checkerboard - Square
Polygon checkerboard - Square
Polygon checkerboard - Hexagon
Polygon checkerboard - Hexagon

Here is the code for the outputs shown above:



Polygonal curved

I now wondered if we could draw curved lines at the sides instead of straight lines.  I wanted to see how this looked for different polygons.

This is what I had in mind:

Expected polygon curved checkerboard
Expected polygon curved checkerboard

I extended the code for the polygon checkerboards to draw the curved ones.  The basic idea is the same, but instead of straight lines, we have to use arcs to draw individual quads.  To draw the arcs correctly, I had to remember and use the correct Turtle direction (heading) while connecting the four points.

Here are some outputs of curved checkerboards for different polygons:

Polygon checkerboard curved - Triangle
Polygon checkerboard curved - Triangle
Polygon checkerboard curved - Square
Polygon checkerboard curved - Square
Polygon checkerboard curved - Hexagon
Polygon checkerboard curved - Hexagon

Here is the code for the outputs  shown above:



Other board patterns

Instead of a full curve (or straight lines) if you connect the points in different ways (red in the picture below) you can get different board patterns.  We could potentially draw checkerboards for these too with a bit more work (future).

Here are some of these board patterns:

Potential board patterns
Potential board patterns

Spirals

The circular checkerboards can be easily extended to connect the four points of the quad in several different ways.  Doing so, we can get interesting spirals.

Here are some sample spiral checkerboards that we can get by making small changes to the circular checkerboard code:

Spiral checkerboards
Spiral checkerboards

Summary

In summary, checkerboards need not be only rectangular or circular, but any regular polygon - even curved, as shown above.

All the code in this post is available on GitHub.

Updates:

5 July 2020
  1. An interactive version of Polygon Checkerboards can be found in this later post.
- Pardhav Maradani

Popular posts from this blog

Interactive Mandalas

Auto Generated Mandalas

Bézier Curve Animations