I don't know how to draw a turtleneck.

Asked 2 years ago, Updated 2 years ago, 36 views

I would like to make the following two diagrams, but I don't understand the code very well.I'm a beginner. Could you tell me?

Enter a description of the image here

python

2022-09-30 21:47

1 Answers

At the beginning of Python's corresponding documentation, depending on how well you understand this part, you will be divided into those who think it's easy and those who think it's difficult.

turtle --- Turtle Graphics

Turtle graphics is a common way to introduce programming to children. This was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.

Imagine a robot turtle moving from (0,0) in the x-y plane.turtle.forward(15) command moves in the direction it faces 15 pixels (on the screen!) and draws a line along the movement.turtle.left(25) command turns 25 degrees counterclockwise on the spot.

Combining these instructions with other similar instructions makes it easy to draw complex shapes and pictures.

The circle itself is circle().

"Drawing two equilateral triangles" and "Creating six small circles" are considered independently, so you won't understand.

"I think ""draw two regular triangles with circles on each vertex"" and to draw them, ""draw three circles and three straight lines"" is probably the concept of the Turtle Graphics style."

Possible actions include:

  • Define the "draw an equilateral triangle with a circle at each vertex" function

    • Repeat the following three times.
      • Draw a circle with an orange pen
      • Turn the angle (60 degrees right)
      • Draw a straight line with a purple pen
      • Turn the angle (60 degrees right)
  • Call the above functions once

  • Put up your pen
  • Move to the next drawing start position
    (Move half the sides of the triangle, rotate 90 degrees to the right, and move one-tenth of the sides)
  • Change angle (turn left 30 degrees)
  • Put your pen down
  • Call the above functions once

Define the function Draw an equilateral triangle with a circle at each vertex

  • Repeat the following three times.
    • Draw a circle with an orange pen
    • Turn the angle (60 degrees right)
    • Draw a straight line with a purple pen
    • Turn the angle (60 degrees right)
  • Draw a circle with an orange pen
  • Turn the angle (60 degrees right)
  • Draw a straight line with a purple pen
  • Turn the angle (60 degrees right)

Call the above function once

Here's the sauce

import turtle

# Define the function "draw an equilateral triangle with a circle at each vertex"
def CircleAndTriangle(t,csize,lsize):
    for_in range(3):
        t.pencolor ('orange')
        t.circle(csize)
        t.pencolor ('purple')
        t.right(60)
        t.forward(lsize)
        t.right(60)

t=turtle.Turtle()
t.pensize(3)
csize = 20# radius of circle
lsize = 200 # Triangle side length

CircleAndTriangle(t,csize,lsize)
t.penup()
t.forward (lsize/2)
t.right (90)
t.forward (lsize/10)
t.left(30)
t.pendown()
CircleAndTriangle(t,csize,lsize)
t.hideturtle()
US>turtle.done()

Spiral diagrams can be treated as follows:
By the way, it seems that there are 20 triangles drawn in the question diagram.

  • Define

    Draw an equilateral triangle that changes the color of each side with a specified length

    • Repeat the following with a list of red, purple, and orange colors.
      • Draw a straight line with this colored pen
      • Turn the angle 120 degrees to the left
  • Set and store the following values

    • Number of triangles to draw
    • 360 (angle) divided by the number of triangles
    • Initial triangle side length
    • Increments to the side length of the next triangle
    • Distance of starting position to draw the next triangle
  • Create and loop the side length list of triangles to draw with range
    • Call the above functions once in this length
    • Put up your pen
    • Change the angle (right 360 divided by the number of triangles drawn)
    • Move the starting position of the next triangle
    • Put your pen down

Define the function "draw an equilateral triangle that changes the color of each side with a specified length"

  • Repeat the following with a list of red, purple, and orange colors.
    • Draw a straight line with this colored pen
    • Turn the angle 120 degrees to the left
  • Draw a straight line with this colored pen
  • Turn the angle 120 degrees to the left

Set and store the following values

  • Number of triangles to draw
  • 360 (angle) divided by the number of triangles
  • Initial triangle side length
  • Increments to the side length of the next triangle
  • Distance of starting position to draw the next triangle
  • Call the above functions once in this length
  • Put up your pen
  • Change the angle (right 360 divided by the number of triangles drawn)
  • Move the starting position of the next triangle
  • Put your pen down

Here's the sauce

import turtle

# Define the function "draw an equilateral triangle that changes the color of each side with a specified length"
def Triangle(t, lsize):
    for c in ['red', 'purple', 'orange']:
        t.pencolor(c)
        t.forward(lsize)
        t.left (120)

t=turtle.Turtle()
t.pensize(3)
divcount=20#Number of triangles drawn
age=360/divcount#360 (angle) divided by the number of triangles drawn
csize = 10 # Distance of starting position to draw the next triangle
tsizeini=20# initial value of triangle side length
tsizediff = 10 # Incremental value to the side length of the next triangle

# Create a list of sides of the triangle to be drawn in a range and loop.
for lsize in range (tsizeini, (tsizeini+(tsizediff*divcount))), tsizeiff:
    Triangle(t, lsize)
    t.penup()
    t.right(adegree)
    t.forward(csize)
    t.pendown()
t.hideturtle()
US>turtle.done()

Also, if you are not familiar with coding, you should try the tutorial around here.
asweigart/simple-turtle-tutorial-for-python/simple_turtle_tutorial.md
Easy Designs-Turtle Graphics Python
Python 3 – Turtle graphics
Turtle Programming in Python
Python Turtle Graphics Tutorial#1-IntroductionYouTube
Text-Based Tutorial & Code:Text above
Complete Python Turtle Graphics Overview! (From Beginner to Advanced) Another YouTube


2022-09-30 21:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.