A browser-backed desktop-style GUI application written in Haskell with Threepenny-GUI for visualizing, classifying, and animating the planar four-bar linkage mechanism used in mechanism design and mechanical engineering courses.
This version replaces the previous Gloss GUI with a Threepenny-GUI + HTML/CSS/SVG
interface. The pure Haskell mechanism solver remains in src/FourBar.hs, while
app/Main.hs now builds a responsive browser-rendered GUI. The application runs a local GUI
server and displays the interface in a web browser at http://127.0.0.1:8023.
Reference model: University of Illinois Dynamics Reference — Four-Bar Linkages.
- Threepenny GUI: uses the browser as the display layer while the application logic stays in Haskell.
- Local font support: loads
resources/fonts/Inter-Regular.ttfthrough CSS@font-face. - Responsive 16:9 layout: the full app is drawn inside a proportional 16:9 shell.
- Minimum width: the app shell uses a
650pxminimum width and shows a warning below that size. - Left pane: occupies 20% of the application width and contains four horizontal sliders.
- Link length sliders: tune
g,a,b, andffrom5 cmto40 cm. - SVG visualization: the four-bar mechanism is redrawn as scalable browser SVG.
- Animation: a Play/Pause button starts and stops input-link motion.
- Angle display: live input angle
αand output angleβ. - Mechanism classification: Grashof/non-Grashof, valid/invalid assembly, and crank/rocker state.
- Keyboard controls:
Spacetoggles Play/Pause,Fflips the assembly branch, andRresets the mechanism.
A planar mechanism is a system of rigid bodies connected by joints so that the bodies move in a plane. For a planar linkage
with n links, j1 lower pairs such as pin or slider joints, and j2 higher pairs, the Kutzbach-Grübler mobility
equation is commonly written as:
For a four-bar linkage, there are four links and four pin joints:
Therefore, an ideal planar four-bar linkage has exactly one degree of freedom. Once the input angle α is chosen,
the other moving link positions are determined by the loop-closure constraints, except for the assembly branch.
| Symbol | Name | Description |
|---|---|---|
g | Ground link | Fixed distance between pivots A and B. |
a | Input link | Driven link from A to moving pivot C; its angle is α. |
b | Output link | Link from fixed pivot B to moving pivot D; its angle is β. |
f | Floating link / coupler | Connects moving pivots C and D. |
The fixed pivots are placed at:
The input point C is determined by:
The output point D must satisfy:
The simulator solves this by circle intersection. A real assembly exists for the current input angle when:
where d = ||B - C||. The two possible circle intersections correspond to the two assembly branches; pressing
F flips between them.
Let the four link lengths be sorted as:
where s is the shortest link and l is the longest link.
G < 0: Grashof mechanism.G = 0: change-point mechanism.G > 0: non-Grashof mechanism.
A physically assemblable four-bar must satisfy V <= 0. If V > 0, the longest link is longer than the sum of
the other three links, so the mechanism cannot close.
The simulator computes:
The signs of T1, T2, and T3 determine whether the input and output are cranks, rockers,
0-rockers, or π-rockers. The GUI displays both the detailed state and the collapsed basic state:
crank - crankcrank - rockerrocker - rockerrocker - crank
fourbar-linkage-threepenny/
├── app/
│ └── Main.hs
├── src/
│ └── FourBar.hs
├── resources/
│ └── fonts/
│ └── Inter-Regular.ttf
├── fourbar-linkage-threepenny.cabal
├── README.md
├── LICENSE
└── .gitignore
This module contains the pure mechanism mathematics and classification logic. It is intentionally separated from the GUI so that future versions can reuse the solver in tests, command-line tools, notebooks, or web backends.
Linkage: storesg,a,b, andf.Pose: stores the coordinates of pivotsA,B,C, andD, plusαandβ.excesses: computesT1,T2, andT3.grashofIndex: computesG = s + l - p - q.validityIndex: computesV = l - s - p - q.classifyMotion: returns detailed labels such ascrank,rocker,0-rocker, andπ-rocker.solvePose: solves the four-bar geometry using circle intersections.nearestValidAlpha: finds a nearby valid angle after parameter changes.
This module now contains the Threepenny GUI and event loop. It builds DOM controls for the sliders and buttons, renders the mechanism as responsive SVG, loads the local Inter font through CSS, and updates the application state from Haskell.
setup: builds the page, sliders, readouts, buttons, and SVG container.renderWorld: synchronizes the browser GUI with the HaskellWorldstate.mechanismSvg: generates the current linkage drawing as SVG.animationLoop: advances the mechanism while Play is active.updateSliderValue: applies slider changes to the pure linkage model.
The recommended installation route is GHCup, which installs ghc, cabal, and optional development tools.
Official Haskell download page: https://www.haskell.org/downloads/
Official GHCup installation page: https://www.haskell.org/ghcup/install/
Set-ExecutionPolicy Bypass -Scope Process -Force;[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; try { & ([ScriptBlock]::Create((Invoke-WebRequest https://www.haskell.org/ghcup/sh/bootstrap-haskell.ps1 -UseBasicParsing))) -Interactive -DisableCurl } catch { Write-Error $_ }
After installation, close and reopen PowerShell, then verify:
ghc --version
cabal --versionRun the simulator:
git clone https://github.com/mohammadijoo/Four-Bar-Mechanism-Haskell.git
cd Four-Bar-Mechanism-Haskell
cabal update
cabal run fourbar-linkage-threepenny
Then open http://127.0.0.1:8023 if the browser does not open automatically.
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
Restart the terminal, then verify:
ghc --version
cabal --versionRun the simulator:
git clone https://github.com/mohammadijoo/Four-Bar-Mechanism-Haskell.git
cd Four-Bar-Mechanism-Haskell
cabal update
cabal run fourbar-linkage-threepenny| Control | Action |
|---|---|
Drag g slider | Change ground link length. |
Drag a slider | Change input link length. |
Drag b slider | Change output link length. |
Drag f slider | Change floating/coupler link length. |
Click Play / Pause | Start or stop the animation. |
Click Flip branch | Switch between the two possible assembly branches. |
Click Reset | Restore the default linkage. |
Space | Toggle Play/Pause. |
F | Flip between assembly branches. |
R | Reset the mechanism. |
Threepenny starts a local GUI server. If the browser is not launched automatically, open:
http://127.0.0.1:8023
Close the older running instance, or change jsPort = Just 8023 in app/Main.hs to another port such as
8024.
Make sure the file exists exactly here:
resources/fonts/Inter-Regular.ttf
Also make sure you run cabal run from the project root, because the static file server is configured with
jsStatic = Just ".".
This project no longer depends on gloss, OpenGL, or GLUT. The old unknown GLUT entry glutInit Windows problem does
not apply to this Threepenny version.
The application shell uses CSS aspect-ratio: 16 / 9. The shell grows to the largest 16:9 rectangle that fits inside the
browser viewport. It uses a 650px minimum width. On a viewport narrower than that, the app may horizontally clip and a
warning is displayed.
- Add a coupler point
Pand trace its curve. - Add export of linkage motion data as CSV.
- Add velocity and acceleration analysis using differentiated loop-closure equations.
- Add force transmission angle and mechanical advantage plots.
- Add automated tests for the classification table.
- Add an Electron wrapper for a packaged desktop executable.
Below is a link to the simulation video on YouTube.
This project is released under the MIT License. See LICENSE for details.