Updating pen plotter code, finally

I've just, nearly-sorta-kinda, finished updating my online pen plotter tools, which has taken far too long in almost a writer's block kind of way. Having to update old code has also stopped me from writing new tools, which has been frustrating.

I only have myself to blame, though.

When I first started writing tools, some things were similar to what I wanted to do but not close enough. So, of course, writing it all myself was the way to go. I was sensible enough to know that I wanted a "framework" of sorts, code that would handle acting as a "sheet of paper", which I could add lines to in various ways, which would both display those lines on a webpage and also write them out as SVG files.

But then it did what code often does.

I wanted to make a new design, so I copied the old code into a new file and added bits, which happened again and again. This is an acceptable way to develop when you don't have a plan and don't know everything you will be doing in the future. Although not having a plan being "acceptable" is up to debate.

However, you then end up with a bunch of "technical debt", which is where I've been for the past, oooh, four months or so.

I had copied and pasted enough code, enough times, to know what I used everywhere and what only gets used now and then. I had a good idea of how I needed to cut all the code out and bundle it up into a standard external file.

The whole copy code from one place to another and accruing bloat looked something like this...

Simple project #1 becomes #2 which turns into both #3 and #4 that end up doing slightly different things. #4 then becomes #5 and #6 that bloat in different ways, while yet another copy and paste makes #7, which has some stuff that needs to be retro-fitted back into #1-#6.

Now, instead, I have my "framework" that stays pretty much as it is, "core utils" that I can add to, and then each project has it's own specific code focused on the design.

It's fair to say that going back and cutting out all the "common" bits of code from all the old tools and then smooshing them together into a uniformed chunk hasn't been fun or easy.

I also know it's still imperfect; my code is still a bit of a mash-up of an "object with methods on it, that you can call" and functions that directly manipulate lines. It is, dear reader, not a framework I'd like anyone else to have to work with.

I'm okay with that, though, as it does what I need and done is better than perfect; it allows me to make pen plots, and that's what counts.

This also means I can start moving forwards again once I've nailed a couple more things.

So, onto the changes.

💻 💻 💻

The basic gist of what I wanted to do was to have some tools to crop circles and rectangles into the designs and crop designs to the circles and rectangles. Easier shown than explained.

Which is helpful for various practical purposes. Putting one plot inside another and mainly creating space to write a postal address so I can get back to making envelopes and stationery.

The second set of tools are displacement maps, which allow me to apply the Perlin noise function to the whole plot. I've shown it below with a design that has a grid, but ideally, it'll be for other things.

I can actually apply two lots of displacement noise. They allow me to have one as a general smooth displacement and a smaller one to roughen it up a bit.

Finally, a couple of things related to rotations still aren't working quite right, but at least they're not working in just one place, rather than in several different places.

And "Dotifying" throws away all the lines in a plot and keeps the endpoints.

All of this takes a while because you have to make sure all the transformations and crops happen in the correct order.

Some of the plotting tools are now slightly broken, in that the new code doesn't play nicely with putting things into different "layers", but I decided it was more important just to shove the code out there than it was to fix all the things.

⏳ ⏳ ⏳

I still have three more things to add, and then I can get on with my life and making new tools.

  1. Pre and Post scaling. I want to be able to grow and shrink my whole design before the tool crops to the margins.
  2. Line breaking. I want to be able to specify a distance, like 5mm and have all current lines broken down into 5mm sections. This is relatively easy to do; I just need to take the time to sit down and do it.
  3. "Decimating". Where I can then tell the code to go along lines, randomly turning sections on and off. With the ability to slide the probability of that happening from one end of the scale to another.
〰 〰 〰

Allowing me to draw a whole bunch of straight lines, break them up into smaller ones, and then have 10% of those smaller sections removed before finally applying a displacement map to the whole thing.

So that's my next step, the above three things before I get to write any more tools.

Being able to do that next step was dependant on bundling everything into a single giant block of "utilities", allowing me to add them in only one place.

I know all the above isn't that interesting, but dealing with technical debt is generally always dull. I've written the above more of a celebration to myself of getting it all done because fixing old stuff while not writing new stuff is terribly frustrating.

Well done me!

🤖 🤖 🤖