# Yet Another Way to Draw Lines

tutorial

Sure, why not, right?

One of the things I was trying to accomplish with the shaky drawing from my last post was to give the idea of hand-drawn lines. By adding in a bit of random variance, lines and shapes can seem like they are drawn by a human rather than perfectly rendered by a computer.

But at some point I was actually sketching something by hand and realized another big difference between computer drawing and human sketching. Very often when a person is sketching, rather than boldly drawing what they hope is a straight line from point A to B, they will instead make a number of very light lines that roughly go between the two points. You’ve seen it, you’ve done it, you know what I mean. So let’s replicate that.

The idea is that when we want to draw a line from `x0, y0` to `x1, y1` for example, we’ll actually draw multiple lines – not exactly to those to points, but from somewhere around the first point, to somewhere around the second one.

In this case, I’m going to dive right into a function. This was my first attempt:

```function sketchLine(context, x0, y0, x1, y1, count, rand) {
context.beginPath();
for (let i = 0; i < count; i++) {
let x = x0 + Math.random() * rand - rand / 2;
let y = y0 + Math.random() * rand - rand / 2;
context.moveTo(x, y);

x = x1 + Math.random() * rand - rand / 2;
y = y1 + Math.random() * rand - rand / 2;
context.lineTo(x, y);
}
context.stroke();
}
```

We can then call this like so:

```context.lineWidth = 0.2;
sketchLine(context, 100, 100, 700, 700, 5, 20);
```

Note that I set the line width down to 0.2 to make very light lines. We’ll let the multiple light lines build up to create the idea of a sketch, the same way you would do it by hand. So this will draw 5 lines with a variance of up to 10 pixels in any direction from the starting and ending points. The result:

Not that great, to be honest. The lines vary a bit, but they are still too uniform. I can bump up the randomness to, say, 50, but that looks even less like what a real person would sketch.

The thing with sketching, particularly in longer lines, is that you don’t always sketch the entire length of the line on each stroke. You might start around the first point and stroke maybe half way to the other point. And then you might stroke the middle third of the distance between them, and then something closer to the ending point. You’d do a bunch of strokes with varying start and end distances. Well, we can do something like that too.

```function sketchLine(context, x0, y0, x1, y1, count, rand) {
const dx = x1 - x0;
const dy = y1 - y0;

context.beginPath();
for (let i = 0; i < count; i++) {
let t0 = Math.random() * 0.5;
let t1 = Math.random() * 0.5 + 0.5;

let x = x0 + dx * t0 + Math.random() * rand - rand / 2;
let y = y0 + dy * t0 + Math.random() * rand - rand / 2;
context.moveTo(x, y);

x = x0 + dx * t1 + Math.random() * rand - rand / 2;
y = y0 + dy * t1 + Math.random() * rand - rand / 2;
context.lineTo(x, y);
}
context.stroke();
}
```

This should look somewhat familiar if you’ve followed along with the other two posts in this series. First I get length of the line on the x and y axes – `dx` and `dy`. In the loop, I create a `t` value between 0 and 1. In this case, I create two of these – `t0` and `t1` – one for the start of the line and one for the end. `t0` will range from 0.0 to 0.5 and `t1` will go from 0.5 to 1.0. This should give us a random assortment of lines – some from very near the start point to very near the end point, some more in the first part of the line, some in the middle and some more near the end. Calling this again with:

```context.lineWidth = 0.2;
sketchLine(context, 100, 100, 700, 700, 5, 20);
```

We get this image:

That’s something I could almost start to believe was done by hand. Let’s do a rectangle function!

```function sketchRect(context, x, y, w, h, count, rand) {
sketchLine(context, x, y, x + w, y, count, rand);
sketchLine(context, x + w, y, x + w, y + h, count, rand);
sketchLine(context, x + w, y + h, x, y + h, count, rand);
sketchLine(context, x, y + h, x, y, count, rand);
}
```

Obviously, I just copied and pasted the `shakyRect` from the previous post and changed the method names and parameters. So let’s call this with:

```context.lineWidth = 0.2;
sketchRect(context, 100, 100, 600, 600, 5, 20);
```

Pretty cool, but this brings up an issue we couldn’t immediately see when drawing a single line. All too often, the lines don’t reach all the way to those starting or ending points. Too many are falling right in the middle. We can shift our random parameters a bit to make up for that.

```function sketchLine(context, x0, y0, x1, y1, count, rand) {
const dx = x1 - x0;
const dy = y1 - y0;

context.beginPath();
for (let i = 0; i < count; i++) {
let t0 = Math.random() * 0.4 - 0.1;
let t1 = Math.random() * 0.4 + 0.7;

let x = x0 + dx * t0 + Math.random() * rand - rand / 2;
let y = y0 + dy * t0 + Math.random() * rand - rand / 2;
context.moveTo(x, y);

x = x0 + dx * t1 + Math.random() * rand - rand / 2;
y = y0 + dy * t1 + Math.random() * rand - rand / 2;
context.lineTo(x, y);
}
context.stroke();
}
```

Now, `t0` will range from -0.1 to +0.3, and `t1` will go from 0.7 to 1.1. This makes it more likely that some lines will make it closer to one of the points, and may even overshoot it a bit, which is just what you’d do by hand. The results of this change:

Not bad, in my opinion. You’ll still have some rectangles with open corners, but not as often as before. Here’s one with a `count` of 20 and a `rand` of 40:

Finally, just for the heck of it, I combined the shaky and sketchy techniques with this function:

```function sketchLine(context, x0, y0, x1, y1, count, rand) {
const dx = x1 - x0;
const dy = y1 - y0;

context.beginPath();
for (let i = 0; i < count; i++) {
let t0 = Math.random() * 0.4 - 0.1;
let t1 = Math.random() * 0.4 + 0.7;

let xA = x0 + dx * t0 + Math.random() * rand - rand / 2;
let yA = y0 + dy * t0 + Math.random() * rand - rand / 2;
let xB = x0 + dx * t1 + Math.random() * rand - rand / 2;
let yB = y0 + dy * t1 + Math.random() * rand - rand / 2;
shakyLine(context, xA, yA, xB, yB, 20, 2);
}
context.stroke();
}
```

Here, I calculated the start and end points of each sketched line and rather than using `moveTo` and `lineTo` I called the `shakyLine` function from my last post. I hard coded it with a `res` of 20 and a `rand` of 2, which makes it just a little bit less than a perfectly straight line and possibly a little bit more like something hand drawn. Called with:

```context.lineWidth = 0.2;
sketchRect(context, 100, 100, 600, 600, 10, 20);
```

it gives us:

Even that might be too much shake, but you can easily make it more subtle.

Here’s one final example. If this doesn’t look hand sketched, I don’t know what does.

Well, that’s that. I don’t have a ready-made library to share with you on this one, but hopefully you’ll find this useful or at least inspiring.

The rest of the How to Draw a Line series:

# More Ways to Draw Lines

tutorial

In the last post, I discussed an alternate way of rendering lines, that could give a glitchy look, painterly look, or many other looks based on what parameters you used. In this post, I’ll discuss another way to draw lines.

The theme to these posts is that using `moveTo` / `drawTo` is fine but gives you clinical, antiseptic, perfect straight lines (or curves). And sometimes it’s nice to shake things up a bit. In fact, shaking things up is exactly what we’re going to do here today.

I’m going to start with something very similar to one of the earlier examples in the last post, but instead of drawing individual pixels along the length of the line, I’m going to draw short line segments.

```const dx = x1 - x0;
const dy = y1 - y0;
const dist = Math.sqrt(dx * dx + dy * dy);
const res = 10;

context.beginPath();
context.moveTo(x0, y0);

for (let i = res; i < dist; i += res) {
let t = i / dist;
let x = x0 + dx * t;
let y = y0 + dy * t;
context.lineTo(x, y);
}
context.lineTo(x1, y1);
context.stroke()
```

Like before, I’m starting with two points defined by `x0, y0` and `x1, y1`. And I’ve defined a `res` variable that dictates the length of those intermediate segments. Note that I `moveTo` the first point and set `i` equal to `res` to initialize the loop, and then `lineTo` the last point. This ensures that the start and end of the line remain constant (which will be important later). Otherwise, things should be familiar here if you read the previous post. Here’s what this gives us:

It’s exactly what you’d get if you just drew a line from the first to last point. So big deal. But now that we have these intermediate points, we can shake things up as promised.

