Teach you how to draw 06 --- how to draw all star polygons with Python Turtle


stay Last blog In, we show how to draw a regular star polygon without degradation, that is, it can be drawn without lifting the pen from the paper. In this, we will try to think of a way to draw two types of star polygons.

Two types I mean degenerate (must lift pen) and non degenerate / regular (no pen), but I don't know the real names of these star polygon types. But I hope I can understand the idea

Repeated regular polygon method

In this method, we will see how to draw degenerate star polygons by repeatedly drawing regular polygons such as triangles, squares and pentagons

Star of David

Let's start with the simplest degenerate star we know about star David. You can draw this type of star by simply drawing an equilateral triangle and then overlapping it with another inverted triangle

Note that you can draw triangles using the circle function! Look at the code below

import turtle as t

t.shape('turtle')
t.color('#4F86F7')
t.pensize(2)

LENGTH=200

t.circle(LENGTH)
t.circle(LENGTH,steps=3)
t.circle(LENGTH,steps=4)
t.circle(LENGTH,steps=5)

In the above code, we use the steps parameter to draw a triangle, a square and a Pentagon inscribed in the same circle. This is what we get:

Now that we know how to use the steps parameter, we can use it twice from two different positions on the circle to draw our stars

import turtle as t

t.shape('turtle')
t.color('#4F86F7')
t.pensize(2)

LENGTH=200

def star_of_david():
    """Draws two overlapping triangles
    """
    t.circle(LENGTH,steps=3)
    t.penup()
    t.circle(LENGTH, 180)
    t.pendown()
    t.circle(LENGTH,steps=3)

t.circle(LENGTH)
star_of_david()

In the above code, T. circle (length, 180) turn moves to the top of the circle so that it can draw another triangle from there. This is what we got

Degenerate Octagon

You can draw degenerate octagonal stars in a similar way

import turtle as t

t.shape('turtle')
t.color('#4F86F7')
t.pensize(2)

LENGTH=200

def degenerate_octagram():
    t.circle(LENGTH,steps=4)
    segments=8
    t.circle(LENGTH,360/segments)
    t.circle(LENGTH,steps=4)

t.circle(LENGTH)
degenerate_octagram()

After drawing the first square, we move the turtle to the position determined by 360 / segments. segments = 8 because octagonal stars have 8 equal segments.

Remember that an n-gram star has N equal segments

Degenerated nine type personality

According to Wikipedia, type 9 personality is a nine pointed star. Unlike hexagons and decagons, nine corners cannot be drawn with just two regular polygons. But how much do we need?

You should use three equilateral triangles to draw a degenerate nine sided shape (you can also try drawing it on a piece of paper).

import turtle as t

t.shape('turtle')
t.color('#4F86F7')
t.pensize(2)

LENGTH=200

def enneagram():
    segments=9

    t.circle(LENGTH, steps=3)
    t.circle(LENGTH, 360/segments)
    t.circle(LENGTH, steps=3)
    t.circle(LENGTH, 360/segments)
    t.circle(LENGTH, steps=3)

t.circle(LENGTH)
enneagram()

In the above code, we call the function circle(LENGTH, steps=33 times)

A General method - 1 (Regular non-star Polygon based)

Now, how do we know how many times we need to repeat the circle and step commands? To answer this question, we can make some observations on the above example:

A hexagonal star is a (6,2) star in which the vertex - 6 is an integer divisible order - 2,3 times
Octagonal stars are (8,2) stars, where the vertex - 8 can be divided by - 2,4 times
Type 9 personality is a (9,3) star, in which the vertex - 9 can be divided by - 3,3 times
To sum up, we can say that for degenerate gram with order n of M, the quotient of n/m is the number of sides of the polygon, and order m is the number of times we need to draw the polygon

Therefore, we should try the following methods:

For n,m degenerate stars, draw m regular polygons
And the drawn polygon should have n/m edges

Let's code

import turtle as t

t.shape('turtle')
t.color('#4F86F7')
t.pensize(2)

LENGTH=200

def degenerate(n,m):
    segments = n
    order = m

    sides = int(n/m)

    for _ in range(order):
        t.circle(LENGTH, steps=sides)
        t.circle(LENGTH, 360/segments)

t.circle(LENGTH)
degenerate(10,2)

The above code is valid! It draws the following 10 grams and 12 grams

You can also draw (12,3) and (12,4)

Interestingly, if you try to draw (10,5) and (12,6), it will draw the following:

This reminds you that the possible order (the value of m) is
2 to (n-1)/2-1. Basically, the order cannot be 1 or half of the number of vertices

With the general code above, do we cover all degenerate regular polygons?

(10,4) star

