Polygon Fractals with Rhino Python

by Tuğrul Yazar | August 2, 2017 15:02

We will see a simple Rhino Python[1] exercise here. I called these Polygon Fractals[2] (or Pentaflakes sometimes). It is both educational and fun to play with them. In Rhino, it can be a good exercise for basic CAD commands and transformations such as move, copy, and scale, and precision drawing operations such as object snapping. Also, in Grasshopper, it can be a good challenge for looping. In Rhino Python, it is already a good exercise for understanding Python and the rhinoscriptsyntax module.

This form generation method, Polygon Fractals is based on an exercise I tried earlier in the Design Geometry[3] course. As you can see, I repeat the selected polygon using a loop[4]. I form new vertices at each step and carried them to the next step of the iteration. This creates a pattern that you can repeat as many times as desired. In such fractals, the geometric displacement rule is applied. It may not be correct to call these shapes that I have produced fractals. But in terms of self-similarity and diversity derived from the repetition of a simple operation, I think they could be called fractals[5].

polygon fractals
polygon fractals
# Drawing Simple Polygon Fractals
# 02.08.2017 www.designcoding.net - Tugrul Yazar
import rhinoscriptsyntax as rs
centerPoint = rs.GetPoint("Specify the center of polygon")
numberEdges = rs.GetInteger("Enter the number of edges", 6, 3)
radius = rs.GetReal("Specify the radius of polygon", 10)
iterat = rs.GetInteger("Enter the number of iterations", 3)
scale1 = rs.GetReal("Specify the first scale",0.5)
scale2 = rs.GetReal("Specify the fractal scale",0.4)
rs.EnableRedraw (False)
tempCircle = rs.AddCircle(centerPoint, radius)
pointList = rs.DivideCurve(tempCircle, numberEdges)
pointList.append(pointList[0])
rs.DeleteObject(tempCircle)
polygon = rs.AddPolyline(pointList)
nextRow = []
scale = scale1
for i in range(0, iterat):
	for x in pointList:
		vector = rs.VectorCreate(x, centerPoint)
		object = rs.CopyObject(polygon, vector)
		temp = rs.ScaleObject(object, x, [scale,scale,scale])
		prep = rs.ExplodeCurves(temp)
		for y in prep:
			nextRow.append(rs.CurveStartPoint(y))
			rs.DeleteObject(y)
	pointList = nextRow
	nextRow = []
	scale = scale * scale2
rs.EnableRedraw (True)

The Python code starts with the user inputs. Then I define the first polygon by drawing a circle and dividing it. Unfortunately, Rhino Python still doesn’t have a polygon method. Then, I define some variables like scale. After that, the for loop runs. On each iteration, I use all the points in the pointList as the center points of copied-scaled versions of the polygon. Immediately after this operation, I explode the newly created polygons. Because, in another loop, I add the vertices of the exploded polygons to a temporary list. After modifying the scale ratio this whole operation is repeated.

Endnotes:
  1. Rhino Python: https://www.designcoding.net/category/tools-and-languages/rhino-python/
  2. Fractals: https://en.wikipedia.org/wiki/Fractal
  3. Design Geometry: https://www.designcoding.net/category/education/design-geometry/
  4. loop: https://www.designcoding.net/tag/loop/
  5. fractals: https://www.designcoding.net/category/research/fractals/

Source URL: https://www.designcoding.net/polygon-fractals-with-rhino-python/