Apr 23 2009

Guest Post: A Mathematician Designs a Pattern

Published by The Knitternaut at 4:08 am under Guest Posts

Mr. K is currently working on designing a knitted globe as a toy for Bug (or at least, that was the original excuse), and was kind enough to write up a post on his process.  Let’s just say that his process differs a little from mine.  Enjoy! - K’naut

A Mathematician Designs a Pattern - by Mr. K

I decided that I wanted to design a pattern for a knitted globe — I figured I’d start with a two-color one (just green and blue) and see how that goes; later on, I’d add in white for the ice caps.  The trouble is, I am not really an artist.  Rather, I am a mathematician who knows a few things about computers.  So I decided to start with some sort of satellite map of the earth, and attack it with computer programs until it was a knitting pattern.  This, however, presented a series of problems.

Problem 1: Find a map.

It turns out that such maps of the earth are rather easy to find on the internet.  They are used in movies, video games, astronomy simulation programs etc… basically whenever somebody wants to use computer graphics to render the earth in 3D, they start with a blank sphere and then wrap an image of the earth around it.  This image is called a “texture map”, and it is usually a 2×1 rectangular map in latitude-longitude coordinates.  The vertical edge of the map is latitude, running from 90 degrees south to 90 degrees north, and the horizontal edge is longitude, running from 0 to 360 degrees.  Here’s a particularly nice texture map from NASA’s “blue marble” earth imaging project (which I didn’t wind up using) as an example.

It turns out that most of these Earth images are licensed for noncommercial use only, so I guess it’s going to be a free pattern.  Sadly, my dreams of early retirement are now dust.

Problem 2: The map has way too many colors!

I wound up using a different map — a two-color version, which computer graphics people use to distinguish land from water — which is essentially a very similar map but with only black and white.  Downside: no arctic ice cap, because there’s no land under it

Problem 3: Too big!

I basically want to turn each pixel on the map into a stitch. However, the map is way too big — it’s 2048 x 1024 pixels.  Moreover, stitches aren’t square.  For example, for a certain weight of wool, you get about 20 stitches x 32 rows = 4 inches– so stitches are usually about 3 units wide and 2 units high.  I solved both problems with the amazing ImageMagick suite of Unix command-line image manipulation programs: I think I did something like

convert -filter point -affine 2,0,0,3,0,0 -transform texturemap.jpg pattern.png
mogrify -filter point -resize 96×72 pattern.png

The first command “stretches” the map, to deal with the unequal dimensions of the stitches, using an affine transformation.  The second one scales the map down, giving me a map 96 stitches wide at the equator.  The “filter point” part means that I don’t want the computer to do any antialiasing (putting in some grey pixels to make the edges appear less jagged).  Remember, I want my final image to still have only two colors: blue and green, land and water.

Problem 4: Distorion

This type of map seriously distorts the scale of objects near the poles (because every circle of latitude, even the tiny ones near the poles, get stretched out to be the whole width of the map).  I have to “undo” that distortion so that Antarctica, Greenland, etc. are the proper size. At the same time, I have to make the pattern generate a sphere rather than a rectangle, so I need to add lines of decrease.

How big around should each circle of latitude be?  This is an easy exercise in Grade 10 trigonometry.  If theta is the angle from the north pole down to a latitude circle, then the radius of the circle should be sin(theta), and the circumference is proportional to the radius.  I want to leave the circumference of the equator unchanged.  So if my image now has 72 rows and 96 columns, the nth row should have about

96 * sin((n/72) * pi)

stitches.  I decided that I wanted my globe to have 6 lines of decrease, basically because the Knitternaut knitted me a fine winter hat (Ravelry link) with six decreases in it. So I round off the above number to be divisible by 6, rescale each row independently to be that wide, and divide the stitches into 6 blocks.  I wrote a little perl script to do this.  At this point, I was growing weary of dealing with a 72×96 image (rather tiny!) so I got it to output text instead.

Because the resolution of this image was so low, there were a few geographical problems:  the Mediterranean, the Persian Gulf and Hudson’s Bay became closed up, while South America became completely disconnected from North America.  Etc.  I fixed those things manually.

Note also that I’m cheating a little bit: knitting in the round makes a big long spiral, rather than distinct latitude circles.  So really I should have rotated the map slightly before doing any of this… but I couldn’t be bothered.

Problem 5:  It turns out it’s actually kind of hard to read that.

…so I wrote another little perl program to translate it into postscript (using a white box for ocean, and a grey box for land, sized like a stitch).  It also added horizontal and vertical lines every 5 stitches and numbered the rows for me.  Finally I used another Unix command-line utility (ps2pdf) to change that into a pdf document.

Problem 6:  Ummmm…

Now all I have to do is figure out what my pattern actually means (i.e. how to interpret the increases and decreases) and to learn to do colourwork!  No problem, right?

-Mr. K.

5 Responses to “Guest Post: A Mathematician Designs a Pattern”

  1. Alanaon 24 Apr 2009 at 4:44 pm

    Dude! DUDE! Dude.

    That’s all I’ve got.

  2. Deborahon 15 May 2009 at 1:32 pm

    I haven’t found a pattern, but an actual knitted globe is pictured on the cover of the book “Knitting for Good” by Betsy Greer. Perhaps it can inspire you!

  3. Snapdragonon 05 Dec 2009 at 11:58 am

    Go for it - longing to see the pattern :) - I’m currently trying to knit one (without the mathematics)

  4. Procrastaknitteron 03 May 2010 at 12:35 pm

    here’s the globe I knit, a-la-cheaters way-I added the countries using embroidery as I was knitting……http://procrastaknitter.blogspot.com/2009/04/earth-day-project.html

  5. Mr. Kon 04 May 2010 at 6:55 am

    Snapdragon: I did wind up knitting it and stuffing it — it came out slightly grape-shaped, but otherwise I’m really happy with it. I’ll see if I can dig up the pattern, get The Knitternaut to photograph it for me, and make a follow-up post.

    Procrastaknitter: That’s a really nice globe! in a lot of ways, it’s better than mine, since the continents pop out so nicely. I don’t think it’s cheating at all — it’s more that you’ve got the skillset of an artist, whereas I essentially don’t. If I were to try to hand-embroider the continents, it definitely wouldn’t have looked recognizable.

    However, what I did was definitely easy for me — it just took a half-hour of computations, and a night or two of perl programming (an activity to which I’m quite accustomed), and I had myself a pattern that I could easily follow to produce a globe. Just goes to show you how many different ways there are to solve a problem!

Trackback URI | Comments RSS

Leave a Reply