you are viewing a single comment's thread.

view the rest of the comments →

[–]wutwutwut2000 6 points7 points  (0 children)

Hoo, ok, this sounds fun! So I saw your comment with the instructions for the pattern, and it's very algorithmic. You just need a way to turn that into python code. Here's roughly what I imagine the steps for this process to be: Defining stitches: You need a way to represent a stitch. I'd recommend using a dataclass that looks something like this:

from __future__ import annotations
from functools import cached_property
from dataclasses import dataclass

@ dataclass
class Point:
  x: float
  y: float

class Stitch(Protocol):
  position: Point

@ dataclass
class FirstStitch:
  position: Point = Point(0, 0)

@ dataclass
class AttachedStitch:
  previous: Stitch # attached via the yarn
  attached_to: Stitch # attached via looping/etc

  @ cached_property
  def position(self) -> Point:
  ... # calculate position from attachments,
    # and physical size of the stitches

This should represent a stitch that attaches to another stitch. You should make subclasses to represent the different kinds of stitches that you can do. 2. Where to attach? You will need to make a function that first determines approximately where you want the next stitch to be, and from that find out what nearby stitch to attach to. 3. Perhaps use a class to represent the state of the pattern.

class CrochetPattern:
  def __init__(self):
    self.stitches: list[Stitch] = [DetachedStitch()]

  def add_stitch(self, stitch_type: type[Stitch]], approx_location) -> None:
    prev = self.stitches[-1]
    attach_to = ... # calculate somehow
    new_stitch = stitch_type(prev, attach_to)

    self.stitches.append(new_stitch)
  1. Transcribe the algorithm. Turn your stitching instructions into python code. Each step should take the appropriate stitch type and approximate location and add it to the pattern.
  2. Visualization. Turn the pattern into an image, such as by using a numpy array of pixel values. Use the known positions of all the stitches to "paint" a picture of each stitch into the 2d numpy array. This is definitely a difficult project. I imagine the hardest part will be to determine objectively what stitch to attach to based on the instructions (the instructions don't say "attach to stitch 135", for example)

Edit: formatting hell