Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, December 20, 2010

Shapematron!

Right, exciting new knitty programming plan!
One of the harder things to do with a knitting pattern is find out what shape from first principles.
In my first Geometric Knitting colloquium I discussed the relationship between Riemannian metrics and the shape of knitting fabrics, and an algorithm which would take a metric and generate a knitting pattern to yield a surface with that metric. The plan this time is to work in the opposite direction -- to take a pattern and figure out the shape this makes.

The way I want to do this is to model the fabric as a network, each stitch representing a vertex, with connections between it and its neighbours. By modelling the forces acting between stitches it should be fairly easy to make an program which will iterate towards an equilibrium position, which would be the shape the knitting will naturally take. (This position won't be unique,
I think there are two main forces to take into account -- there's a spring-like force between neighbouring stitches, acting along the line between them. Then there's a twisting force acting perpendicular to the surface, which depends on the type of stitch, which makes stocking stitch fabric curl up, and allows ribbed fabric to scrunch together.

So that's the aim, to write a program which will take a knitting pattern, interpret this as a network of points, will work out the shape of the fabric, and display this shape. The last step may well be the hardest one, since I currently have no idea how to do 3D graphics, but I'm hoping this is a standard enough problem that I'll be able to find a good standard way to do that.

Exciting!
Hugh.

Saturday, July 24, 2010

Linked list knitting

Bit of an odd project to talk about today (also, if you know me on facebook you may have seen this already.
The idea is to implement a system of knitting using linked lists. There have been quite a few recent experiments into knitting with unusual media, whether it's carrier bags, wire, human hair, or even fibre-optic cable -- you can knit with just about anything which can be made into threads. You could think of this as a really extreme version of this -- a linked list is essentially a thread of data, so why not knit with this? (Or you could see it as just a terrible pun being taken too far, which is probably more accurate.)
So here I've designed a system of knitting using these lists, and implemented it in C.

So, linked list is a data structure in which each data point contains the address for the next piece of data, so to read it all you start at one point and move along the chain following the addresses. I'll actually be using doubly-linked lists -- for these each data point contains two references, which means the chain can self-intersect.

To come up with a system of knitting using these lists, we need to be able to form loops, and to pass loops through each other. This can be done as follows:


This is really all you need to start knitting -- forming a loop corresponds to casting on, we know how to knit into a stitch, and we can cast off by simply threading the 'yarn' through the stitch without forming a loop.
It would also be possible to give analogues for many other knitting concepts -- you can purl by reversing the orientation when you knit, you could Kfb by passing two loops through the stitch, Make by simply forming a new loop in between stitches, K2tog by passing the new loop through two previous stitches, and so on.

So I thought it would be fun to actually implement this, and you can see the results here:
llknitting.c
tensionsquare.c
ribbedcuff.c
linkedlist.h
The first file contains the details of the implementation, the second is a set of instructions for knitting a small stocking stitch swatch, and the the third gives a 2*1 ribbed cuff knit in the round -- hopefully you will be able to see how these correspond to a real knitting pattern. (The final file is the header) If you're using linux you can compile these with the commands:
gcc tensionsq.c llknitting.c
Or:
gcc ribbedcuff.c llknitting.c
I'm not too sure what the equivalent command would be in Windows.
The output from each program gives the positions of the start of each stitch in the array, which isn't terribly illuminating, but hopefully convinces you that it's actually doing something. (I decided not to worry too much about outputting data since it's really the program which is the interesting part.)

So there you go -- that's how to knit with linked lists. If, for some reason, you wanted to do that. So, linked list crochet?

Happy knitting, and/or data structuring!
Hugh.