R
R
Rav2018-05-15 07:59:50
Python
Rav, 2018-05-15 07:59:50

How to draw with turtle using def functions?

Usually, to draw a hexagon, I would write like this:

sides = 6

import turtle

win = turtle.Screen()
table = turtle.Turtle()
win.setup(600,600)

table.pencolor("black")
table.begin_fill()
table.circle(100, 360, sides)
table.fillcolor("grern")
table.end_fill()

win.exitonclick()

But how can you output something like this using def function ?
You need to have four parameters. For example:
def table (sides, length, x, y): #length это длина пикселя каждой стороны, x and y это точки, где turtle начнёт рисовать

If there are 6 sides, then each line has a length of 100 pixels, the length parameter.
Circle () won't work here. It is necessary using loop. For 6 sides, we would divide 360 ​​by 6.
What is the right way to output all this to def functions ?
for loop would be something like this:
for i in range(6):
    table.forward(100)
    table.left(60)

And how to be with function I will not understand in any way. Tell.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Paperno, 2018-05-15
@AviPaperno

import turtle

def draw_n(n, length, x = 0,y = 0):
    turtle.penup()
    turtle.setpos(x,y)
    turtle.pendown()
    deg = 180 - 180*(n-2)/n
    print(deg)
    for i in range(n):
        turtle.left(deg)
        turtle.forward(length)

win = turtle.Screen()
table = turtle.Turtle()
win.setup(600,600)
table.pencolor("black")
draw_n(8,100,100,100)

About something like this. First, the angle of rotation is calculated, for regular n-gons, the standard formula. The parameters are n - the number of corners, length - the length of the side, and the initial coordinates.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question