Tuesday, August 25, 2015

simple fun with R

I'm in the process of learning about the programming language R, and thought a nice way to get started would be to play with a few simple examples of things I've previously posted about. You can take any of these examples and try them out yourself by copying the code right into an in-browser R environment like R-fiddle.

The two baby R examples here make use of some of the built in math functions in R (sin, cos, and sqrt), and illustrate the vector-based approach that R takes. In R, instead of iterating using a while or for loop and applying functions to individual values, many functions are 'vectorized' so you can provide a whole vector of arguments to a function and it will do the iterating for you. Hopefully you will see what I mean in the examples below.

Example 1: Lissajous figures


















R provides a number of ways of creating vectors - ordered collections of a single data type. One way to create a vector that is just a sequence of the form n, n + 1, n + 2, ... k is to use the colon notation n:k. Here (example 1a) some basic raw input the (vector t) has a scaling factor f applied to it. When you multiply a 'scalar' (actually, in R this is a one-element vector) by a vector, the scalar is applied to each element of the vector (as you might expect) to produce a new vector. Something a bit less expected is that functions like sin() are vectorized - if you provide a vector as an argument, they will produce a vector of the same size as a result, and the contents of the returned vector are what you would expect to get from applying the function to each element.

The plot() function is flexible - when a single vector is provided (example 1a) it will plot the ordered pairs (x, y) using elements of the vector as y values against x values inferred from each element's position in the vector (its index). When two vectors are provided (example 1b), the first vector provides the x values, and the second the y values (generally, R implements some 'recycling' behavior when the two vectors are not of equal length but the length of one is a multiple of the other). Finally, the plot() function offers some additional arguments for formatting the output.

 A very different "synthetic" approach to drawing lissajous figures can be taken in Geometer's Sketchpad, as mentioned here.

Example 2: Phyllotaxis spirals

















Something that takes getting used to: sqrt(t)*cos(t) is not merely the multiplication of two numbers, it is the position-wise multiplication of two vectors (lists of numbers): t is a vector, sqrt(t) is a vector made up of the square roots of the elements of t, cos(t) is a vector made up of the cosines of the elements of t, and sqrt(t)*cos(t) is a vector made up elements obtained by multiplying the ith element of sqrt(t) by the ith element of cos(t). This is not the normal 'mathematical' way of multiplying vectors, but it is consistently used throughout R to achieve its distinctive vectorized programming model.

There are some older posts on drawing spirals like this in Fathom and Tinkerplots (see here), and also in Processing (see here).

Monday, July 20, 2015

regular polygons, in rings

Looking at the Kepler pentagonal tiling, you may notice the nice looking rings of pentagons around the decagons.


You can also make up other tilings with these rings of pentagons - to get the one below to work you have to sneak in some dented or overlapping pentagons.


But which regular n-gons can form rings like this? You obviously can't do it with a square.


And some regular n-gons, like heptagons, nonagons, decagons, and hendecagons (11-gons) don't work either.


All the angles of the regular n-gon are (n-2)pi/n - so the angles of the polygon in the center would have to be 4pi/n, but for that interior polygon to be a regular polygon itself, there must be some k for which the angle is also (k-2)pi/k. Equating these two values and solving for k gives k = 2n/(n-4). If we look for n that give integer values for k, then we have the n-gons that can form this sort of ring.


Which tells us that only the pentagon, hexagon, octagon, and dodecagon can form a ring around another regular n-gon (the regular decagon, hexagon, quadrilateral, and triangle, respectively). Coincidentally, these are the same polygons that can form a dented pinwheel, as described here.

But what if we skip over another edge (so 2 are skipped over) while forming the ring? We end up getting a star instead of a polygon in the center, and the smallest regular polygon this works for is the heptagon:


With a little bit of work, you may believe that this will work for n that give integer values for k = 2n/(n-6), and this turns out that those n values correspond to the regular heptagon, octagon, nonagon, decagon, dodecagon and octadecagon (18-gon).



As with the first kind of ring, the hendecagon fails:


But we can go further, and skip over another edge (3 now) when forming the ring of polygons. The center is no longer a star, but  a bumpy gear-like polygon, and the smallest regular polygon that can do this is the nonagon:


