Coding Curves 01

coding curves

Background

For a number of years, I’ve been wanting to writing a book called “Coding Curves”. There are all kinds of really fascinating curves that are fun to code and understand and create very interesting images. I’ve started this book at least three times. I’m feeling the urge to to it again, but I’m going to be more realistic about it this time. I don’t think I’m going to sit down and write a book cover to cover and get it formatted and edited and self publish it. I’ve accepted that that’s just not going to happen. But I want to write about this stuff.

The Plan

So I’m going to do it as a series of blog posts. I don’t know how often these posts will come out. Maybe once a week if I’m diligent. Maybe more often if I get on a roll. Maybe less if I get bored, distracted, or busy with other things. But even if there’s a gap, I can pick back up where I left off and continue.

Maybe, when it’s all done (will it ever be?) there will be enough there to actually compile into a book. Who knows.

Update (2/19/23)

It’s “done”. Which is not to say that I’ll never add to it, but I’ve covered what I set out to cover initially, and I’m pretty happy with it. I may eventually compile it all into an ebook that people can download as a single file and read like that. Probably under a pay-what-you-want type system. But for now, other projects await.

Table of Contents

This is the plan. It may differ, but we gotta start somewhere. I’ll link the posts as they come out here, so this post can continue to serve as an index.

  1. Intro (you’re reading it)
  2. Trig Curves
  3. Arcs, Circles, Ellipses
  4. Lissajous Curves
  5. Harmonographs
  6. Pintographs
  7. Parabolas
  8. Bezier curves
  9. Roulette curves
  10. Spirals
  11. Roses
  12. Guilloche Patterns
  13. Superellipses and Superformulas
  14. Miscellaneous Curve

In general we’ll be covering two-dimensional curves. Some of these subjects will take multiple posts to cover. I’ll update the TOC as it evolves. There may be more subjects too, and I’m open to suggestions.

The Audience and Purpose

The audience for this course is people who are interested in exploring different types of curves and want practical techniques for drawing them. It may be artists, designers, game developers, UI developers, or recreational mathematicians. This is NOT a mathematics course, though there will be plenty of math in it. A lot of the explanations I give will be very incomplete. There will hopefully be enough information in the materials to give you the understanding you need to draw the given curves and expand on them. But at best, each chapter will be a mere introduction to a topic. Hopefully nothing will be straight out incorrect, but I’m happy to hear about it if there is, and will make my best efforts to correct it.

Personally, I love finding some new mathematical formula that I can apply to create some new, interesting shape, then tweaking the parameters to see how it changes that shape, and eventually animating those parameters. And finally combining the formula for one curve with the formula for another one and possibly making some shape that nobody has every seen before. If any of that rings true for you too, you will probably enjoy reading through this.

The Format

Most of what I’ve written before has been tailored to a specific language. For example, I wrote Playing With Chaos using JavaScript and HTML 5 Canvas. The idea was that it should be generic enough to convert to any language or platform, but I felt I needed to base it in some concrete language.

After recently reading a couple of really fantastic books on ray tracing (covered here), which were completely language agnostic and only included pseudocode samples, I’ve changed my thinking about this. So all “code samples” in this series will actually be “pseudocode samples”.

So write in whatever language or platform you want. The main requirements will be:

A. The ability to set up and size a canvas or drawing surface of some kind.

B. The ability to draw lines of different colors and widths on said canvas.

C. Typical language features and control structures like functions, variables, for loops, conditionals, etc. We’ll probably need some kind of array or list as well as some structured object type, so we can have a point object that has x and y properties. Most of this is all table stakes for any modern language, though it may look different in each one.

Additionally, it will be nice if you can draw circles (or at least arcs, which can become circles) and rectangles, and be able to fill an area with a given color. Extra credit if your drawing api lets you write text to the canvas.

A simple pseudocode example might look like this:

canvas(800, 800)

