Monday, May 28, 2018

Polygonal knitting!

Hi folks, this is a quick little computational knitting thing.  Years ago, I looked into how you could work out knitting pictures into circular knitting - this is a little tricky because you first need to convert your picture into radial coordinates, but you would also like to account for the knitting not being perfectly circular (because there's pooling with the increases).  The solution I came up with was to purposely knit a polygon, then map these into 2D cartesian coordinates.  This, along with some radial curves led to this: https://www.ravelry.com/projects/Jhadur/aromantic-hat

This was quite cumbersome, took a lot of set-up, and particularly required me to express the heart shape as a mathematical equation - so it's pretty hard to generalise.  What I've aimed to do this time is to come up with a system which makes all of this automatic, using the existing declarative knitting framework I've got set up.
So, how does it work? You provide an image, and a target number of colours.  The system then picks out the dominant colours and flattens the image to use just those (this is important because I want to be able to do this with photos as well as block graphics).  Here's the sample image I made to test this with:
Next, you specify the number of sides you would like your polygon to have.  The system uses this to work out a pattern of increases.  The trick here is that you want an average of ~4 increases per row, and will have increases at both ends of each side.  Once that's done, we can take the location of each stitch in that pattern and work out it's location in cartesian coordinates - this isn't quite radial, instead you need to find the side it's on and interpolate between the two vertices.  This can then be output as a spreadsheet:
Of course, it's quite hard to know if that's right!  It looks plausible, but it's hard to be confident without doing a test knit.  So using this pattern, I made these two little doilies!  The shapes aren't perfect, but I think a lot of that is because the stranding has altered the tension slightly.  I'm definitely impressed with how close it came, and it's certainly better than I could have done by hand!

Next step with this will be to write up another little modules for Declarative Knitting Tools which provides a web front end to kick it all off!

Happy knitting,
Hugh.