1

I was just wondering how to draw some trapezoids in my turtle code.

I want my output to be like this:

expected

What my output is right now:

actual

Here's the code I've written so far:

import turtle as trtl

num_sides = 6
side_length = 15

circumradius = side_length

trtl.pencolor((245, 176, 66))
trtl.pensize(8)

for move_turtle in range(1):
    trtl.penup()
    trtl.sety(-circumradius)
    trtl.pendown()

    trtl.circle(circumradius, steps = num_sides)

    circumradius *= 2

side_length = side_length + 8


trtl.pensize(12)

for move_turtle in range(1):
    trtl.pencolor((255, 83, 71))
    trtl.penup()
    trtl.sety(-circumradius)
    trtl.pendown()

    trtl.circle(circumradius, steps = num_sides)

    circumradius *= 2

for move_turtle in range(1):
    trtl.pencolor((247, 220, 67))
    trtl.penup()
    trtl.sety(-circumradius)
    trtl.pendown()

    trtl.circle(circumradius, steps = num_sides)

    circumradius *= 2


trtl.hideturtle()

What techniques can I use to get the desired output?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Does this answer your question? [Different lines of color in a hexagon](https://stackoverflow.com/questions/63946756/different-lines-of-color-in-a-hexagon) – ggorlen Oct 15 '20 at 21:23

1 Answers1

0

The approach I often take when given patterns like this is to think about a slight-of-hand trick that can produce the result with less trouble than figuring out how to draw something potentially fussy like a trapezoid.

In this case, if you remove the white spaces, we have a "pie"-shaped circle of different solid colors radiating from the middle. If we can manage to solve that simpler problem, then we can draw white lines on top and wind up with the desired result.

The only trig we need is the angle-to-coordinates formula:

x = cos(radians(angle)) * radius
y = sin(radians(angle)) * radius

Using this, we can iterate over n points (with some rotational offset) along the circumference of a circle with a loop and fill in the colors to make a pie:

import math
import turtle

def draw_pie(t, r, n, colors, rot_offset=0.5):
    for i in range(n + 1):
        a = 360 / n * (i + rot_offset)
        t.color(colors[i%len(colors)])
        t.begin_fill()
        t.goto(0, 0)
        x = math.cos(math.radians(a)) * r
        y = math.sin(math.radians(a)) * r
        t.goto(x, y)
        t.end_fill()

if __name__ == "__main__":
    t = turtle.Turtle()
    t.screen.setup(540, 540)
    t.penup()
    t.speed("fastest")
    sides = 6
    draw_pie(t, 450, sides, ["#dd2", "orange", "#d02"])
    t.ht()
    turtle.exitonclick()

Next, we need to draw a bunch of thick white lines with square corners. Unfortunately, turtle draws rounded edges by default which aren't suitable for our needs. There are many tricks for making square edges (stamping is a reasonable method). The approach I used here was writing a custom rectangle drawing function that takes advantage of turtle.distance and turtle.setheading(turtle.towards(x, y)) which lets me point to the next point along the circumference of the circle. The rest is just loops to compute angles and picking the right values.

Here's the code:

import math
import turtle

def draw_rect(t, x, y, xx, yy, width):
    t.goto(x, y)
    t.setheading(t.towards(xx, yy))
    t.begin_fill()
    
    for d in [t.distance(xx, yy), width] * 2:
        t.forward(d)
        t.left(90)

    t.end_fill()

def draw_pie(t, r, n, colors, rot_offset=0.5):
    for i in range(n + 1):
        a = 360 / n * (i + rot_offset)
        t.color(colors[i%len(colors)])
        t.begin_fill()
        t.goto(0, 0)
        x = math.cos(math.radians(a)) * r
        y = math.sin(math.radians(a)) * r
        t.goto(x, y)
        t.end_fill()

def draw_reg_polygon(t, r, n, thickness, rot_offset=0.5):
    for i in range(n):
        a = 360 / n * (i + rot_offset)
        x = math.cos(math.radians(a)) * r
        y = math.sin(math.radians(a)) * r
        a = 360 / n * (1 + i + rot_offset)
        xx = math.cos(math.radians(a)) * r
        yy = math.sin(math.radians(a)) * r
        draw_rect(t, x, y, xx, yy, thickness)

if __name__ == "__main__":
    t = turtle.Turtle()
    t.screen.setup(540, 540)
    t.penup()
    t.speed("fastest")
    sides = 6
    draw_pie(t, 450, sides, ["#dd2", "orange", "#d02"])
    t.color("white")

    for r in range(50, 500, 100):
        draw_reg_polygon(t, r, sides, 45)

    t.ht()
    turtle.exitonclick()
ggorlen
  • 44,755
  • 7
  • 76
  • 106