@Charanpreet/

3.07 Examples - Drawing a triangle grid

Python (with Turtle)

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.0041645073668210575","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"aW1wb3J0IHR1cnRsZQoKZGVmIGRyYXdfcmlnaHRfdHJpYW5nbGUod2lkdGgsIGhlaWdodCk6CiAgc3RhcnRpbmdfeCA9IHR1cnRsZS54Y29yKCkKICBzdGFydGluZ195ID0gdHVydGxlLnljb3IoKQogIHR1cnRsZS5zZXRoZWFkaW5nKDApICMgRmFjZSB0byB0aGUgcmlnaHQKICB0dXJ0bGUucGVuZG93bigpCiAgdHVydGxlLmZvcndhcmQod2lkdGgpCiAgdHVydGxlLmxlZnQoOTApCiAgdHVydGxlLmZvcndhcmQoaGVpZ2h0KQogIHR1cnRsZS5nb3RvKHN0YXJ0aW5nX3gsIHN0YXJ0aW5nX3kpCiAgdHVydGxlLnBlbnVwKCkKCkMKZGVmIGRyYXdfb2Zmc2V0X2dyaWRfb2ZfdHJpYW5nbGVzKG51bV9jb2x1bW5zLCBudW1fdHJpYW5nbGVzX3Blcl9jb2x1bW4sIHRyaWFuZ2xlX3dpZHRoLCB0cmlhbmdsZV9oZWlnaHQpOgogIHN0YXJ0aW5nX3ggPSB0dXJ0bGUueGNvcigpCiAgc3RhcnRpbmdfeSA9IHR1cnRsZS55Y29yKCkKICBmb3IgY29sdW1uX2luZGV4IGluIHJhbmdlKG51bV9jb2x1bW5zKToKICAgIHR1cnRsZS5zZXR4KHN0YXJ0aW5nX3ggKyBjb2x1bW5faW5kZXggKiB0cmlhbmdsZV93aWR0aCkKICAgIGlmIGNvbHVtbl9pbmRleCAlIDIgPT0gMDoKICAgICAgdHVydGxlLnNldHkoc3RhcnRpbmdfeSArIHRyaWFuZ2xlX2hlaWdodCAvIDIpCiAgICBlbHNlOgogICAgICB0dXJ0bGUuc2V0eShzdGFydGluZ195KQogICAgZHJhd19jb2x1bW5fb2ZfdHJpYW5nbGVzKG51bV90cmlhbmdsZXNfcGVyX2NvbHVtbiwgdHJpYW5nbGVfd2lkdGgsIHRyaWFuZ2xlX2hlaWdodCkKICB0dXJ0bGUuc2V0eChzdGFydGluZ194KQogIHR1cnRsZS5zZXR5KHN0YXJ0aW5nX3kpCgojIERyYXdpbmcgYSBncmlkIG9mIHRyaWFuZ2xlcwp0dXJ0bGUucGVudXAoKQoKIyBZb3UgY2FuIHNldCB0aGUgYW5pbWF0aW9uIHNwZWVkIGZyb20gMSAoc2xvdykgdG8gMTAgKGZhc3QpLgojIFlvdSBjYW4gYWxzbyBzZXQgdGhlIHNwZWVkIHRvIDAgdG8gZ28gYXMgZmFzdCBhcyBwb3NzaWJsZS4KdHVydGxlLnNwZWVkKDApCgp0dXJ0bGUuZ290bygtMTAwLCAxMDApCnR1cnRsZS5jb2xvcignYmx1ZScpCmRyYXdfY29sdW1uX29mX3RyaWFuZ2xlcyg3LCAzMCwgMjApCgp0dXJ0bGUuZ290bygtNTAsIDEwMCkKdHVydGxlLmNvbG9yKCdyZWQnKQpkcmF3X2dyaWRfb2ZfdHJpYW5nbGVzKDUsIDQsIDIwLCAzMCkKCnR1cnRsZS5nb3RvKC0xMDAsIC0xMDApCnR1cnRsZS5jb2xvcignZ3JlZW4nKQpkcmF3X29mZnNldF9ncmlkX29mX3RyaWFuZ2xlcyg5LCA3LCAyMCwgMjApCg=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import turtle

def draw_right_triangle(width, height):
  starting_x = turtle.xcor()
  starting_y = turtle.ycor()
  turtle.setheading(0) # Face to the right
  turtle.pendown()
  turtle.forward(width)
  turtle.left(90)
  turtle.forward(height)
  turtle.goto(starting_x, starting_y)
  turtle.penup()

C
def draw_offset_grid_of_triangles(num_columns, num_triangles_per_column, triangle_width, triangle_height):
  starting_x = turtle.xcor()
  starting_y = turtle.ycor()
  for column_index in range(num_columns):
    turtle.setx(starting_x + column_index * triangle_width)
    if column_index % 2 == 0:
      turtle.sety(starting_y + triangle_height / 2)
    else:
      turtle.sety(starting_y)
    draw_column_of_triangles(num_triangles_per_column, triangle_width, triangle_height)
  turtle.setx(starting_x)
  turtle.sety(starting_y)

# Drawing a grid of triangles
turtle.penup()

# You can set the animation speed from 1 (slow) to 10 (fast).
# You can also set the speed to 0 to go as fast as possible.
turtle.speed(0)

turtle.goto(-100, 100)
turtle.color('blue')
draw_column_of_triangles(7, 30, 20)

turtle.goto(-50, 100)
turtle.color('red')
draw_grid_of_triangles(5, 4, 20, 30)

turtle.goto(-100, -100)
turtle.color('green')
draw_offset_grid_of_triangles(9, 7, 20, 20)
result
console