setLineWidth(5)
setColor(1, 0, 0) // red
moveTo(100, 100)
lineTo(700, 700)
stroke()

This should output an image like this:

I’ll avoid going as deep as OOP or FP as those can look quite different on different platforms, but we’ll need some functions, so I’ll keep those pretty basic:

canvas(500, 100)
foo(100)

function foo(count) {
  for (i = 0; i < count; i++) {
    moveTo(i * 10, 10)
    lineTo(i * 10, 90)
  }
  stroke()
}

Resulting in:

A few things to note here.

First, I’m calling the function before it’s defined. That might work in your language, or it might not. If not, you’ll have to rearrange things.

Secondly, the drawing api functions are written like global functions. It’s likely that in your drawing api, these will need to be called as methods of some kind of canvas object, which may have to be passed into the function, or might be able to be defined globally. I’m going to ignore that and assume that you can figure stuff like that out. The focus here is on covering concepts, not best coding practices.

Also, I’m not bothering with types for vars, args or returns, unless in some instance it becomes non-obvious and important.

As I get deeper into the series, my pseudocode style might change somewhat. If so, I’ll come back here and update things.

Wait for it!

Look for the first installment soon!

27 thoughts on “Coding Curves 01

  1. Cool. Pseudocode, makes sense, equitable. I just had a funny flashback to drawing in PostScript decades ago. 😉

    Realize most of your work here will be 2D, but I’m dreaming/yearning to see this procedural geometry/animation in 3D, especially immersively with headset. Blender first interested me with its Python control over all menu operations, but lately the action seems to be in its Geometry Nodes, a composition method which can be driven by external data. There are older YouTube videos of constructing Sierpinski Gaskets and such, but recent profile-along-curve nodes seem to offer greater procedural freedom.

    Not that I’m nudging you or such… appreciate your work in making this math more accessible!

    1. Depends on the drawing api I guess, but most that I’ve worked with will allow multiple moveTo’s and lineTo’s with a single final stroke. In fact, doing a single stroke is usually a lot faster than doing one for every segment.

  2. I think there’s a small error in the second bit of pseudocode:

    function foo(count) {
      for (i = 0; i < count; i++) {
        moveTo(count * 10, 10)
        lineTo(count * 10, 90)
      }
    }

    moveTo and lineTo should probably reference `i` not `count`

  3. Thanks for the comments about the typo. Fixed now. That’s the one problem with writing in pseudocode. Hard to error check. 🙂 Of course, I write a concrete working example, but sometimes there are errors in copying it over.

  4. Thank you for this. I look forward to the finished book. Since this will be a book some day I thought I should point out that I noticed a typo. Near the end you say, “Also, I’m not bother with types . . . ” Should this be, “Also, I’ll not bother with types . . . ” or, “Also, I’m not bothering with types . . . “

    1. Thanks. Not sure it’ll be a real book, but when all is said and done I might compile it into a pdf/epub and let people download it. I guess that’s a book.

  5. Hi peter.

    First i should say thank you!

    Since 2008 when i was read your book 《Foundation Actionscript 3.0 Animation:Making Things Move!》

    I am your big fan from china.

    Of course, I was read another your book named 《AdvancED ActionScript 3.0 Animation》too.

    Now, I am a Frontend developer. I still benefit from the book on my work.

    I was shocked from the book of your written. The book’s content is so easy to understand.

    I was hated the Math when i in middle school. Because i am not good at it.

    And my math still baddly until to the college…

    After i read the Coding curves series. Yes, another great one, And i got an idea: why not introduce your great work to chinese audience.

    Could i translate this series to chinese version and post it to my blog or Frontend developer community.

    I will put the author and your blog reference on the top.

    Thanks again.

    Mentions

  • 💬 Let there be sound
  • 💬 Coding Color 01
  • 💬 Coding Curves 14: Miscellaneous Curves
  • 💬 Coding Curves 12: Guilloche Patterns
  • 💬 Coding Curves 05: Harmonographs

Leave a Reply