Help with creating a script

I would like to add a command to create a rectangle by area with fixed proportions (in the golden ratio). I did that little script in grasshopper, but I’d find it much more easier to have that as a command written in python. Could someone give me a hand with that?
So the main thing is to create a rectangle based on the area. A rectangle is “a * b”, because it should be in the golden ratio “b = a * 1.618”, so “A=a² * 1.618” therefore “sqrt(A/1.618) = a”.

In my example the area of the rectangle is the input, 25 sqm, based on the golden ratio the length and the width of the rectangle is 3,93/6,36.

Here is a quickie hack - will ask for area input and lower left corner point, then draws rectangle along world XY axes… More sophisticatation could be added of course… --Mitch

import rhinoscriptsyntax as rs
import math

def CreateGoldRect():
    tol=rs.UnitAbsoluteTolerance()*10
    
    area=rs.GetReal("Square area?",minimum=tol)
    if area is None: return
    
    pt0=rs.GetPoint("Lower left corner of rectangle")
    if not pt0: return
    
    gs=1.618
    yval=math.sqrt(area/gs)
    xval=yval*gs
    
    pt1=rs.coerce3dpoint([pt0.X+xval,pt0.Y,pt0.Z])
    pt2=rs.coerce3dpoint([pt0.X+xval,pt0.Y+yval,pt0.Z])
    pt3=rs.coerce3dpoint([pt0.X,pt0.Y+yval,pt0.Z])
    
    rs.AddPolyline([pt0,pt1,pt2,pt3,pt0])
    
CreateGoldRect()
1 Like

thank you Helvetosaur! Everything I wanted! One thing, how can I implement another option to 1.618? I want it to be the standard, but changable.

Edit: Another nice thing would be rounded numbers. :slight_smile:

alright, it’s pretty straight forward. my outcome:

import rhinoscriptsyntax as rs
import math

def CreateGoldRect():
tol = rs.UnitAbsoluteTolerance() * 10
area = rs.GetReal(“Square area?”, minimum=tol)
if area is None: return

pt0 = rs.GetPoint("Lower left corner of rectangle")
if not pt0: return
gs = rs.GetReal("Ratio", 1.618)
yval = math.sqrt(area / gs)
xval = yval * gs
yvalr = round(yval, 1)
xvalr = round(xval, 1)

pt1 = rs.coerce3dpoint([pt0.X + xvalr, pt0.Y, pt0.Z])
pt2 = rs.coerce3dpoint([pt0.X + xvalr, pt0.Y + yvalr, pt0.Z])
pt3 = rs.coerce3dpoint([pt0.X, pt0.Y + yvalr, pt0.Z])

rs.AddPolyline([pt0, pt1, pt2, pt3, pt0])

CreateGoldRect()

You got it! --Mitch