What other polygons can form this third kind of ring where 3 edges are skipped? Our function is now k = 2n/(n-8), and we get integer values for n = 9, 10, 12, 16, and 24.


Our hendecagons still won't form a ring when skipping 3 edges, but will once we start skipping 4.


If we skip m edges when putting the ring together, we can find the number of regular n-gons that will form the ring using the formula k = 2n/(n-2(m+1)), and will only get closed rings when k takes on integer values.

From this relationship we can find out a few things about these rings. For example, for any odd n, where n is 5 or more, we can form a ring by skipping (n-3)/2 edges and have a ring of 2n: for regular pentagons, we skip 1 edge and get a ring of 10, for heptagons we skip 2 edges and get a ring of 14, and for regular hendecagons, we skip 4 edges to get a ring of 22. Another observation: the eminently factorable 12 allows the dodecagon to form rings of 3, 4, 6, or 12.

I was lead to this while playing with regular heptagon, having fun making rings (and rings of rings) like the ones below.




Friday, July 3, 2015

regular polygons, intersecting regularly

Looking through the chapter on the number 5 in the really engaging book Single Digits: In Praise of Small Numbers, by Marc Chamberland, I came across an image and description of Kepler's pentagonal tiling, which looks like this:

Kepler Pentagonal Tiling

This tiling is made of pentagons, pentagrams, decagons, and fused decagons. Both the decagons and the fused decagons can be made from combinations of regular pentagons and dented pentagons (by dented, I mean in the way described here), so this tiling could also be made with pentagons, dented pentagons, and pentagrams.

Decagons and fused decagons from
pentagons and dented pentagons 

I wanted to try to make a similar tiling made only of the pentagons, dented pentagons, and their complementary rhombs, and found this:

Another pentagonal tiling

Which at first looks good - except these fused decagons are not the same as Kepler's - they have a greater overlap and cannot be made from pentagons and dented pentagons.

Encountering these two kinds of fused decagons, I wondered how many ways you can intersect two decagons, or other regular polygons, in the nice regular way these decagons were fusing.

Regular intersecting pentagons to nonagons

To overlap "regularly," the two n-gons must line up on their vertices. This means that the overlapping region will also be an equilateral polygon, that will have an even number of sides (half from each of the original n-gons), and will have all but two of its angles equal to (n-2)pi/n, as all but two of the angles will be angles from the original regular n-gons.

Since the overlapping region cannot have fewer sides/vertices than four, and because it can only be a polygon with an even number of sides/vertices, an n-gon will have [(n-4)/2] ways of regularly intersecting with itself (where the square bracket is the "ceiling" function, which tells you to round up any decimals). So, there are [(10-4)/2] = 3 of these nice fusings of the regular decagon.

The 3 fused decagons

Friday, June 12, 2015

regular polygons, dented and sliced

A while ago, l noticed that sliced up octagons made nice tiles.
In particular, octagons that are split in a particular way into a dented octagon and a rhombus are pretty neat. These rhombuses are formed from so that they share with the octagon two adjacent sides of the octagon. The dented octagon is formed by slicing off the rhombus. Four of those dented octagons can be put around a vertex to form a pinwheel pattern, and four of the rhombs can be added to that pinwheel to make a bigger octagon.