In the example we are looking at, a degenerate star polygon can consist of multiple regular (non star) polygons. But (10,4) is a strange example. In the last article, we wrote a code to draw regular stars. Let's see what happens when we use it here

import turtle as t

t.shape('turtle')
t.color('#4F86F7')
t.pensize(2)

LENGTH=150

def regular_star(n,m):
    """ n = number of pointies
        m = order of the star,
            (number of skips + 1)
    """

    angle = 360*m/n

    for count in range(n):
       t.forward(LENGTH)
       t.right(angle)

regular_star(10,4)


Yes, we got a five pointed star! But we should have 10 vertices 😦

A new type of degradation

Therefore, (10,4) is a new degenerate star, n, which cannot be divided by whole, m, so we can't draw it with the function we wrote before.

We can draw this type of degenerate star by overlapping two stars. Unfortunately, we can no longer use the step parameter of the circle. We need to draw an ordinary star and then rotate it to draw another star. To do this, we will first modify our regular_star function, as shown below

def regular_star(n,m):
    """ n = number of pointies
        m = order of the star,
            (number of skips + 1)
    """

    angle = 360*m/n

    t.left(180-angle/2)

    for count in range(n):
       t.forward(LENGTH)
       t.right(angle)

    t.right(180-angle/2)

The above code adds t.left (180 angle / 2) to place the regular star on the left side of the turtle. This is similar to how the circle command behaves

Notice that the two stars above are drawn to the left of the turtle

We will also make the following changes to our code

import turtle as t
import math

t.shape('turtle')
t.color('#4F86F7')
t.pensize(2)

RADIUS=150


def regular_star(n,m):
    """ n = number of pointies
        m = order of the star,
            (number of skips + 1)
    """

    angle = 360*m/n
    center_angle = 2*m*math.pi/n

    t.left(180-angle/2)


    length=2*RADIUS*math.sin(center_angle/2)

    for count in range(n):
       t.forward(length)
       t.right(angle)

    t.right(180-angle/2)

regular_star(7,2)
t.circle(RADIUS)

In the above code, we made the following changes

  • Replaced global LENGTH and RADIUS. This is to let us draw a star with a defined RADIUS
  • Calculate a center angle, which is the angle of the line segment on the center m. center_angle is the length of the arch in radians
  • We use center_angle and RADIUS to calculate the arm length of stars

    We can now draw a star to the left of a turtle with a defined radius. We can even overlap them all

Better generic approach

Our goal is to establish a better general method to draw any degenerate regular polygon star with a given number and order of vertices. Let's start listing the properties of degraded stars again

Star characteristics
(6,2) 2 overlapping triangles
(8,2) 2 overlapping squares
(9,3) 3 overlapping triangles
(10,4) two overlapping five pointed stars
(14,4) 2 (7,2) stars
(14,6) 2 (7,3) stars
(15,6) three overlapping five pointed stars
The above can be verified by trying on a piece of paper (or just thinking too much)

This is the algorithm we can use to draw all the above stars

Find the greatest common divisor (GCD) n and m
Divide N and m by GCD to obtain stars (N / GCD, M / GCD)
Draw (n/gcd,m/gcd) stars
Move the turtle to another position on the circumscribed circle determined by 360/n degrees
Repeat 3 until we draw the number of times the star gcd
Let's try:

import turtle as t
import math

t.shape('turtle')
t.color('#4F86F7')
t.pensize(2)

RADIUS=150

def regular_star(n,m):
    """ n = number of pointies
        m = order of the star,
            (number of skips + 1)
    """
    angle = 360*m/n
    center_angle = 2*m*math.pi/n

    t.left(180-angle/2)

    length=2*RADIUS*math.sin(center_angle/2)

    for count in range(n):
       t.forward(length)
       t.right(angle)

    t.right(180-angle/2)


def star(n,m):
    """ n = number of pointies
        m = order of the star,
            (number of skips + 1)
    """
    gcd = math.gcd(n,m)
    new_n = int(n/gcd)
    new_m = int(m/gcd)
    segment_angle = 360/n

    for _ in range(gcd):
        regular_star(new_n, new_m)
        t.penup()
        t.circle(RADIUS, segment_angle)
        t.pendown()

star(10,4)

Through the above code, when gcd is 1, we get a regular non degenerate star or polygon. So we wrote code to summarize:

  • Regular polygon
  • Ordinary non degenerate star!
  • Ordinary degenerate star~
  • Strange star:)

example:

You can overlap all orders of a fixed number of vertices to get a beautiful image, for example

def all_stars(n):
    for x in range(int(n/2)):
        star(n,x+1)


all_stars(10)
t.hide()


Yeah~

Added by ahasanat on Fri, 28 Jan 2022 10:14:11 +0200