This repository contains code to generate visualizations of folded paper, specifically "cross-folds," using both terminal output and SVG. It uses a "bar-bra" notation to represent the folds, where "|" represents an open edge and "()" a closed edge. For example, "||()" visualizes as:
code
││╭╮
││││
│╰╯│
╰──╯
The code is split between Python and Prolog. Python handles the visualization and user interface, while Prolog generates the bar-bra notation for specific fold types and numbers of folds. Let's break down the Prolog part (falten.pl):
The Prolog code defines a recursive predicate fold/4 to generate the nested (NV) and double (DV) sides of a cross-fold in bar-bra notation. The arguments are:
Fold Type:in or out, specifying the type of fold.
Fold Number (Num): An integer representing how many times the paper is folded.
Nested Side (NV): The bar-bra representation of one side of the folded paper.
Double Side (DV): The bar-bra representation of the other side of the folded paper.
Base Case:
prolog
fold(_,1,"||","|;|").
This defines the simplest case: a single fold (Num = 1). The nested side (NV) is "||" (two open edges), and the double side (DV) is "|;|" (one open edge on each of two separate pieces of paper, separated by a semicolon).
Recursive Cases (out-fold and in-fold):
prolog
fold(out,Num,NV,DV) :- Num >= 2, ...
fold(in,Num,NV,DV) :- Num >= 2, ...
These clauses define how to generate the bar-bra notation for folds greater than 1. They work recursively by:
Decrementing the fold number (DecrNum is Num - 1).
Recursively calling fold/4 with the decremented number to get the NV and DV of the previous fold (DecrNV, DecrDV).
Constructing the new NV and DV (NV, DV) based on the previous DV and NV using the new_nv/3 and new_dv/3 predicates.
Helper Predicates:
new_nv/3: Constructs the new nested side (NV) based on the previous double side (DV). It does this by splitting the DV at the semicolon, generating a certain number of nested "V"s (parentheses), and concatenating everything together. The logic is slightly different for in and out folds.
new_dv/3: Constructs the new double side (DV) by prepending or appending "||;" or ";||" to the previous nested side (NV), depending on whether it's an in or out fold.
vs/2 and vs_helper/3: These are utility predicates to generate a string of nested parentheses "()" repeated a specified number of times.
In essence, the Prolog code provides a concise, declarative way to generate the complex bar-bra strings needed for visualizing different folds. Python then takes these strings and creates the visual representation. This separation of logic makes the code cleaner and easier to maintain. The Prolog code leverages its pattern-matching and recursion capabilities to elegantly describe the fold generation process.