How to generate a ripple shape pattern using python

Question:

I am looking to create a repetitive pattern from a single shape (in the example below, the starting shape would be the smallest centre star) using Python. The pattern would look something like this:

pattern to be generated

To give context, I am working on a project that uses a camera to detect a shape on a rectangle of sand. The idea is that the ripple pattern is drawn out around the object using a pen plotter-type mechanism in the sand to create a zen garden-type feature.

Currently, I am running the Canny edge detection algorithm to create a png (in this example it would be the smallest star). I am able to convert this into an SVG using potrace, but am not sure how to create the ripple pattern (and at what stage, i.e. before converting to an SVG, or after).

Any help would be appreciated!

Asked By: thefijimonster

||

Answers:

Assing you are using turtle (very beginner friendly) you can use this:

import turtle, math

turtle.title("Stars!")

t = turtle.Turtle()
t.speed(900) # make it go fast
t.hideturtle() # hide turtle
t.width(1.5) # make lines nice & thick

def drawstar(size):
    t.up() # make turtle not draw while repositioning
    t.goto(0, size * math.sin(144)) # center star at 0, 0
    t.setheading(216); # make star flat
    t.down() # make turtle draw
    for i in range(5): # draw 5 spikes
        t.forward(size)
        t.right(144)
        t.forward(size)
        t.right(288)
    
drawstar(250)
drawstar(200)
drawstar(150)
drawstar(100)

input() # stop turtle from exiting

which creates this:
enter image description here

Answered By: SollyBunny

Here’s how I did it:

  • In the end, I ran a vertex detection algorithm to calculate the shape’s vertices.
  • Then, I sorted them in a clockwise order around the centroid coordinate. Using the svgwrite library, I recreated the shapes using lines.
  • I ‘drew’ a circle with a set radius around each vertex and calculated the intersection between the circle and a straight line from the centroid through the vertex.
  • This gave me two potential solutions (a +ve and a -ve). I chose the point furthest away from the centroid, iterated this method for each vertex and joined the points to create an outline of the shape.

enter image description here

Answered By: thefijimonster
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.