I’ll just randomly shift each `x, y` point a bit before drawing to it. (Just showing the for loop here:

```for (let i = res; i < dist; i += res) {
let t = i / dist;
let x = x0 + dx * t;
let y = y0 + dy * t;
x += Math.random() * 4 - 2;
y += Math.random() * 4 - 2;
context.lineTo(x, y);
}
```

This puts each `x, y` location somewhere from -2 to +2 on each axis. And gives us this:

I then put this all into a function to make it easily reusable:

```function shakyLine(context, x0, y0, x1, y1, res, rand) {
const dx = x1 - x0;
const dy = y1 - y0;
const dist = Math.sqrt(dx * dx + dy * dy);

context.beginPath();
context.moveTo(x0, y0);

for (let i = res; i < dist; i += res) {
let t = i / dist;
let x = x0 + dx * t;
let y = y0 + dy * t;
x += Math.random() * rand - rand / 2;
y += Math.random() * rand - rand / 2;
context.lineTo(x, y);
}
context.lineTo(x1, y1);
context.stroke();
}
```

Now I can just call it like so:

```shakyLine(context, 100, 100, 700, 700, 10, 10);
```

And it creates a line like this:

Note that the random factor is 10 here, so it’s a lot more shaky than the earlier example. Note that the `res` and `rand` parameters both contribute in different ways to the shakiness of the line. So if I move `res` down to 5, but keep `rand` at 10, we get something more shaky, but in a tight way.

But here, I’ve bumped both `res` and `rand` up significantly:

```shakyLine(context, 100, 100, 700, 700, 50, 40);
```

This gives us a line that varies a lot more, but because each segment is longer, it’s more of a chunky random shake.

So you can play these two parameters off each other to get various effects.

The next thing to do is create a `shakyRect` function. It looks like this:

```function shakyRect(context, x, y, w, h, res, rand) {
shakyLine(context, x, y, x + w, y, res, rand);
shakyLine(context, x + w, y, x + w, y + h, res, rand);
shakyLine(context, x + w, y + h, x, y + h, res, rand);
shakyLine(context, x, y + h, x, y, res, rand);
}
```

As mentioned before, the fact that each line starts and ends on a non-shaky point means that the rectangle will be continuous. You can call it like this:

```shakyRect(context, 100, 100, 600, 600, 10, 10);
```

And get a rectangle like this:

To show how `res` and `rand` relate, I made this for loop that draws a bunch of squares on the canvas. `res` increases from left to right and `rand` increases from top to bottom. It gives you a good idea of the different effects you can create.

```for (let y = 20; y < 730; y += 70) {
for (let x = 20; x < 730; x += 70) {
shakyRect(context, x, y, 50, 50, x/730 * 10, y/730 * 10);
}
}
```

Now, this rectangle function isn’t the best because it doesn’t support fills, but you can probably work up a better solution. You might also want to create functions for drawing other shaky shapes.

Or, maybe you just want a drop in library you can use. I created a whole JavaScript `shaky` library about 7 years ago. I can’t guarantee how well it has stood the test of time. There are undoubtedly some best practices in there that could be updated. But the core code should be good enough to work out things for yourself. It supports not only lines and rectangles, but circles, arcs, quadratic curves, bezier curves, and arcs. And as of this writing, it has 101 stars, which is kind of neat!

https://github.com/bit101/shaky

All the posts in the How to Draw a Line series:

# How to Draw a Line

tutorial

… in code of course.

First, I’m going to assume that you are working in some kind of system that has a drawing API. But wait, even the simplest of drawing APIs have a function to draw a line already. All right then, we’ll start with that. Assuming you are using something like HTML, JS and Canvas, you’re going to do something like this:

```context.moveTo(x0, y0);
context.lineTo(x1, y1);
context.stroke();
```

You start with two points, defined by two x, y coordinates. You move to the first one, you line to the second one, you stroke that path. And you wind up with:

Easy. And Boring. Sure you can change the width and the color and transparency, but it’s still just a boring line. In order to do a bit more with it, let’s abandon the built-in line functionality and make our own.

The idea is to start at the first point and draw a series of small squares between it and the second point. We’ll use the Pythagorean theorem to get the distance between the two points. That will let us know how many steps we have to take. Then just linearly interpolate between the x and y positions and draw a square there.

```const dx = x1 - x0;
const dy = y1 - y0;
const dist = Math.sqrt(dx * dx + dy * dy);

for (let i = 0; i < dist; i++) {
let t = i / dist;
let x = x0 + dx * t;
let y = y0 + dy * t;
context.fillRect(x - 0.5, y - 0.5, 1, 1);
}
```

And the result of that:

Not half bad really. Of course, if you want to have a really fast and more accurate line drawing routine, you’d probably want to go with Bresenham’s algorithm or maybe Wu’s algorithm. Honestly, I’m really just referencing them so people don’t comment to correct me and tell me I should be using them. But we’re just getting started here. This still isn’t any more interesting than the built-in line function. It’s just too straight, too even, to regular. Let’s mix it up a bit.

We can start by changing the increment from `i++` to, say, `i += 5`, and get:

Now we have some space between each “pixel” that makes up the line. We can then increase the size of these “pixels”:

```context.fillRect(x - 1.5, y - 1.5, 3, 3);
```

Which has the general effect of pixel art:

In the next example, I changed the increment to 8 and used the following code to draw a circle instead of a square:

```context.beginPath();
context.arc(x, y, 4, 0, Math.PI * 2);
context.fill();
```

And got:

You can keep playing with this, making lines out of any shapes with any amount of spacing. But let’s throw in some randomness.

Instead of drawing a pixel at every single point along the line, let’s draw them at random positions along the line. We can do that just by making `t` a random value from 0 to 1. Altering the first version, we get:

```for (let i = 0; i < dist; i++){
let t = Math.random();
let x = x0 + dx * t;
let y = y0 + dy * t;
context.fillRect(x - 0.5, y - 0.5, 1, 1);
}
```

And an image that will look something like this:

Now we’re getting something a bit more interesting.

Let’s play with size and transparency next. I’ll make a `size` variable that will let us easily change the size of each pixel. And then set the transparency down real low. This will let the pixels overlap and build up more gradually in different areas. I’ll also use `dist*2` as the pixel count, which gives us a lot more pixels to play with.

```const size = 4;
context.fillStyle = "rgba(0, 0, 0, 0.05)";

for (let i = 0; i < dist*2; i++){
let t = Math.random();
let x = x0 + dx * t;
let y = y0 + dy * t;
context.fillRect(x - size / 2, y - size / 2, size, size);
}
```

And that gives us:

Finally, lets go all out, add even more pixels, vary the size, vary the transparency, even vary the position of each individual pixel.

```for (let i = 0; i < dist*4; i++){
let t = Math.random();
let x = x0 + dx * t;
let y = y0 + dy * t;
x += Math.random() * 4 - 2;
y += Math.random() * 4 - 2;
let size = Math.random() * 8;
context.fillStyle = "rgba(0,0,0, " + Math.random()*0.2 + ")";
context.fillRect(x - size / 2, y - size / 2, size, size);
}
```

Now we have something that looks kind of like ink on wet paper.

But we don’t have to limit it to straight lines. Here’s the built-in code for drawing a quadratic curve – defined by three points:

```const x0 = 100;
const y0 = 700;
const x1 = 400;
const y1 = -100;
const x2 = 700;
const y2 = 700;

context.beginPath();
context.moveTo(x0, y0);
context.stroke()
```

This gives you a bland curve:

But we can apply the same concept here. We’ll need a function that gives us a point somewhere along the curve. Fortunately, such a function is well known. We can again choose a random `t` value and start drawing random pixels. Here’s the code – note I chose an arbitrary value of 3000 for number of pixels. Use more or less to get a denser or lighter line.

```for (let i = 0; i < 3000; i++){
let t = Math.random();
let p = quadraticPoint(x0, y0, x1, y1, x2, y2, t);
x = p.x + Math.random() * 4 - 2;
y = p.y + Math.random() * 4 - 2;
let size = Math.random() * 8;
context.fillStyle = "rgba(0,0,0, " + Math.random()*0.2 + ")";
context.fillRect(x - size / 2, y - size / 2, size, size);
}

function quadraticPoint(x0, y0, x1, y1, x2, y2, t) {
const oneMinusT = 1.0 - t;
const m0 = oneMinusT * oneMinusT;
const m1 = 2.0 * oneMinusT * t;
const m2 = t * t;
return {
x: m0 * x0 + m1 * x1 + m2 * x2,
y: m0 * y0 + m1 * y1 + m2 * y2,
}
}
```

And the curve:

This looks even better drawn with circles:

This looks interesting all by itself, but I suggest you create some parameterized functions for drawing lines, curves and shapes like this and see if it doesn’t change the overall look of your creative pieces.

The whole series of How to Draw a Line:

# Probability #2

tutorial

## The Monty Hall Problem

You’ve surely heard of this one. It’s a really tough one to wrap your head around. I’ll describe the problem in order to solve it, but it’s worth looking into the background of the “Ask Marilyn” incident if you’re not familiar with it.

Behind Monty Hall’s Doors

The story shows that it’s not just hacks like myself that find this stuff non-intuitive. Lots of highly educated, very smart people got tricked by this one.

The problem is based on an old game show, “Let’s Make a Deal”. The host of the show was named Monty Hall. The contestant is shown three doors. Behind two doors are goats. Behind one is A NEW CAR!!!

The contestant chooses a door. If he chooses the one with THE NEW CAR, he gets to keep the car. If he chooses a goat door, he gets a goat. The unspoken assumption being that he’d rather have a car.

But… before the chosen prize is revealed, Monty Hall opens one of the two remaining doors, revealing a goat!

There are now two doors remaining. The one that the contestant chose, and one other one. Monty asks the contestant if he wants to stay with his choice, or if he’d like to switch to the other door.

What should he do?

To clarify, what should he do in order to optimize his chances of winning THE NEW CAR? Are the odds of winning the car better if he sticks with his original choice? Are they better if he switches? Does it just not matter?

Intuition tells us that it doesn’t matter. There are two doors. One has a goat, one has a car. The odds are 50/50. Doesn’t really matter if he sticks with the first choice or switches. The odds are the same.

The truth though? He should absolutely switch. It will significantly improve his odds of winning. In fact, he’s twice as likely to win if he switches than if he sticks with his original choice.

## But why???

If you want a deep explanation… I mean really deep, just head over to the Monty Hall Problem page on wikipedia. But I’ll give my take on it.

When the contestant first chose a door, he had a 1 in 3 chance of winning the car. That’s pretty straightforward.

When Monty opens a door, he is not randomly opening a door. He’s opening a door with a goat behind it, always. So he is using his knowledge of what’s behind each door to change the nature of the game.

Let’s say we have doors A, B, and C. A and B have goats, C has a car.

There are three possible scenarios:

1. Contestant chooses door A. Monty will open door B. If contestant switches doors, he wins.
2. Contestant chooses door B. Monty opens A. If contestant switches, he wins.
3. Contestant chooses door C. Monty opens either A or B. It doesn’t matter. If contestant switches, he loses.

So in two of the three scenarios, switching is the winning move.

Even though I’ve worked out the logic of this myself many times and done it in code more than once, it still feels vaguely magical.

## Bring on the Code

Let’s start by playing the game 1000 times in a for loop. And we’ll start by setting up the three doors with goats behind them and swapping out a car on one of them.

```// we'll play the game 1000 times.
for (let i = 0; i < 1000; i++) {
// create 3 doors with goats behind them.
let doors = ["goat", "goat", "goat"];

// randomly choose one door and put a car behind it.
let winner = Math.floor(Math.random() * 3);
doors[winner] = "car";
console.log(doors);
}
```

This outputs the 1000 random door configurations. Something like this:

```[ 'car', 'goat', 'goat' ]
[ 'car', 'goat', 'goat' ]
[ 'goat', 'car', 'goat' ]
[ 'car', 'goat', 'goat' ]
[ 'goat', 'car', 'goat' ]
[ 'car', 'goat', 'goat' ]
[ 'goat', 'car', 'goat' ]
[ 'car', 'goat', 'goat' ]
[ 'car', 'goat', 'goat' ]
...
```

Now let’s have the contestant make a choice – 0, 1 or 2. Just to check ourselves, let’s see whether he wins a car and add up how many times he wins. Out of 1000 games, he should win somewhere close to 333.

```let wins = 0;

// we'll play the game 1000 times.
for (let i = 0; i < 1000; i++) {
// create 3 doors with goats behind them.
let doors = ["goat", "goat", "goat"];

// randomly choose one door and put a car behind it.
let winner = Math.floor(Math.random() * 3);
doors[winner] = "car";
// console.log(doors);

// choose a door and count how many wins we get
let choice = Math.floor(Math.random() * 3);
if (doors[choice] === "car") {
wins++;
}
}
// should be around 333
console.log(wins);
```

That checks out for me.

Next, we let Monty open a door to reveal a goat. There are probably plenty of clever ways to code this, but I’ll just loop through the three doors and choose one that is not the same as what the contestant chose, and make sure that it’s a goat door.

```  ...
// now monty chooses another door (must be a goat!)
let montyChoice;
for (let j = 0; j < 3; j++) {
if (j != choice && doors[j] === "goat") {
montyChoice = j;
break;
}
}
console.log(doors[montyChoice]);
...
```

Just to check myself, I logged what’s behind Monty’s door. Sure enough, nothing but goats.

Now we have the contestant’s choice and Monty’s choice. We can add the code back in that calculates how many times the contestant wins the car…

```let wins = 0;
// we'll play the game 1000 times.
for (let i = 0; i < 1000; i++) {
// create 3 doors with goats behind them.
let doors = ["goat", "goat", "goat"];

// randomly choose one door and put a car behind it.
let winner = Math.floor(Math.random() * 3);
doors[winner] = "car";
// console.log(doors);

// choose a door and count how many wins we get
let choice = Math.floor(Math.random() * 3);

// now monty chooses another door (must be a goat!)
let montyChoice;
for (let j = 0; j < 3; j++) {
if (j != choice && doors[j] === "goat") {
montyChoice = j;
break;
}
}
// console.log(doors[montyChoice]);

// contestant does not switch
if (doors[choice] === "car") {
wins++;
}
}
console.log(wins);
```

This isn’t any different than the first time we counted the wins. I consistently get numbers in the low 300s. Of course, because the fact that Monty opens a door doesn’t change the fact that there was a 1 in 3 chance of the contestant winning.

But let’s see what happens if the contestant switches doors. Again, you can get fancy here, but I’ll go brute force, looping through the doors till I find the one that is neither the contestant’s choice nor Monty’s choice. And I’ll count how many times he wins with that choice.

```let wins = 0;
// we'll play the game 1000 times.
for (let i = 0; i < 1000; i++) {
// create 3 doors with goats behind them.
let doors = ["goat", "goat", "goat"];

// randomly choose one door and put a car behind it.
let winner = Math.floor(Math.random() * 3);
doors[winner] = "car";
// console.log(doors);

// choose a door and count how many wins we get
let choice = Math.floor(Math.random() * 3);

// now monty chooses another door (must be a goat!)
let montyChoice;
for (let j = 0; j < 3; j++) {
if (j != choice && doors[j] === "goat") {
montyChoice = j;
break;
}
}
// console.log(doors[montyChoice]);

// contestant does not switch
// if (doors[choice] === "car") {
//   wins++;
// }

// contestant switches
let newChoice;
for (let j = 0; j < 3; j++) {
if (j != choice && j != montyChoice) {
newChoice = j;
break;
}
}
if (doors[newChoice] === "car") {
wins++;
}
}
console.log(wins);
```

Running this I get numbers in the upper 600s! Winning roughly 2 out of 3 times, exactly as predicted.

## Summary

So there you go. The code proves things out. But it still feels a bit magical.

I did think of another though experiment that helps to make it make sense. Let’s say there were four doors – three goats, one NEW CAR. Contestant chooses one, and Monty Hall opens two of them to reveal two goats. Or better yet, there are ten doors. Contestant chooses one and Monty opens eight. The only way he’d lose by switching is if he had chosen the car to begin with. And there was only a 1 in 10 chance he did that. So if he switches now, he’s got a 9 out of 10 chance of winning! That helps my brain a bit. But still seems a bit magical.

# Probability #1

tutorial

I started re-reading The Drunkard’s Walk again today.

I read it a few years ago and remember really liking it. There are lots of examples in there of situations that seem to defy logic, or at least defy our sense of what is logical. But these are provable mathematically using the basic probability.

Whenever I come across a problem like this, even once I get my head around it as much as I can, I like to write some code to prove it out.

One of my favorite such problems is the two children problem. I haven’t gotten to it in the book yet, but I know it’s coming up. It’s a classic. Here it is, my paraphrase:

A woman says she has two children. She says one of them is a boy. What are the odds that the other one is a boy?

The obvious answer is 50%. There’s a child you don’t know about. It’s either a girl or a boy. Everything else is irrelevant, right?

Nope. Actually the odds are 1 in 3 that the other child a boy, 2 in 3 that it’s a girl.

And the really odd part of it is you can change the wording in a way that seems to make no difference, but totally changes it:

A woman says she has two children. She says the first born one is a boy. What are the odds that the other one is a boy?

In this case, yes, the odds are 50% that the other child is a boy.

To understand the odds of a particular situation occurring, such as the genders of two children, you have to consider all possible arrangements and then how many of those arrangements satisfy the criteria. Then divide.

In this case, we have a family who had one child, then another child. They may have had a boy first, then a girl. Or maybe a boy, then another boy. Or a girl and then a boy. Or a girl and another girl. That’s four possibilities:

• boy boy
• boy girl
• girl boy
• girl girl

So in the first problem, the mother says that one of her children is a boy. This narrows us down to just three possibilities.

• boy boy
• boy girl
• girl boy

In all three of those, one of the children is a boy. In two of them the other child is a girl and in only one, the other child is also a boy. So, 1 in 3 for boy, 2 in 3 for girl.

But let’s look at the second problem. Mom says that her first child is a boy. That gives us only two possibilities:

• boy boy
• boy girl

In one of those, the other child is a girl and in one, it’s a boy. 50/50.

## Prove it with Code

I’m doing this with JavaScript using node.js. But use whatever you want.

For probability situations, it helps to have a large number of samples. So let’s make 1000 families. Each family will have two children. We can represent these by strings: “bb” means they had a boy, then another boy. “gb” means they had a girl, then a boy, in that order. Likewise, “bg” means the opposite order and “gg” means they had two girls. We’ll store all the families in an array, and we’ll actually go through one by one, randomly choosing the gender of the first child, and then the second child.

```let families = [];

// create 1000 families with two randomly gendered children.
for (let i = 0; i < 1000; i++) {
// no kids yet.
let family = "";

// first child
if (Math.random() < 0.5) {
family += "b";
} else {
family += "g";
}

// second child
if (Math.random() < 0.5) {
family += "b";
} else {
family += "g";
}

families.push(family);
}
console.log(families);
```

This should give you something like the following output, with 1000 total strings.

```['gb', 'gb', 'gg', 'gb', 'gb', 'gg', 'bb', 'bg', 'bb', 'gb',
'bg', 'gg', 'gg', 'bg', 'bg', 'gg', 'gg', 'bg', 'gb', 'gg',
'gg', 'gb', 'bb', 'bg', 'gg', 'gb', 'gg', 'gb', 'bb', 'gb',
'bg', 'bb', 'gg', 'gb', 'gb', 'bb', 'bg', 'bg', 'gb', 'bb',
'gb', 'gb', 'gg', 'gg', 'bb', 'bb', 'bb', 'gb', 'gg', 'gb',
'gb', 'bg', 'gg', 'bg', 'bg', 'gb', 'bg', 'gg', 'gg', 'bg',
'bb', 'gb', 'bg', 'gb', 'gg', 'gg', 'bg', 'bg', 'gg', 'bb',
'gb', 'bg', 'gg', 'gb', 'bg', 'bg', 'bg', 'gg', 'bb', 'gb',
'gg', 'bb', 'bb', 'gg', 'gg', 'bb', 'gg', 'gg', 'bg', 'bb',
'bb', 'gg', 'gg', 'gg', 'gg', 'bg', 'gg', 'gg', 'bg', 'gg',
...
]
```

We have multiples of every type of family in there: “bb”, “bg”, “gg”, “gb”. Now, the mother said that one of the children was a boy. So let’s filter this down to only the families that have a “b” in them.

```// now lets get all the families who have at least one boy
let oneBoyFamilies = families.filter(family => family.indexOf("b") > -1);
console.log(oneBoyFamilies);
```

My var name isn’t the greatest. Consider `oneBoyFamilies` to mean “at least one boy”. This should give you something like the following output.

```['gb', 'gb', 'gb', 'gb', 'bb', 'bg', 'bb', 'gb', 'bg', 'bg',
'bg', 'bg', 'gb', 'gb', 'bb', 'bg', 'gb', 'gb', 'bb', 'gb',
'bg', 'bb', 'gb', 'gb', 'bb', 'bg', 'bg', 'gb', 'bb', 'gb',
'gb', 'bb', 'bb', 'bb', 'gb', 'gb', 'gb', 'bg', 'bg', 'bg',
'gb', 'bg', 'bg', 'bb', 'gb', 'bg', 'gb', 'bg', 'bg', 'bb',
'gb', 'bg', 'gb', 'bg', 'bg', 'bg', 'bb', 'gb', 'bb', 'bb',
'bb', 'bg', 'bb', 'bb', 'bg', 'bg', 'bg', 'bb', 'bb', 'bb',
'gb', 'bb', 'bb', 'bb', 'bg', 'bg', 'bg', 'gb', 'bb', 'bb',
'gb', 'bb', 'bg', 'bg', 'bb',
...
]
```

I don’t see any “gg”s in there, but just to be sure, we can say:

```// validate that there are no families with two girls here
console.log(oneBoyFamilies.indexOf("gg"));
```

If we’ve done that right, we should get a -1 here. I do, so I’m satisfied.

Now, to find families where the other child is a boy, we need to look for families consisting of “bb”. But for families where the other child is a girl, we need to look for either “gb” or “bg”. Already you can see why it’s 2 to 1 in favor of the other child being a girl. But here’s the code:

```let otherChildIsABoy = oneBoyFamilies.filter(family => family === "bb");
let otherChildIsAGirl = oneBoyFamilies.filter(family => family === "bg" || family == "gb");
console.log("boy: " + otherChildIsABoy.length);
console.log("girl: " + otherChildIsAGirl.length);
```

Alternately, for the girls, you could do something like:

```let otherChildIsAGirl = oneBoyFamilies.filter(family => family.indexOf("g") > -1);
```

Shouldn’t make any difference. With either method, I consistently get in the mid-200s for boys and around 500 for girls. Total would be around 750-ish, which makes sense since we filtered out one of the four arrangements (“gg”). So, 1 in 3 for boys, 2 in 3 for girls. Spot on.

Finally, let’s do the second version. Here, the mom says her first child is a boy. So for that we have to search for families where the first character is a “b”. Then we just get the count of the resulting families where the second character is a “b” and the count where it’s “g”.

```// this time, lets get all the families where the FIRST child is a boy
let firstBoyFamilies = families.filter(family => family.charAt(0) === "b");
console.log(firstBoyFamilies);

let secondChildIsABoy = firstBoyFamilies.filter(family => family.charAt(1) === "b");
let secondChildIsAGirl = firstBoyFamilies.filter(family => family.charAt(1) === "g");

console.log("boy: " + secondChildIsABoy.length);
console.log("girl: " + secondChildIsAGirl.length);
```

The results you see here will vary. Sometimes you’ll get more boys than girls as the second child. Sometimes the opposite. Occasionally they’ll be dead even. We’ll call that 50%.

## Why am I doing this?

This is firmly in the realm of recreational mathematics. I just find it really interesting to prove these things out even when my brain doesn’t agree 100% all the time. I enjoy doing it and I’ll probably do some more.

#### PS:

Here’s all the code in one spot:

```let families = [];
// create 1000 families with two randomly gendered children.
for (let i = 0; i < 1000; i++) {
// no kids yet.
let family = "";

// first child
if (Math.random() < 0.5) {
family += "b";
} else {
family += "g";
}

// second child
if (Math.random() < 0.5) {
family += "b";
} else {
family += "g";
}

families.push(family);
}
console.log(families);

// now lets get all the families who have at least one boy
let oneBoyFamilies = families.filter(family => family.indexOf("b") > -1);
console.log(oneBoyFamilies);

// validate that there are no families with two girls here
console.log(oneBoyFamilies.indexOf("gg"));

let otherChildIsABoy = oneBoyFamilies.filter(family => family === "bb");
let otherChildIsAGirl = oneBoyFamilies.filter(family => family === "bg" || family == "gb");
// alternately…
// let otherChildIsAGirl = oneBoyFamilies.filter(family => family.indexOf("g") > -1);

console.log("boy: " + otherChildIsABoy.length);
console.log("girl: " + otherChildIsAGirl.length);

// this time, lets get all the families where the FIRST child is a boy
let firstBoyFamilies = families.filter(family => family.charAt(0) === "b");
console.log(firstBoyFamilies);

let secondChildIsABoy = firstBoyFamilies.filter(family => family.charAt(1) === "b");
let secondChildIsAGirl = firstBoyFamilies.filter(family => family.charAt(1) === "g");

console.log("boy: " + secondChildIsABoy.length);
console.log("girl: " + secondChildIsAGirl.length);
```

# Grep and Sed, Demystified

tutorial

I’ve kind of half understood `grep` for a while, but assumed that I didn’t really get it at all. I thought I knew nothing at all about `sed`. I took some time this weekend to sit down and actually learn about these two commands and discovered I already knew a good deal about both of them and filled in some of what I didn’t know pretty easily. Both are a lot more simple and straightforward than I thought they were.

## Grep

`grep` comes from “global regular expression print”. This is not really an acronym, but comes from the old time `ed` line editor tool. In that tool, if you wanted to globally search a file you were editing and print the lines that matched, you’d type `g/re/p` (where `re` is the regular expression you are using to search. The functionality got pulled out of `ed` and made into a standalone tool, `grep`.

### Basics

`grep`, in its simplest use, searches all the lines of a given file or files and prints all the lines that match a regular expression. Syntax:

`grep <options> <expression> <file(s)>`

So if you want to search the file `animals.txt` for all the lines that contain the word `dog`, you just type:

`grep dog animals.txt`

It’s usually suggested that you include the expression in single quotes. This prevents a lot of potential problems, such as misinterpretations of spaces or unintentional expansion:

`grep 'flying squirrel' animals.txt`

It’s also a good idea to explicitly use the `-e` flag before the expression. This explicitly tells `grep` that the thing coming next is the expression. Say you had a file that was list of items each preceded with a dash, and you wanted to search for `-dog`

`grep '-dog' animals.txt`

Even with the quotes, `grep` will try to parse `-dog` as a command line flag. This handles it:

`grep -e '-dog' animals.txt`

You can search multiple files at the same time with wildcards:

`grep -e 'dog' *`

This will find all the lines that contain `dog` in any file in the current directory.

You can also recurse directories using the `-r` (or `-R`) flag:

`grep -r -e 'dog' *`

You can combine flags, but make sure that `e` is the last one before the expression:

`grep -re 'dog' *`

A very common use of `grep` is to pipe the output of one command into `grep`.

`cat animals.txt | grep -e 'dog'`

This simple example is exactly the same as just using `grep` with the file name, so is an unnecessary use of `cat`, but if you have some other command that generates a bunch of text, this is very useful.

`grep` simply outputs its results to `stdout` – the terminal. You could pipe that into another command or save it to a new file…

`grep -e 'dog' animals.txt > dogs.txt`

### Extended

When you get into more complex regular expressions, you’ll need to start escaping the special characters you use to construct them, like parentheses and brackets:

`grep -e '\(flying \)\?squirrel' animals.txt`

This can quickly become a pain. Time for extended regular expressions, using the `-E` flag:

`grep -Ee '(flying )?squirrel' animals.txt`

Much easier. Note that `-E` had nothing at all to do with `-e`. That confused me earlier. You should use both in this case. You may have heard of the tool `egrep`. This is simply `grep -E`. In some systems `egrep` is literally a shell script that calls `grep -E`. In others it’s a separate executable, but it’s just `grep -E` under the hood.

`egrep -e '(flying )?squirrel' animals.txt`

### Other Stuff

The above covers most of what you need to know to use basic `grep`. There are some other useful flags you can check into as well:

`-o` prints only the text that matches the expression, instead of the whole line

`-h` suppresses the file name from printing

`-n` prints the line number of each printed line.

#### Simple Text Search

If the thing you are searching for is simple text, you can use `grep -F` or `fgrep`. All the same, but you can’t use regular expressions, just search for a simple string.

```grep -Fe 'dog' animals.text
fgrep -e 'dog' animals.text```

#### Perl

There’s also `grep` with Perl syntax for regular expressions. This is a lot more powerful than normal `grep` regex syntax, but a lot more complex, so only use it if you really need it. It’s also not supported on every system. To use it, use the `-P` flag.

`grep -Pe 'dog' animals.text`

In this simple case, using Perl syntax gives us nothing beyond the usual syntax. Also note that `pgrep` is NOT an alternate form of `grep -P`. So much for consistency.

## Sed

I thought that I knew next to nothing about `sed`, but it turns out that I’ve been using it for a few years for text replacement in `vim`! `sed` stands for “stream editor” and also hails from the `ed` line editor program. The syntax is:

`sed <options> command <file(s)>`

The two main options you’ll use most of the time are `-e` and `-E` which work the same way they do in `grep`.

The most common use of `sed` is to replace text in files. There are other uses which can edit the files in other ways, but I’ll stick to the basic replacement use case.

Like `grep`, `sed` reads each line of text in a file or files and looks for a match. It then performs a replacement on the matched text and prints out the resulting lines. The expression to use for replacement is

`s/x/y/`

where `x` is the text you are looking for, and `y` is what to replace it with. So to replace all instances of `cat` with the word `feline` in `animals.txt`

`sed -e 's/cat/feline/' animals.txt`

Note that `sed` will print every line of text in the file, whether or not it found a match or not. But the lines that it matched will be changed the way you specified.

After the final slash in the expression, you can add other regex flags like `g` for global or `i` for case insensitivity.

Like `grep`, `sed` just outputs to `stdout`. You can redirect that to another file using `>` or pipe it to another process using `|`. But do NOT save the output back to the original file. Try it out some time on a test file that you don’t care about and see what happens.

There are lots of other options you can use with `sed` but the above will probably get you by for a while to come. As you need more, just read up.

## Summary

The biggest thing I took away from the couple of hours I spent on this was actually how easy these two commands were to learn. I’d been avoiding doing so for a long time and now wish that I had spend the effort on this much earlier.

# New Shell Script Shortcut

tutorial

I’m often making shell scripts for various things – sometimes just a quick one for a specific task, sometimes just to test something out, sometimes for some kind of workflow task that I plan to keep around.

It’s always the same steps:

1. Create the file.
2. Add the header: `#! /bin/bash`
3. Write the code.
4. Save it.
5. Exit the editor.
6. Make it executable with `chmod +x <filename>`
7. Run, test, edit, etc.

When you’re repeating yourself, time for some automation. So I wrote a shell script that creates shell scripts.

```#! /bin/bash
if [ -f "\$1" ]
then
exit 1
fi
echo '#! /bin/bash' > \$1
chmod +x \$1
\$EDITOR \$1```

Create a file, add that code, make it executable (sound familiar?) and put it somewhere in your path. I named mine `newss` for new shell script.

Now you can just say:

`newss test.sh`

And you are editing an executable shell script. As you can see, it checks to see if the file exists as a minimum level of safety. It will follow any paths, such as:

`newss foo/bar/test.sh`

But only if the directories exist. Creating non-existent directories wouldn’t be too hard if that’s important to you.

# Animated Sinusoidal Cardiods

tutorial

I think I just made up a thing. Usually when I think that, it just means fewer than a few hundred people have thought about it before me, so who knows.

Let’s start with cardioids. A cardioid is a heart-shaped curve. One way to create a cardiod is to roll a circle around another circle of the same size, tracing the path of a single point on the moving circle. Like so:

I discovered another neat way to create a cardioid while checking out the math art challenge.

#MathArtChallenge Day 7: Cardioids!

In this method, you divide a circle into an arbitrary number of points around the radius. Then, for each point `n`, you draw a line from point `n` to point `n*2`. Point 1 to 2, point 2 to 4, point 3 to 6, etc.

Here’s some JavaScript/Canvas code showing this in action:

`context.translate(400, 400);const radius = 350;const res = 100;const slice = Math.PI * 2 / res;const mult = 2;context.beginPath();for(let i = 1; i < res; i++) {  let a1 = slice * i;  let a2 = slice * i * mult;  context.moveTo(Math.cos(a1) * radius, Math.sin(a1) * radius);  context.lineTo(Math.cos(a2) * radius, Math.sin(a2) * radius);}context.stroke();`

And here’s what that gives you:

You’ll notice in the code that I’ve divided the circle into 100 points, which is is rather low-res. If I up that to 360, we get something nicer:

So I’m calculating the points by getting two angles, `a1` and `a2`, calculating those with `slice * i` and `slice * i * mult` as described above. `slice` being a full circle divided by `res` and `mult` equals 2 for now.

`   let a1 = slice * i;   let a2 = slice * i * mult;`

What if I change that to `mult = 3` instead?

Or, `mult = 4` ?

You see that for each multiplier, `m`, you get `m-1` nodes in the cardioid. Let’s just go crazy and see what happens when we set `res` to 1440 points and `mult` to 25:

Here’s the code for those of you following along at home:

`context.translate(400, 400);const radius = 350;const res = 1440;const slice = Math.PI * 2 / res;const mult = 25;context.lineWidth = 0.25;context.beginPath();for(let i = 1; i < res; i++) {  let a1 = slice * i;  let a2 = slice * i * mult;  context.moveTo(Math.cos(a1) * radius, Math.sin(a1) * radius);  context.lineTo(Math.cos(a2) * radius, Math.sin(a2) * radius);}context.stroke();`

All very interesting, but I wanted to start changing things up even more. I decided that rather than using a simple circle, what if I varied the radius of the circle with a sine wave? Here’s the code I came up with:

`context.translate(400, 400);const radius = 300;const res = 1440;const slice = Math.PI * 2 / res;const mult = 5;const waves = 6;context.lineWidth = 0.25;context.beginPath();for(let i = 1; i < res; i++) {  let a1 = slice * i;  let a2 = slice * i * mult;  let r1 = radius + Math.sin(a1 * waves) * 100;  let r2 = radius + Math.sin(a2 * waves) * 100;  context.moveTo(Math.cos(a1) * r1, Math.sin(a1) * r1);  context.lineTo(Math.cos(a2) * r2, Math.sin(a2) * r2);}context.stroke();`

First I created a `waves` constant that controls how many sine waves will go around the circle. Then an `r1` variable that is based on radius, multiplied by the sine of `a1 * waves` times 100. And an `r2` variable based on `a2 * waves`. So for each point, it’s radius will get larger and smaller as they progress around the circle. The result (setting `mult` back to 5):

You can get all kinds of interesting shapes by varying how many nodes and how many waves and the size of the waves and the resolution.

Of course, I had to have a go at animating these. The first idea was to vary the height of that radial wave. Here, it’s going back and forth from -80 to +80:

I’m not going to give the source for the animation examples, because it was written in another system entirely, but if you’ve followed along so far, you’ll be able to figure it out.

Next, I thought about varying the phase of that radial wave, so that the wave itself seemed to be animating around in a circle. This produced some really striking animations. I’ll close the article by posting animations for 2, 3, 4, 5 and 6 wave animated sinosoidal cardiodids. Enjoy!

# Perlinized Hexagons

tutorial

In my last post, Hexagons, I talked a bit about how cool hexagons are, and gave one method of creating a hex grid. I also used this image as a header:

This was a bit of a tease, as I didn’t show how to create such an image. But I promised I’d come back around and give some code for it, so here we go.

### First, some refactoring

As I’m sure most of you guessed, this image uses Perlin noise, but you could use any other function to get different textures. Check out my articles on flow fields (parts one and two) for more ideas along these lines. I’ll also be using the same Perlin noise library mentioned in the second of those articles.

I’m going to take the basic code from the tiled hexagons in the last article and change it up a bit. Mainly all I’m doing here is removing the translation code and calculating each x, y drawing point directly. Here’s what that looks like:

``````const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
context.lineWidth = 0.5;

function hexagon(x, y, r) {
let angle = 0;
for(let i = 0; i < 6; i ++) {
context.lineTo(x + Math.cos(angle) * r, y + Math.sin(angle) * r);
angle += Math.PI / 3;
}
context.closePath();
}

const ydelta = Math.sin(Math.PI / 3) * radius;
let even = true;

for(let y = 0; y < 900; y += ydelta) {
let offset = 0;
let yy = y;
if(even) {
}
for(let x = 0; x < 900; x += radius * 3) {
context.beginPath();
context.stroke();
}
even = !even;
}``````

Here’s what that gives us. Notice that I made the radius much smaller and stroked it instead of filling it.

Again, this all builds off of the last article. I had to remove the translation code because I need the exact x, y coordinates of every point of every hexagon, so I can feed that into a Perlin noise function to know how much to shift it.

The Perlin function is going to make use of this library: https://github.com/josephg/noisejs. Feel free to add that to your project however you’d like. Or add a different noise function. It doesn’t really matter which one you use.

I need a function that’s going to take an x, y coordinate, calculate an offset based on Perlin noise, and return a new, shifted x, y coordinate. Since JavaScript doesn’t let us return multiple values, I’ll return and object with x, y properties. Here’s the function:

``````function perlinize(x, y) {
const scale = 0.01;
const strength = 20;
const angle = noise.perlin2(x * scale, y * scale) * Math.PI;
return {
x: x + Math.cos(angle) * strength,
y: y + Math.sin(angle) * strength,
}
}``````

To revisit how a Perlin flow field works, we’re going to use 2-dimensional Perlin noise, feeding it an x, y value. Since the numbers we’re using will be in the hundreds or even thousands for pixel values, we’ll scale that down a bit with a scale value. This function returns a value from -1 to +1. Other Perlin noise functions sometimes return values from 0 to 1. Either will work just fine, but might need some tweaking. I take that value and multiply it by PI, which will give me a number from -PI to +PI. We’ll call that an angle in radians. In degrees, it would be -180 to +180. And we’ll use the sine and cosine of that angle to create an offset based with a customizable strength. Then, we return the original x, y coordinate + that x, y offset.

Now we just need to alter our code to use the `perlinize` function. I’ll just show the relevant function:

``````function hexagon(x, y, r) {
let angle = 0;
for(let i = 0; i < 6; i ++) {
const p = perlinize(x + Math.cos(angle) * r, y + Math.sin(angle) * r);
context.lineTo(p.x, p.y);
angle += Math.PI / 3;
}
context.closePath();
}``````

And this is where this gets us.

Each hexagon still perfectly tiles because the Perlinized offset for each shared point of each hexagon should be exactly the same, at least down to an adequate degree of precision.

Now let’s add some shading. Basically we just want a grayscale value for each hexagon. To enhance the effect, the shading should be coordinated with the offset flow field values, so we’ll use the same Perlin noise settings. Here’s a function that will accomplish that:

``````function getShading(x, y) {
const scale = 0.01;
const value = (noise.perlin2(x * scale, y * scale) + 1) / 2;
const shade = 255 - value * 255;
}``````

This takes an x, y coordinate and returns and rgb color string. Remember that this particular noise function returns -1 to +1. I’ll add 1 to that to have a range of 0 to 2, then divide by 2 to get 0 to 1. Then I’ll calculate a shade value by subtracting that normalized value by 255 and subtracting from 255. A bit convoluted, I know. This is all made a lot easier if you have a map range function. It would look something like this:

``````const n = noise.perlin2(x * scale, y * scale);
const shade = map(n, -1, 1, 255, 0);``````

To see how a map function would work, check out this video.

Finally, we just need to alter our drawing code to set the fill color based on the x, y of each hexagon, and do a fill before the stroke:

``````  for(let x = 0; x < 900; x += radius * 3) {
context.beginPath();
context.fillStyle = getShading(x + offset, y);
context.fill();
context.stroke();
}``````

And here is the result:

### Summary

Don’t stop here. There are all kinds of variables and different rendering techniques to experiment with here. This is all just about taking two different concepts – hex grids and Perlin noise – and combining them in a creative way. This worked out pretty well, but there are an infinite other combinations waiting for you to discover.

#### Late breaking comment…

One thing I had in writing this but totally spaced out on while writing the article is that this code is extremely unoptimized. Every single x, y point here is calculated and then “perlinized” three times. OK, some of the ones on the edges only get calculated only once or twice, but you get the point.

Strategies for optimizing would probably involve creating a single point grid one time and then using a loop to draw using the pre-calculated points in that grid.

This is left as an exercise for the reader.™

# Hexagons

tutorial

I know what you’re thinking. I’m thinking it too. So let’s stop tiptoeing around the subject and just say it out loud:

Hexagons are freaking cool.

Oh… you weren’t thinking that? Oops. My bad. I just assumed. Must be just me. So let me state my case on why hexagons are so damn neat.

First, let’s get some definitions out of the way. A hexagon is a six-sided polygon. And just to be clear, the hexagon I’m talking about is the regular, convex polygon type of hexagon. Which is to say that all of its sides are the same length and all of its angles are the same (120 degrees).

1. A hexagon is made up of six equilateral triangles. Each triangle is composed of three 60-degree angles, and three sides that are the same length, which is the radius of the hexagon.
2. This means that each side of the hexagon is the same length as its radius.
3. Hexagons are one of the three regular tessellations. That means you can take a bunch of hexagons and pack them together perfectly with no space left over. The only other two regular polygons you can do that with are squares and triangles. And in a sense a hexagon tiling and triangle tiling wind up being exactly the same thing. See #1 above.
4. Hexagons appear pretty regularly in nature. The bounds of any snowflake, the cells of a beehive, basalt columns, the compound eyes of insects, the packing of bubbles, the cloud formations on the north pole of Saturn.

By NASA/JPL-Caltech/Space Science Institute – http://www.nasa.gov/sites/default/files/pia18274_full.jpg, Public Domain, https://commons.wikimedia.org/w/index.php?curid=33952464

5. When you tile hexagons, exactly six hexagons will surround every other hexagon. If you connect the center points of the six surrounding hexagons, you get another, larger hexagon.
6. If you connect every other point of a hexagon to its center, you have a perfect isometric cube.

All of these points, and more, make hexagons a great shape to play with if you’re doing any kind of creative coding.

## So, you want to draw a hexagon…

Now you’re saying, “Gosh, Keith. These hexagons sound wicked pissah! How can I draw one???” Well, let me tell you.

You need three parameters, the x, y position and a radius. You can also throw in a rotation parameter, but I’ll start without that. I’m going to do this in JavaScript, and assume there’s an 800×800 canvas element on the page with an id of “canvas”. You should be able to easily convert this code to whatever language you are working with.

``` const canvas = document.getElementById("canvas"); const context = canvas.getContext("2d"); function hexagon(x, y, r) { context.save(); context.translate(x, y); let angle = 0; for(let i = 0; i < 6; i ++) { context.lineTo(Math.cos(angle) * r, Math.sin(angle) * r); angle += Math.PI / 3; // 60 degrees } context.closePath(); context.restore(); } context.beginPath(); hexagon(400, 400, 100); context.stroke(); ```

Run that, and we get a very nice hexagon. You can fill it instead of stroke it, make a bunch at different positions and of different sizes and colored differently. I’m not going to dive into the code there much, as it’s pretty simple.

Note, that I’m keeping it super simple here. You can optimize this, you can rework this, make it more reusable, make it draw regular polygons of any number of sides, pass in a rotation value and set angle to that instead of zero, and on and on. Maybe you want to pass in the context, or make the function a method of the context object. And you should do all of that. Or some of that. Or however much of that you want to do. But I’m in charge today, and we’re doing hexagons. And we’re doing them simple.

## What about that tessellation stuff?

Yeah, now let’s make a bunch of hexagons and stick them together perfectly to see them tile. With squares, this is really easy. Loop through on the y and x axes and draw a square at each point. With hexagons, it’s a bit trickier. There are certainly multiple ways to go about this. I’ll give you one method, but if you come up with something that fits your needs better, go for it.

First of all, note that with the hexagon we drew above, it’s a bit wider than it is tall. Here, I’ve draw lines from the center to each point of the hexagon.

We can see that the width is two times the radius. Simple. But the height is a bit less. Knowing a bit of trigonometry, we can figure out that the distance from the center to the top of the hexagon is the sine of 60 degrees times the radius. So the height of the hexagon is twice that.

Here’s a hex grid.

The main thing to note is that unlike a grid of squares or rectangles, each alternate row or column overlaps with the previous and next row or column. If you consider we have even rows and odd rows, the even rows are horizontally offset from the odd rows by 1.5 times the radius. To see that, consider the horizontal distance from the center of one hexagon to the center of one of the hexagons immediately to its left or right, but in an alternate row.

Within each row, the center-to-center horizontal distance between each hexagon is 3 times the radius. To see that, consider the distance from the center of one hexagon to the center of the hexagon directly to its left or right in the same row.

And vertically, each row is offset from the previous row by one half a hexagon height, or sin(60) * radius.

Knowing all of that, we can put together a double for loop that draws a grid.

``` const radius = 50; const ydelta = Math.sin(Math.PI / 3) * radius; let even = true; for(let y = 0; y < 900; y += ydelta) { context.save(); if(even) { context.translate(radius * 1.5, 0); } for(let x = 0; x < 900; x += radius * 3) { context.beginPath(); hexagon(x, y, radius); context.stroke(); } context.restore(); even = !even; } ```

I’ll walk through this code quickly. First we set the `radius` to whatever. 50 in this case. Then we calculate how far apart to place each row. We said that was `sin(60) * radius`. 60 degrees is PI / 3 radians. And I’ll set an `even` variable to true to know whether we are on an even or odd row.

Then we create a double for loop on y and x. I have an 800 pixel canvas, but I looped through to 900 on each axis so I didn’t wind up with any empty space on the right or bottom edges. The y loop increments by `ydelta` and the x loop increments by `radius * 3`, all as discussed above.

If we’re on an even row, I’ll translate the canvas by `radius * 1.5`. Note that I did a `context.save()` before that, and follow up with a `context.restore()` at the end of the loop.

Inside the inner x loop, I just draw the hexagon. And at the very end of the loop I set `even` to `!even` to make the next row be odd, meaning it will not get that horizontal translation applied.

And now you can make a hex grid. Of course, you can fill each hexagon instead of stroking it. If you used different colors, that works out well, but if you are using all the same color, they’re all going to blend in together. A trick to handle that is to reduce the radius you pass into the hexagon function like so:

``` context.beginPath(); hexagon(x, y, radius - 5); context.fill(); ```

This gives you a 10 pixel border between each hexagon (5 pixels on each side).

Another strategy for filling a space with hexagons is to start with a single hexagon at the center and draw six new hexagons around that one and continue out in rings from there.

Hexagons are also a common tiling pattern in various strategy games. In that case, you need a bit more logic to figure out which six tiles are adjacent to any given tile and how to move from one to the next. Beyond the scope of this tutorial.

## But what about that cool image at the top with the warped, 3d-looking hexagons?

Yeah, that one is pretty neat, isn’t it? Try it for yourself. I’ll write up another article on how I did that soon.