I tried drawing a line using turtle

Turtle module

A Python version of turtle graphics that allows you to manipulate a turtle with a pen to draw a picture. Scratch is famous for children, but it seems to be a good teaching material depending on how you use it. I thought that this drawing was amazing and tried using it ...

――You can draw such a line art (like a forest or a forest)

スクリーンショット 2020-06-15 23.35.58.jpg

Here is the sample code. Let's create a function called forest.

turtlePicture.py


#
#Import turtle and draw a line
# 2020.06.15 ProOJI
#
import turtle

#kameSan object creation
kameSan = turtle.Turtle()

#Create a method (recursive usage)
def forest(n, length:int=1000)->None:
    if n <= 0:
        kameSan.forward(length)
        return

    '''Recursion 1 length= 1000 * 0.5 '''
    forest(n-1, length * 0.5)

    #Rotate 85 degrees to the right
    #From gentle to sharp
    kameSan.right(85)

    '''Recursive 2 length= 1000 ÷ 3 '''
    forest(n-1, length / 3)

    #Rotate 170 degrees to the left
    #Draw a sharp needle on a sharp turn
    kameSan.left(170)

    '''Recursion 3 length= 1000 ÷ 3 '''
    forest(n-1, length / 3)

    #Rotate 85 degrees to the right
    #Land in a gentle direction again
    kameSan.right(85)
    '''Recursion 4 length= 1000 ÷ 0.38 '''
    forest(n-1, length * 0.38)

if __name__ == '__main__':
    kameSan.penup()
    kameSan.setpos(270, -20)
    kameSan.pendown()
    kameSan.left(180)
    kameSan.speed(0)
    forest(5)
    kameSan.done()

By doing this, you can draw a line. Also, if you make some modifications and set a color change in the recursive part ...

スクリーンショット 2020-06-15 23.52.20.jpg

--Sample code (colored)

turtlePictureWithColor.py


#
#Import turtle and draw a line
#kameSan draws lines when moving
# 2020.06.15 ProOJI
#
import turtle

#kameSan object creation
kameSan = turtle.Turtle()

#Create a method (recursive usage)
def forest(n, length:int=1000)->None:
    if n <= 0:
        kameSan.forward(length)
        return

    '''Recursion 1 length= 1000 * 0.5 '''
    kameSan.pencolor("red")
    forest(n-1, length * 0.5)

    #Rotate 85 degrees to the right
    #From gentle to sharp
    kameSan.right(85)

    '''Recursive 2 length= 1000 ÷ 3 '''
    kameSan.pencolor("blue")
    forest(n-1, length / 3)

    #Rotate 170 degrees to the left
    #Draw a sharp needle on a sharp turn
    kameSan.left(170)

    '''Recursion 3 length= 1000 ÷ 3 '''
    kameSan.pencolor("green")
    forest(n-1, length / 3)

    #Rotate 85 degrees to the right
    #Land in a gentle direction again
    kameSan.right(85)
    '''Recursion 4 length= 1000 ÷ 0.38 '''
    forest(n-1, length * 0.38)

if __name__ == '__main__':
    #penup No drawing because the pen is raised
    kameSan.penup()
    #Move to Position
    kameSan.setpos(270, -20)
    #pendown The pen is lowered so it is drawn from here
    kameSan.pendown()
    #Turn 180 degrees to the left
    kameSan.left(180)

    kameSan.speed(0)
    forest(5)
    kameSan.done()

You can see which code draws the line and how.

Recommended Posts

I tried drawing a line using turtle
I tried drawing a pseudo fractal figure using Python
I tried using Twitter api and Line api
I tried playing a ○ ✕ game using TensorFlow
I tried using pipenv, so a memo
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
I tried using Pythonect, a dataflow programming language.
I tried reading a CSV file using Python
I tried using a database (sqlite3) with kivy
I tried to make a ○ ✕ game using TensorFlow
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
I tried using BigQuery ML
I tried hosting a Pytorch sample model using TorchServe
I tried using Amazon Glacier
I tried using git inspector
[Python] I tried using OpenPose
[Python] I tried running a local server using flask
I tried using magenta / TensorFlow
I tried reading data from a file using Node.js.
I tried using Python (3) instead of a scientific calculator
PyTorch Learning Note 2 (I tried using a pre-trained model)
I tried using AWS Chalice
I tried to draw a configuration diagram using Diagrams
I tried using Slack emojinator
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
I tried using Tensorboard, a visualization tool for machine learning
I tried to automate [a certain task] using Raspberry Pi
I tried to make a stopwatch using tkinter in python
I tried to make a simple text editor using PyQt
I made a Chatbot using LINE Messaging API and Python
I tried using Rotrics Dex Arm # 2
I made a Line-bot using Python!
Drawing a silverstone curve using python
I tried using Rotrics Dex Arm