News for hackers. Stuff that matters.

Ask HN: What do you use for programmatic drawing?

When you need to write some program that calculates and shows something quick you probably write some console program in whatever language.

But what do you personally do, when you need a quick program that needs to draw some arbitrary graphics?

What languages and libraries do you use to draw something quick to see it, save it or animate it without a hassle?

( 18 comments | 21 points | on HN )
Re:Ask HN: What do you use for programmatic drawing? by susam on Thursday November 11, 2021 @01:08PM (#29191008)
Gnuplot for graphs.

LaTeX PGF/TikZ for complicated diagrams.

Lines and boxes drawn with ASCII symbols (e.g., +, -, |, <, >, ^, v, /, \, ', `, etc.) using Emacs for very simple diagrams.

PGF/TikZ has considerable learning curve, so it may seem like a hassle in the initial days of learning to use it. But after mastering it once, one can unlock a lot of power and flexibility that comes with it. The PGF/TikZ manual at http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf is really worth seeing. This manual, which is itself designed using PGF/TikZ, is a masterpiece!

Re:Ask HN: What do you use for programmatic drawing? by kaycebasques on Thursday November 11, 2021 @12:13PM (#29190379)
https://sequencediagram.org served my needs when I needed to create some timing diagrams for the Corrily docs (e.g. [1]). The cool thing about it is that I could save the source code of the diagram alongside the document itself (as a comment in the markdown).

[1] https://docs.corrily.com/docs/price-optimization-overview

Re:Ask HN: What do you use for programmatic drawing? by ai_ia on Thursday November 11, 2021 @01:18PM (#29191130)
I usually use Manim for creating mathematical animations for some Mathematical courses I am writing. It’s a python library developed by the guy from 3b1b and it’s a joy to work with it.
Re:Ask HN: What do you use for programmatic drawing? by truly on Friday November 12, 2021 @01:34AM (#29196825)
I am also using Manim and I second the recommendation.

It works well enough for animations.

There are two versions: 3b1b and the community edition. You will most likely prefer the community edition.

Here is the quickstart tutorial:

https://docs.manim.community/en/stable/tutorials/quickstart....

Re:Ask HN: What do you use for programmatic drawing? by cpach on Thursday November 11, 2021 @12:41PM (#29190718)
Have you tried Pic? It might suit your needs.

http://floppsie.comp.glam.ac.uk/Glamorgan/gaius/web/pic.html

Re:Ask HN: What do you use for programmatic drawing? by scotty79 on Thursday November 11, 2021 @01:16PM (#29191101)
Personally I'd prefer to have something I can use with programming language that I use. For quick coding I mostly use Python.

I could probably make Python script that generates Pic file and run it through Pic to generate the image but it won't work for any animations and I'd need another piece of software to display the graphic.

Re:Ask HN: What do you use for programmatic drawing? by cpach on Thursday November 11, 2021 @02:49PM (#29192293)
Ah. I see.

BTW, may I suggest that you take a look at Nim. Pretty slick for graphics:

https://news.ycombinator.com/item?id=24179060

Re:Ask HN: What do you use for programmatic drawing? by dkersten on Thursday November 11, 2021 @08:01PM (#29195204)
I've used pixijs.com in javascript, especially the drawing bits: https://pixijs.io/examples/#/graphics/simple.js You can export the drawn surface as an image. Not sure how if it will would work from a commandline program though.
Re:Ask HN: What do you use for programmatic drawing? by truly on Friday November 12, 2021 @01:38AM (#29196859)
I am using Manim for animations.

However, I am also on the lookout for a good programmatic graphics editor. I am thinking of developing my own, but time is an issue.

I have successfully also used Python libraries like this one to create some graphics:

https://pypi.org/project/svgwrite/

Re:Ask HN: What do you use for programmatic drawing? by tmaly on Thursday November 11, 2021 @03:19PM (#29192634)
For Python you could use the pyglet OpenGL library to do both 2D and 3D https://github.com/pyglet/pyglet

For Scratch, the pen tool extension works really well. I was just doing a demo on this for an elementary class the other day.

Re:Ask HN: What do you use for programmatic drawing? by scotty79 on Thursday November 11, 2021 @03:55PM (#29193039)
Pyglet looks great, but drawing is done in OpenGL way.

Is there a libraray that uses pyglet giving it the drawing interface more limilar to what other 2d APIs offer? Like the one suppoted by HTML Canvas tag?

EDIT:

Never mind. I just noticed pyglet.shapes offers something more palatable than OpenGL. :-)

and doing stuff is pretty clean:

    import pyglet
    from pyglet import shapes

    batch = pyglet.graphics.Batch()

    circle = shapes.Circle(700, 150, 100, color=(50, 225, 30), batch=batch)
    square = shapes.Rectangle(200, 200, 200, 200, color=(55, 55, 255), batch=batch)

    window = pyglet.window.Window(960, 540)

    @window.event
    def on_draw():
        window.clear()
        batch.draw()

    @window.event
    def on_key_press(symbol, modifiers):
        if symbol == pyglet.window.key.ESCAPE:
            window.close()

    pyglet.app.run()
Re:Ask HN: What do you use for programmatic drawing? by scotty79 on Friday November 12, 2021 @03:51AM (#29197413)
After some experimentation I found out that pygame is even better them pyglet.

I had multiple issues with pyglet with high monitor refresh rate, HDR and refresh rate mysteriously slowing down after I dragged the window around. While pygame works flawlessly.

Re:Ask HN: What do you use for programmatic drawing? by slopdo on Friday November 12, 2021 @03:37AM (#29197365)
I'm not using it but just in case it is useful to anybody here I would add Processing to the list. Lots of examples can be found at its website:

https://processing.org/examples

Re:Ask HN: What do you use for programmatic drawing? by zzo38computer on Friday November 12, 2021 @03:07AM (#29197239)
I use Ghostscript (although it doesn't do animations as far as I know, but it works for programmable drawings without animations).
Re:Ask HN: What do you use for programmatic drawing? by jiehong on Thursday November 11, 2021 @05:15PM (#29193935)
P5.js[0] for animating stuff quickly.

[0]: https://p5js.org/

Re:Ask HN: What do you use for programmatic drawing? by dummy_physicist on Thursday November 11, 2021 @01:37PM (#29191343)
Graphviz for diagrams, R standard library for plots, and imageMagick for simple image creation or processing.
Re:Ask HN: What do you use for programmatic drawing? by scotty79 on Thursday November 11, 2021 @02:38PM (#29192161)
For charts Python with pyplot is very quick and easy.

But for arbitrary graphics I have no idea. I usually end up just launching Visual Studio, create new WinForms application and placing PictureBox component with empty bitmap or draw directly in OnPaint method of the Form.

Re:Ask HN: What do you use for programmatic drawing? by tomjen3 on Friday November 12, 2021 @12:52AM (#29196652)
Javascript and canvas. You can just right click and save the image.