You can do this sort of rhombic slicing with any regular polygon with more than 4 sides (you could slice a rhombus off a square in this way, if you consider the split off rhombus to be the same as the original square, but let's not go there). The picture below shows the angles you get when you slice a regular n-gon to get a dented n-gon and an n-rhomb.

One way to play around with these is to see how they can fit back together in various combinations. The motivating question is: For a given n, which combinations of regular n-gons, dented n-gons and n-rhombs can be placed around a vertex without gap or overlap?

Consider the pentagon
Lets start with a pentagon. The interior angles of the pentagon are 3pi/5. The small angle of the rhombus is going to be 2pi/5, and the small angle of the dented pentagon is going to be pi/5.



You may know that you can't place a set of non-overlapping pentagons around a vertex without leaving a gap (the best you can do is three, which give 9pi/5 around the vertex). Well, the dented pentagon you get from rhomb-splitting is just what you need to fill that gap (9pi/5 + pi/5 = 2pi).


The dented pentagons on their own can be placed around vertex nicely to form a decagon pinwheel, as can the rhombs, to form a star.



Rhombuses formed from "rhombic slicing" of a regular polygon can always be placed in a star pattern around a vertex. For an n-gon, the big angle of the sliced rhomb is always (n-2)pi/n, so the small angle is always 2pi/n, which means you can always place n of these small angles around a vertex without a gap or overlap.

You can't always place the dented polygons (small angle inward) formed by rhombic slicing around a vertex to form pinwheels like the dented pentagon, and you can't always use the dented polygons to fill in gaps formed by regular polygons around a vertex, but there are some combinations of all three (regular, dented, and rhomb) that will always fit around a vertex.

A few special arrangements
Suppose we wanted to put k regular polygons around a vertex. The sum of the angles around the vertex would have to be 2pi. So we would have k(n-2)pi/n = 2pi. Solving for k, we have k = 2n/(n-2). This only has 3 integer solutions, which occur when n = 3, 4, and 6. So from this we know we can arrange 6 equilateral triangles, 4 squares, or 3 regular hexagons around a vertex, but no other single regular polygon.



If we want to put k dented polygons around a vertex to make a pinwheel like we saw above for the pentagons, we'd place the small angles at the vertex and try to have them sum to 2pi. This requires us to have k(n-4)pi/n = 2pi, or  k = 2n/(n-4). So only n = 5, 6, 8, or 12 will work.



It was neat how the dented pentagon could be used to fill the gap left by an arrangement of regular pentagons; can we do that for any other regular polygons? It turns out that octagons are the only other regular polygon that fits together with one of its "dented" selves in this way. Here we are looking at k(n-2)pi/n + (n-4)pi/n = 2pi, which gives us k = (n+4)/(n-2), and k will be an integer only for n = 5 and n = 8.



Above we saw that regular polygons with 5, 6, 8, and 12 sides would, when dented, form pinwheels. There may be some cases where an incomplete pinwheel can be completed with a full regular polygon (sort of the opposite case of what we just saw with a dented polygon completing an arrangement of regular polygons). For this we are looking at (n-2)pi/n + k(n-4)pi/n = 2pi, which gives us k = (n+2)/(n-4), with integer values of k at n = 5, 6, 7, and 10.

If we throw the sliced rhombuses into the mix, we get more options for arranging tiles around a vertex. For example, when can you arrange one regular polygon with k of its sliced rhombuses around a vertex? Here the equation is  (n-2)pi/n + 2kpi/n = 2pi, or k = (n+2)/2, which gives us integer k values for all even n.  The resulting squid-like shapes alternate between having an even number or odd number of limbs.



What always works
We already noticed that you can always place n of the rhombs from an n-gon around a vertex without a gap to form a star. There are some other combinations of these tiles that also always work.

For example, 2 regular polygons and 2 corresponding rhombs will work. 2(n-2)pi/n + 2(2pi)/n = 2pi. But then, you can take one of those two regular polygons and split it into a dented polygon and a rhomb, so 1 regular polygon, 1 dented polygon and 3 rhombs will work. Finally you can split your last regular polygon to get a combination of 2 dented polygons and 4 rhombs.




Saturday, May 30, 2015

octo rhomb

Regular octagons cannot be used to tile by themselves - if you try, you will find there are square gaps that need to be filled.

If you slice a rhombus off your octagon, you'll end up with two tiles - a rhombus and a dented octagon.
Each of these shapes can be used to tile by themselves, or tile together. The rhomb-by-itself tiling is easy to visualize (imagine a squashed grid), here is the dented-octagon tiling:


Now, here's something else: you can take four of these rhombically challenged octagons to make a bigger octagon:


You get a nice tree shape if you remove two rhombs from the original octagon.


And these two shapes can be combined in many different ways.




If you remove 3 rhombs from the octagon, you get a shape that's a combination of two squares and another rhomb - a bent spiky thing: