I have been trying to follow this article: https://andersource.dev/2020/11/06/organic-grid.html
Specifically, the appendix. But my understanding of calculus and its notation is limited.
So I tried also to understand and adapt/rewrite the source code: https://raw.githubusercontent.com/andersource/andersource.github.io/master/assets/organic-grid/index.js
But it uses some libraries I'm unfamiliar with and I want to work from first principles.
Here is a simple example containing my code (all relevant code is in the rotateSquareToFitQuad
function)
https://editor.p5js.org/marcusround/sketches/5jckZCCw-
[edit: this example has since been updated with the fix provided by andersource below] But the resulting square seems to have an almost random rotation so I must have made some error in adapting the code.
My goal is to minimise the total length of the four lines connecting each pair of vertices.