Drawing in SWI (or any) Prolog
Asked Answered
D

1

6

Is it possible to draw shapes in SWI prolog? There are print statements but I haven't found anything that would let me draw rectangles like a javascript canvas.

Is this something Prolog is conceptually capable of?

Am I asking the wrong question?

Drexler answered 25/5, 2020 at 19:27 Comment(3)
You can, see for example github.com/alexandre77/img-prolog/blob/master/img.plCellaret
What do you want to draw? You can draw svgs and graphviz reasonbly easily.Sabah
look into the link Annie posted at swi-prolog.discourse.group/t/functional-geometry-in-prolog/2372. It's very nice. And of course, you can open XPCE graphics examples from SWI-Prolog console (run ?- edit. and then explore XPCE manual menus)Traynor
C
2

Prolog is a general purpose Turing-complete programming language with a mature library, so surely yes. There is more than one way to do it. To give you some ideas in addition to simply generating image files.

You can set up a WebSocket connection to a web page that hosts a Canvas or SVG element and send drawing commands or blocks of SVG-containing XML to it. This way you won’t have to learn any esoteric or Prolog-specific drawing libraries and can instead rely on the full richness and wide use of Canvas, SVG and any related tools, while fully featured Prolog can be used for the logic/structure of your drawings.

If you need Prolog to be available on both sides, you can set up TauProlog in the browser, or if not, you can generate the SVG markup or canvas drawing commands in the backend Prolog. The web page can contain custom code (vanilla JS or TauProlog) to handle the incoming commands and translate them for applying to the canvas or SVG element. If needed, the page can also feed back to the backend so the pure Prolog drawing logic can inspect what’s going on on the screen. Or you can even incorporate interactive elements such as buttons that feed back to the drawing code.

With Prolog running in the backend, you can leverage any .svg or other image generating add-ons for Prolog, such as graphviz, and then send the output to the web page to be mixed with SVG or rendered into the canvas (Paper.js can import .svg files, see below). SVG also allows the embedding of full HTML elements or even blocks of HTML content, which could be used to add supporting elements, or for interactivity.

I don’t know any out of the box working examples for this specifically (although I intend to work on something similar) but you should be able to find working examples of the Prolog<-WS->Browser (and even ->TauProlog) setup. With TauProlog running in the browser, certain types of heavier traffic between Prolog and the rendering can be unloaded from the WS connection, in case the drawing logic needs to do some heavy inspection of what’s rendered during a loop.

For working with the canvas with out of the box raster and vector utilities, you can use Paper.js instead of vanilla Javascript canvas API.

You can also run full SWI Prolog (compiled to WebAssembly) in the browser, either for standalone execution, or to complement one running in the backend. See https://github.com/SWI-Prolog/swipl-wasm (I haven’t tested this myself).

Let me add that browser based UIs (applications) can easily be packaged as desktop apps that work locally, somehow you’ll just have to hide (embed) a SWI-Prolog (or any other implementation) server inside it, to make it work fully offline, or run it asa WebAssembly module. Or in the case of mobile apps, the Prolog backend can be running in the cloud.

Caudillo answered 29/1, 2022 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.