Recursive Subdivision 4 sided polygon

Recursive subdivision of a 4-sided polygon 

This generates a subdivision based on a 4-sided polygon. Be careful as it does not provide sufficient error-checking, it simply assumes that you input the correct geometry.

#recursive subdivision

#import library

import rhinoscriptsyntax as rss

import random

 

#definition that subdivides the object

def subdivide(rect,maxLen,ctrlNr):

    #divide into 4 pieces

    #get all 4 segments

    segments = rss.ExplodeCurves(rect)

 

    #print("Los segmentos son:")

    #print(segments)

    #store the segments in different variables (not necessary)

    #segment 1-3 / segment 2-4

    segment1 = segments[0]

    segment2 = segments[1]

    segment3 = segments[2]

    segment4 = segments[3]

 

    #calculate middle points on the segments

    midPt1 = rss.CurveMidPoint(segment1)

    midPt2 = rss.CurveMidPoint(segment2)

    midPt3 = rss.CurveMidPoint(segment3)

    midPt4 = rss.CurveMidPoint(segment4)

 

    #verify it is correct

    #rss.AddPoints((midPt1,midPt2,midPt3,midPt4))

 

    #calculate centerPoint

    #join non-consecutive segments

    midLn1 = rss.AddLine(midPt1,midPt3)

    midLn2 = rss.AddLine(midPt2,midPt4)

 

    ctPt = rss.CurveCurveIntersection(midLn1,midLn2)

    #from the whole list of results we take [0][1]

    #[0]: because we know that we have a SINGLE object: intersection is 1 point

    #[0][1]: because for that one intersection event, we need to get the point coordinates

    ctPt = ctPt[0][1]

    #rss.AddPoint(ctPt)

 

    #extract the vertices of the rectangle in order to use them for the subsivision

    vertexPts = rss.CurvePoints(rect)

    rect1 = rss.AddPolyline((ctPt,midPt4,vertexPts[0],midPt1,ctPt))

    rect2 = rss.AddPolyline((ctPt,midPt1,vertexPts[1],midPt2,ctPt))

    rect3 = rss.AddPolyline((ctPt,midPt2,vertexPts[2],midPt3,ctPt))

    rect4 = rss.AddPolyline((ctPt,midPt3,vertexPts[3],midPt4,ctPt))

    

    #calculate the length and delete unnecesary objects

    len = rss.CurveLength(segment1)

    

    #create extrusions

    crv1 = rss.AddLine((0,0,0),(0,0,len))

    ext1 = rss.ExtrudeCurve(rect1,crv1)

    rss.CapPlanarHoles(ext1)

    rss.DeleteObject(crv1)

 

    len2 = rss.CurveLength(segment2)

    crv2 = rss.AddLine((0,0,0),(0,0,len))

    ext2 = rss.ExtrudeCurve(rect2,crv2)

    rss.CapPlanarHoles(ext2)

    rss.DeleteObject(crv2)

    

    len3 = rss.CurveLength(segment3)

    crv3 = rss.AddLine((0,0,0),(0,0,len))

    ext3 = rss.ExtrudeCurve(rect3,crv3)

    rss.CapPlanarHoles(ext3)

    rss.DeleteObject(crv3)

    

    len4 = rss.CurveLength(segment4)

    crv4 = rss.AddLine((0,0,0),(0,0,len))

    ext4 = rss.ExtrudeCurve(rect4,crv4)

    rss.CapPlanarHoles(ext4)

    rss.DeleteObject(crv4)

    

    rss.DeleteObjects((midLn1,midLn2))

    rss.DeleteObjects(segments)

    

    #add a control number

    randNr = random.random()

    #implement a control(how do i stop?)

    if((len>maxLen)and(randNr<ctrlNr)):

        rss.DeleteObjects((ext1,ext2,ext3,ext4))

        subdivide(rect1,maxLen,ctrlNr)

        subdivide(rect2,maxLen,ctrlNr)

        subdivide(rect3,maxLen,ctrlNr)

        subdivide(rect4,maxLen,ctrlNr)

 

#get object from user

#object needs to be 4-sided (in this case)

rectangles = rss.GetObjects("Please select the curves to be subdivided, note they need to have 4 sides and 4 sides only",4)

maxDesiredLen = rss.GetReal("Please enter minimum desired length",0.3,0.001)

controlNr = rss.GetReal("Please enter control number for random subdivision",0.8,0.1,1)

#use the definition we created above

for i in range(len(rectangles)):

    rss.EnableRedraw(False)

    rectangle = rectangles[i]

    subdivide (rectangle,maxDesiredLen,controlNr)

 

    rss.EnableRedraw(True)

Search Site




archi·o·logics survey

I am interested in: