Home > Enterprise >  Function to divide a line into two segments using the Golden Ratio, Phi
Function to divide a line into two segments using the Golden Ratio, Phi

Time:10-29

I have a line length I'm dividing into two segments. I wish to use the Golden Ratio, or Phi, to determine the length of each segment.

Given a height value of 100, I need the return of 61.8 and 38.2.


In this case I am programming an SVG shape using Javascript. A form is used to provide dynamic inputs, for example height of the shape. There are corners at the bottom that use enter image description here

const phi = (1   Math.sqrt(5)) / 2;

console.log(phi);

const getSegments=(h)=>{return {'a': h/phi, 'b': h-h/phi};}

console.log(getSegments(100));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

So in context of your existing code:

const phi = (1   Math.sqrt(5)) / 2;
const getSegments=(h)=>{return {'a': h/phi, 'b': h-h/phi};}
let height = 100;
let segments = getSegments(height);

let pathDef = 
  `m 300 300 v ${segments.b} 
   c 0 ${segments.a} ${segments.b} 
   ${segments.a} ${segments.a} ${segments.a}`;

console.log(pathDef);

document.querySelector('path').setAttribute("d", pathDef);
<svg width="12cm" height="4cm" 
  viewBox="0 0 300 600"
  xmlns="http://www.w3.org/2000/svg"
  version="1.1"
>
  <path d="" 
    fill="none" stroke="blue" 
    stroke-width="20"
  />
</svg>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related